Installation
composer require kominfogusit/laravel-helper
Publish the config (if needed):
php artisan vendor:publish --provider="KominfoGusit\Helper\HelperServiceProvider"
First Use Case Quickly generate a UUID:
use KominfoGusit\Helper\Facades\Helper;
$uuid = Helper::uuid(); // Returns a v4 UUID string
Where to Look First
KominfoGusit\Helper\Facades\Helper (primary entry point).config/helper.php (if published).README.md for core utilities (e.g., slugify(), randomString(), array_diff_recursive()).String Manipulation
// Slugify a string
$slug = Helper::slugify('Hello World!'); // "hello-world"
// Random string generation
$token = Helper::randomString(16); // e.g., "aB3x9KpL2qR7tY1"
Array Utilities
// Deep array diff (returns associative array of differences)
$diff = Helper::arrayDiffRecursive(
['a' => 1, 'b' => ['x' => 10]],
['a' => 1, 'b' => ['x' => 20]]
);
// Merge arrays with custom logic
$merged = Helper::arrayMergeRecursiveDistinct($array1, $array2);
Data Validation & Sanitization
// Sanitize input (e.g., remove HTML tags)
$cleanInput = Helper::sanitize($userInput);
// Check if a string is a valid email
$isValid = Helper::isEmail('[email protected]');
File & Path Handling
// Generate a unique filename
$filename = Helper::uniqueFilename('image', 'jpg');
// Get the public path
$path = Helper::publicPath('assets/images');
Date/Time Helpers
// Format a date (e.g., for Indonesian locale)
$formatted = Helper::formatDate('2023-10-01', 'd-m-Y');
// Check if a date is within a range
$isInRange = Helper::isDateInRange('2023-10-01', '2023-01-01', '2023-12-31');
Integration with Laravel Features
// Register in a service provider
Blade::directive('uuid', fn() => "<?php echo \\KominfoGusit\\Helper\Facades\\Helper::uuid(); ?>");
// Usage in Blade:
<div>{{-- @uuid --}}</div>
use KominfoGusit\Helper\Facades\Helper;
public function rules()
{
return [
'email' => ['required', 'email', fn($attr, $value) =>
Helper::isValidEmail($value) ? 'valid' : 'invalid'
],
];
}
Testing
// Mock the Helper facade in tests
Helper::shouldReceive('slugify')->andReturn('test-slug');
Locale-Specific Functions
formatDate()) may rely on the app’s locale. Ensure App::setLocale() is set correctly if using non-default locales.Helper::formatDate('2023-10-01', 'd-m-Y', 'id');
UUID Version Confusion
Helper::extend('uuidv1', fn() => Str::uuid('v1'));
Array Merge Overwrites
arrayMergeRecursiveDistinct may not handle all edge cases (e.g., nested arrays with mixed types). Test with complex data structures.array_merge_recursive with custom logic if needed.Sanitization Edge Cases
sanitize() may strip critical characters (e.g., in code snippets). Use sparingly for user-generated content.strip_tags() or allowlists:
$safeHtml = Helper::sanitize($input, ['<b>', '<i>']);
Public Path Caching
publicPath() caches the base path. Clear the cache if paths change dynamically (e.g., in multi-tenant apps).Helper::refreshPublicPathCache().Performance with Large Arrays
arrayDiffRecursive() can be slow for very large arrays. For production, consider batch processing or alternative approaches.Enable Debug Mode
Add this to config/helper.php to log helper calls:
'debug' => env('APP_DEBUG', false),
Check for Deprecated Methods
The package may evolve. Use php artisan vendor:list --installed to track updates and check the changelog.
Override Default Behavior Bind custom implementations in the service provider:
$this->app->bind('helper', fn($app) => new CustomHelper());
Add Custom Helpers
Extend the Helper facade dynamically:
Helper::extend('customHelper', fn($arg) => $arg * 2);
// Usage: Helper::customHelper(5); // Returns 10
Create a Helper Trait For reusable logic across services:
use KominfoGusit\Helper\Traits\HelperTrait;
class MyService {
use HelperTrait;
public function doSomething() {
$slug = $this->slugify('Test');
}
}
Localization Hooks Override locale-specific helpers in a service provider:
Helper::macro('formatDate', function($date, $format, $locale = null) {
// Custom logic here
});
Event Listeners for Helper Calls Log or monitor helper usage via events (if the package supports it). Example:
// Hypothetical event (check package docs)
event(new \KominfoGusit\Helper\Events\HelperCalled($helperName, $args));
How can I help you explore Laravel packages today?