sweetchuck/utils
Utility helpers for PHP projects: small, reusable classes and functions aimed at simplifying common tasks and reducing boilerplate. Includes tested code with CI and coverage badges. Suitable as a lightweight dependency for general-purpose apps and libraries.
Installation Add the package via Composer:
composer require sweetchuck/utils
Register the service provider in config/app.php:
'providers' => [
Sweetchuck\Utils\UtilsServiceProvider::class,
],
First Use Case Access utility classes via the facade:
use Sweetchuck\Utils\Facades\Utils;
// Example: String manipulation
$result = Utils::str()->slugify('Hello World!'); // Returns 'hello-world'
// Example: Array helpers
$flattened = Utils::arr()->flatten([1, [2, 3]]); // Returns [1, 2, 3]
Where to Look First
Sweetchuck\Utils\Facades\Utils (main entry point).src/Sweetchuck/Utils directory for available classes (e.g., StringHelper, ArrayHelper).tests/ directory may contain usage examples (if any exist).String Manipulation
Use the str() helper for common string operations:
// Slugify, truncate, or format strings
Utils::str()->slugify('Test String');
Utils::str()->truncate('Long text...', 10);
Array Operations
Leverage arr() for array utilities:
// Flatten, merge, or search arrays
Utils::arr()->flatten([1, [2, 3]]);
Utils::arr()->merge([1, 2], [2, 3]); // Returns [1, 2, 3]
Integration with Laravel
$this->app->bind('custom.helper', function () {
return new \Sweetchuck\Utils\CustomHelper();
});
UtilsServiceProvider for custom logic.Blade Directives Register Blade helpers for frontend use:
// In a service provider
Blade::directive('slug', function ($expression) {
return "<?php echo Utils::str()->slugify({$expression}); ?>";
});
Usage in Blade:
<h1>{{ slug('My Title') }}</h1>>
Model Observers or Accessors Use utilities in Eloquent models:
class Post extends Model
{
public function getSlugAttribute()
{
return Utils::str()->slugify($this->title);
}
}
Outdated Package
Limited Documentation
slugify, flatten).src/Sweetchuck/Utils/).Facade Overhead
Utils::str()->method()) can be verbose. Consider:
$stringHelper = app(Sweetchuck\Utils\StringHelper::class);
$stringHelper->slugify('Test');
Namespace Conflicts
ArrayHelper vs. Laravel’s Arrayable).No Type Safety
slugify returns string, but no @return hints).Check Method Existence
If a method fails (e.g., Utils::str()->unknownMethod()), verify it exists in:
\Sweetchuck\Utils\StringHelper::class
Inspect Source Code For undocumented behavior, review:
vendor/sweetchuck/utils/src/Sweetchuck/Utils/StringHelper.php
Test Edge Cases
Utils::str()->slugify(''); // Returns ''
Utils::arr()->flatten([]); // Returns []
Custom Helpers Extend existing classes:
class ExtendedStringHelper extends \Sweetchuck\Utils\StringHelper
{
public function customMethod($str)
{
return strtoupper($str);
}
}
Register in UtilsServiceProvider:
$this->app->bind(
\Sweetchuck\Utils\StringHelper::class,
ExtendedStringHelper::class
);
Add New Utilities
Create a new helper class (e.g., DateHelper) and bind it:
$this->app->singleton('utils.date', function () {
return new \Sweetchuck\Utils\DateHelper();
});
Override Facade Replace the facade to add custom methods:
class CustomUtils extends \Sweetchuck\Utils\Facades\Utils
{
public static function customMethod()
{
return 'Custom logic';
}
}
Update config/app.php to use CustomUtils.
No Config File
The package likely has no config/utils.php. All behavior is code-driven.
Service Provider Binding Ensure the provider is registered after Laravel’s core providers to avoid conflicts.
Facade Alias
Verify the facade alias exists in config/app.php:
'aliases' => [
'Utils' => \Sweetchuck\Utils\Facades\Utils::class,
],
How can I help you explore Laravel packages today?