Installation Add the package via Composer in a Laravel/Symfony project:
composer require bastsys/utils-bundle
For Laravel, manually register the bundle in config/app.php under providers (Symfony-style bundles may not work directly; check for Laravel adapters or use standalone utilities).
First Use Case
The package provides utility classes like StringUtils, ArrayUtils, and FileUtils. Start with a simple string manipulation:
use Bastsys\UtilsBundle\Utils\StringUtils;
$text = StringUtils::slugify('Hello World!');
// Output: "hello-world"
Where to Look First
src/Utils/ for available utilities (e.g., StringUtils, ArrayUtils).tests/ for real-world examples (though the package has no stars/dependents, tests may be minimal).String Manipulation
Use StringUtils for common tasks like slugifying, truncating, or formatting:
$slug = StringUtils::slugify('Laravel 10 Guide');
$truncated = StringUtils::truncate('Long text...', 10);
Array Operations
Leverage ArrayUtils for nested array operations:
$flattened = ArrayUtils::flatten(['a' => ['b', 'c']]); // ['b', 'c']
$merged = ArrayUtils::mergeRecursive(['a' => 1], ['a' => 2]); // ['a' => 2]
File Handling
Use FileUtils for path/filename operations (e.g., generating unique filenames):
$uniqueName = FileUtils::generateUniqueFilename('image', 'jpg');
// Output: "image_1678901234.jpg"
Integration with Laravel
$this->app->singleton(StringUtils::class, function () {
return new StringUtils();
});
app/Helpers/utils.php:
if (!function_exists('slugify')) {
function slugify($text) {
return StringUtils::slugify($text);
}
}
Validation Combine with Laravel’s validator for custom rules:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($data, [
'title' => ['required', function ($attribute, $value, $fail) {
if (StringUtils::length($value) > 100) {
$fail('Title must be less than 100 characters.');
}
}]
]);
Symfony vs. Laravel Compatibility
No Active Maintenance
StringUtils).Limited Documentation
StringUtils::camelCase()).src/Utils/).Performance Overhead
collect()) where possible.Check Input/Output
For StringUtils or ArrayUtils, log inputs/outputs to debug:
\Log::debug('Slugify input:', ['text' => 'Café']);
$slug = StringUtils::slugify('Café');
\Log::debug('Slugify output:', ['slug' => $slug]);
Unicode Handling
Some string methods may mishandle non-ASCII characters (e.g., é, ñ).
mb_strtolower() or use Str::of() in Laravel.FileUtils Quirks
Storage facade or Str::of() for consistency:
use Illuminate\Support\Str;
$path = Str::of(FileUtils::generateUniqueFilename('file', 'txt'))->replace('/', DIRECTORY_SEPARATOR);
Custom Utilities
Extend existing classes (e.g., StringUtils) by creating a child class:
namespace App\Utils;
use Bastsys\UtilsBundle\Utils\StringUtils;
class AppStringUtils extends StringUtils {
public static function customMethod($text) {
return parent::slugify($text) . '_app';
}
}
Add to Laravel’s Str Helper
Override or extend Laravel’s Str helper to include package methods:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Str;
use Bastsys\UtilsBundle\Utils\StringUtils;
Str::macro('bastSlugify', function ($text) {
return StringUtils::slugify($text);
});
Usage:
$slug = Str::bastSlugify('Hello World');
Testing Mock utilities in tests to isolate logic:
$this->partialMock(StringUtils::class, ['slugify'])
->shouldReceive('slugify')
->andReturn('mocked-slug');
How can I help you explore Laravel packages today?