Installation Add the package via Composer:
composer require dontdrinkandroot/utils
Register the facade (if used) in config/app.php under aliases:
'Utils' => Dontdrinkandroot\Utils\Facades\Utils::class,
First Use Case
Use the Utils facade or helper directly in a Laravel controller or service:
use Dontdrinkandroot\Utils\Facades\Utils;
// Example: Generate a random string
$randomString = Utils::randomString(10);
// Example: Check if a string is sluggable
$isSluggable = Utils::isSluggable('test-string');
Key Classes to Explore
Utils facade (main entry point)StringUtils (string manipulation)ArrayUtils (array operations)FileUtils (file handling)DateUtils (date/time utilities)Check the source code for full functionality.
$slug = Utils::slugify('Hello World!', '-');
// Output: 'hello-world'
if (Utils::isEmail('test@example.com')) {
// Process email
}
$merged = Utils::arrayMergeRecursive([
'key' => 'value1'
], [
'key' => 'value2',
'nested' => ['key' => 'value']
]);
// Output: ['key' => 'value2', 'nested' => ['key' => 'value']]
$diff = Utils::arrayDiffAssoc([
'a' => 1, 'b' => 2
], [
'a' => 1, 'b' => 3
]);
// Output: ['b' => 2]
Utils::writeFile('path/to/file.txt', 'Hello World');
$content = Utils::readFile('path/to/file.txt');
Utils::createDirectory('storage/app/public/uploads');
$formatted = Utils::formatDate('2023-01-01', 'Y-m-d H:i:s');
// Output: '2023-01-01 00:00:00'
$diff = Utils::timeDifference('2023-01-01', '2023-01-02');
// Output: 1 day(s)
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'username' => ['required', function ($attribute, $value, $fail) {
if (!Utils::isSluggable($value)) {
$fail('The '.$attribute.' must be sluggable.');
}
}]
]);
public function handle($request, Closure $next) {
$request->merge([
'sanitized_input' => Utils::sanitizeInput($request->input('user_input'))
]);
return $next($request);
}
Add to composer.json under autoload.aliases:
"Dontdrinkandroot\\Utils\\Helpers" => "src/Helpers"
Then use in Blade:
{{ Utils::truncate('This is a long string', 10) }}
Namespace Conflicts
Dontdrinkandroot\Utils namespace. Ensure no naming collisions with other packages or custom classes.File Handling Permissions
FileUtils methods may fail silently if permissions are insufficient.chmod -R 775 storage).Deprecated PHP Features
create_function).No Laravel-Specific Optimizations
arrayMergeRecursive) may not leverage Laravel’s built-in helpers like array_merge_recursive.collect()->merge()).Limited Error Handling
readFile) lack robust error handling for edge cases (e.g., file not found).try {
$content = Utils::readFile('file.txt');
} catch (\Exception $e) {
Log::error($e->getMessage());
$content = null;
}
Enable Strict Typing
Add to composer.json to catch type-related issues early:
"config": {
"platform-check": true,
"platform": {
"php": "8.0"
}
}
Log Utility Outputs
Debug complex operations like arrayDiffAssoc:
$diff = Utils::arrayDiffAssoc($array1, $array2);
Log::debug('Array Diff:', ['diff' => $diff]);
Test Edge Cases
null values, or malformed inputs may break assumptions.Utils::slugify(null); // May return null or throw an error.
Customize Slug Generation
Override the slugify method in a service provider:
Utils::extend('slugify', function ($string, $separator = '-') {
// Custom logic
return strtolower(preg_replace('/[^a-z0-9]+/', $separator, $string));
});
Add Laravel-Specific Helpers Create a custom facade or trait to bridge Laravel and the package:
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
use Dontdrinkandroot\Utils\Facades\Utils as BaseUtils;
class Utils extends Facade {
protected static function getFacadeAccessor() {
return BaseUtils::class;
}
public static function laravelSlug($string) {
return static::slugify($string, '-');
}
}
Integrate with Laravel Events
Use the package in event listeners (e.g., sanitizing input on creating):
public function handle($event) {
$event->model->name = Utils::sanitizeInput($event->model->name);
}
Performance Optimization
$slug = Cache::remember("slug_{$input}", now()->addHours(1), function() use ($input) {
return Utils::slugify($input);
});
randomString uses alphanum by default). Override via method parameters:
Utils::randomString(10, 'abc123'); // Custom character set
How can I help you explore Laravel packages today?