Installation Add the bundle via Composer:
composer require ardent/util-bundle
Register the bundle in config/bundles.php:
Ardent\UtilBundle\ArdentUtilBundle::class => ['all' => true],
First Use Case: String Manipulation
Use the StringHelper for common string operations:
use Ardent\UtilBundle\Helper\StringHelper;
$cleaned = StringHelper::slugify('Hello World!'); // 'hello-world'
$truncated = StringHelper::truncate('Long string...', 10); // 'Long strin...'
Where to Look First
src/Helper/ for utilities like ArrayHelper, DateHelper, StringHelper, etc.src/Service/ for reusable services (e.g., CacheService, LoggerService).src/Traits/ for reusable logic (e.g., ArrayTrait, CollectionTrait).String Operations
Use StringHelper for consistent string formatting across projects:
// Slugify, truncate, or sanitize strings
$slug = StringHelper::slugify($userInput);
Array/Collection Manipulation
Leverage ArrayHelper for deep array operations:
// Get nested array value with a fallback
$value = ArrayHelper::get($array, 'path.to.value', 'default');
// Merge arrays recursively
$merged = ArrayHelper::merge($array1, $array2);
Date/Time Handling
Use DateHelper for timezone-aware operations:
// Convert a string to a Carbon instance
$date = DateHelper::parse('2023-12-31', 'Europe/Amsterdam');
// Format dates consistently
$formatted = DateHelper::format($date, 'Y-m-d H:i:s');
Service Integration
Inject CacheService or LoggerService into controllers/services:
public function __construct(private CacheService $cache) {}
public function getCachedData() {
return $this->cache->get('key', function() {
return $this->fetchExpensiveData();
});
}
Traits for Reusability Extend classes with traits to avoid duplication:
use Ardent\UtilBundle\Traits\ArrayTrait;
class MyService {
use ArrayTrait;
public function processData(array $data) {
return $this->arrayGet($data, 'key', []);
}
}
Namespace Collisions
Ardent\UtilBundle namespace. Ensure no conflicts with your project’s namespaces.config/app.php.Carbon Dependency
DateHelper) rely on nesbot/carbon. Ensure it’s installed:
composer require nesbot/carbon
Service Container Binding
CacheService are bound to the container but may require configuration (e.g., cache driver).config/services.php if needed:
'cache.driver' => env('CACHE_DRIVER', 'array'),
Trait Method Conflicts
ArrayTrait may override core PHP methods (e.g., arrayGet). Test thoroughly in your project.Check Helper Methods
dd() or dump() to inspect helper outputs:
dump(StringHelper::slugify('Test!')); // Debug slugification
Cache Service Issues
CacheService fails, verify the cache driver is configured in .env:
CACHE_DRIVER=file
String Sanitization
StringHelper::sanitize() may strip unexpected characters. Test edge cases:
StringHelper::sanitize('User <script>input</script>'); // 'User input'
Custom Helpers
class CustomStringHelper extends StringHelper {
public static function customSlugify($string) {
return parent::slugify($string) . '-custom';
}
}
Add New Traits
app/Traits/ and reuse them across services.Override Services
AppServiceProvider:
$this->app->bind(CacheService::class, function($app) {
return new CustomCacheService();
});
Configuration
config/packages/ardent_util.yaml:
string:
separator: '_' # Override slugify separator
How can I help you explore Laravel packages today?