alhames/phphelper
PHP Helper is a small set of utility classes and functions for PHP 7.1+ to make common tasks easier. Includes a Str helper (docs in Russian) and other lightweight tools to simplify everyday development.
Installation
composer require alhames/phphelper:^1.5.0
Register the service provider in config/app.php:
'providers' => [
// ...
Alhames\PhpHelper\PhpHelperServiceProvider::class,
],
First Use Case Quickly generate a UUID, slug, or leverage improved DateTime handling:
use Alhames\PhpHelper\Facades\PhpHelper;
// Generate UUID
$uuid = PhpHelper::uuid(); // e.g., "550e8400-e29b-41d4-a716-446655440000"
// Generate slug
$slug = PhpHelper::slug("Hello World!"); // e.g., "hello-world"
// New: Improved DateTime formatting
$formatted = PhpHelper::date()->format("Y-m-d H:i:s", now());
Where to Look First
PhpHelper::method() for direct access.PhpHelper::date() for new DateTime utilities (e.g., diffInHours(), isFuture()).php artisan vendor:publish --provider="Alhames\PhpHelper\PhpHelperServiceProvider" to access updated config stubs.Data Transformation
Use PhpHelper::array() for nested array manipulations (unchanged):
$flattened = PhpHelper::array()->flatten($multiDimensionalArray);
String Manipulation
Leverage PhpHelper::string() for text operations (unchanged):
$titleCase = PhpHelper::string()->titleCase("hello world");
Enhanced Date Handling (New in v1.5.0) Simplify DateTime operations with new methods:
// Format Carbon instances or strings
$formatted = PhpHelper::date()->format("Y-m-d H:i:s", now());
// Calculate time differences
$diffHours = PhpHelper::date()->diffInHours($date1, $date2);
// Check future/past dates
if (PhpHelper::date()->isFuture($futureDate)) {
// Handle future date logic
}
Integration with Laravel
$this->app->singleton('custom.helper', function () {
return new \App\CustomHelper(PhpHelper::make());
});
Blade::directive('uuid', function () {
return "<?php echo Alhames\PhpHelper\Facades\PhpHelper::uuid(); ?>";
});
Blade::directive('formatDate', function ($expression) {
return "<?php echo Alhames\PhpHelper\Facades\PhpHelper::date()->format('Y-m-d', {$expression}); ?>";
});
Usage in Blade:
<div>{{ formatDate(now()) }}</div>
Validation Rules Extend Laravel’s validation with custom rules (unchanged):
$validator = Validator::make($request->all(), [
'expiry_date' => ['required', function ($attribute, $value, $fail) {
if (!PhpHelper::date()->isFuture($value)) {
$fail('The '.$attribute.' must be a future date.');
}
}]
]);
Deprecated Methods (Unchanged) The package was last updated in 2020, but v1.5.0 focuses on DateTime improvements. Test thoroughly in your PHP version.
Namespace Conflicts (Unchanged)
The package uses Alhames\PhpHelper namespace. Use fully qualified aliases:
use Alhames\PhpHelper\Facades\PhpHelper as PhpHelperFacade;
Type Safety (Unchanged) Some methods may not enforce type hints. Add runtime checks:
$result = PhpHelper::date()->format("Y-m-d", $date);
if (!is_string($result)) {
throw new \InvalidArgumentException("Expected string, got ".gettype($result));
}
Configuration Overrides (New in v1.5.0)
The package may now include DateTime-specific config. Override in AppServiceProvider:
public function boot() {
$this->app->singleton('phphelper.config', function () {
return [
'default_timezone' => 'America/New_York',
'date_format' => 'Y-m-d H:i:s',
// Custom DateTime overrides
];
});
}
Inspect Available Methods (Unchanged) Dump the helper’s class methods to explore new DateTime utilities:
dd(get_class_methods(\Alhames\PhpHelper\PhpHelper::class));
Mocking for Testing (Unchanged) Use Laravel’s mocking to isolate the helper in tests:
$this->app->instance(\Alhames\PhpHelper\Facades\PhpHelper::class, Mockery::mock());
Performance Considerations (Unchanged) Avoid heavy operations in loops. Cache DateTime results if needed:
$cachedDiff = Cache::remember("diff_{$date1}_{$date2}", now()->addHours(1), function () use ($date1, $date2) {
return PhpHelper::date()->diffInDays($date1, $date2);
});
Custom Helpers (Unchanged) Extend the base class to add domain-specific methods:
namespace App\Helpers;
use Alhames\PhpHelper\PhpHelper as BaseHelper;
class AppHelper extends BaseHelper {
public function isValidEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
}
Service Provider Binding (Unchanged)
Override the default binding in AppServiceProvider:
public function register() {
$this->app->bind(\Alhames\PhpHelper\Facades\PhpHelper::class, function ($app) {
return new App\Helpers\AppHelper();
});
}
Blade Extensions (Updated for v1.5.0) Register custom Blade helpers for DateTime utilities:
public function boot() {
Blade::extend(function ($view) {
$view->share('phpHelper', PhpHelper::make());
return $view;
});
// New: DateTime-specific Blade helpers
Blade::directive('isFuture', function ($expression) {
return "<?php echo Alhames\PhpHelper\Facades\PhpHelper::date()->isFuture({$expression}) ? 'Yes' : 'No'; ?>";
});
}
Usage in Blade:
<div>{{ isFuture($expiryDate) }}</div>
How can I help you explore Laravel packages today?