Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phphelper Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require alhames/phphelper:^1.5.0
    

    Register the service provider in config/app.php:

    'providers' => [
        // ...
        Alhames\PhpHelper\PhpHelperServiceProvider::class,
    ],
    
  2. 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());
    
  3. Where to Look First

    • Facade: PhpHelper::method() for direct access.
    • Documentation: Check the GitHub repo for v1.5.0 updates.
    • DateTime Helpers: Explore PhpHelper::date() for new DateTime utilities (e.g., diffInHours(), isFuture()).
    • Published Config: Run php artisan vendor:publish --provider="Alhames\PhpHelper\PhpHelperServiceProvider" to access updated config stubs.

Implementation Patterns

Common Workflows

  1. Data Transformation Use PhpHelper::array() for nested array manipulations (unchanged):

    $flattened = PhpHelper::array()->flatten($multiDimensionalArray);
    
  2. String Manipulation Leverage PhpHelper::string() for text operations (unchanged):

    $titleCase = PhpHelper::string()->titleCase("hello world");
    
  3. 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
    }
    
  4. Integration with Laravel

    • Service Container: Bind custom helpers as singletons (unchanged):
      $this->app->singleton('custom.helper', function () {
          return new \App\CustomHelper(PhpHelper::make());
      });
      
    • Blade Directives: Create custom Blade helpers (unchanged):
      Blade::directive('uuid', function () {
          return "<?php echo Alhames\PhpHelper\Facades\PhpHelper::uuid(); ?>";
      });
      
    • New: DateTime Blade Helpers
      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>
      
  5. 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.');
            }
        }]
    ]);
    

Gotchas and Tips

Pitfalls

  1. Deprecated Methods (Unchanged) The package was last updated in 2020, but v1.5.0 focuses on DateTime improvements. Test thoroughly in your PHP version.

  2. Namespace Conflicts (Unchanged) The package uses Alhames\PhpHelper namespace. Use fully qualified aliases:

    use Alhames\PhpHelper\Facades\PhpHelper as PhpHelperFacade;
    
  3. 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));
    }
    
  4. 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
            ];
        });
    }
    

Debugging Tips

  1. Inspect Available Methods (Unchanged) Dump the helper’s class methods to explore new DateTime utilities:

    dd(get_class_methods(\Alhames\PhpHelper\PhpHelper::class));
    
  2. Mocking for Testing (Unchanged) Use Laravel’s mocking to isolate the helper in tests:

    $this->app->instance(\Alhames\PhpHelper\Facades\PhpHelper::class, Mockery::mock());
    
  3. 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);
    });
    

Extension Points

  1. 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;
        }
    }
    
  2. 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();
        });
    }
    
  3. 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>
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel