Weave Code
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

Algerian Mobile Phone Number Laravel Laravel Package

cherif/algerian-mobile-phone-number-laravel

Laravel integration for the cherif/algerian-mobile-phone-number value object. Provides an Eloquent cast to store/read Algerian mobile numbers as a value object, plus a validation rule for request input. Install via Composer and use in models/forms.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require cherif/algerian-mobile-phone-number-laravel
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="CherifGsoul\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumberServiceProvider"
    
  2. Basic Usage: Validate a phone number in a Laravel request:

    use CherifGsoul\AlgerianMobilePhoneNumber\Rules\AlgerianMobilePhoneNumber;
    
    $request->validate([
        'phone' => ['required', new AlgerianMobilePhoneNumber],
    ]);
    
  3. First Use Case:

    • Form Validation: Use the AlgerianMobilePhoneNumber rule in a FormRequest or manually in a controller.
    • Quick Check: Validate a raw string:
      $isValid = (new AlgerianMobilePhoneNumber)->passes('0612345678');
      

Implementation Patterns

Common Workflows

  1. Request Validation:

    // In a FormRequest
    public function rules()
    {
        return [
            'phone' => ['required', 'string', new AlgerianMobilePhoneNumber],
        ];
    }
    
  2. Custom Logic Integration:

    • Format Phone Numbers:
      $formatted = (new \CherifGsoul\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumber())->format('0612345678');
      // Returns: "+213612345678"
      
    • Extract Carrier:
      $carrier = (new \CherifGsoul\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumber())->getCarrier('0612345678');
      // Returns: "Djezzy" (or null if unsupported)
      
  3. Database Storage:

    • Store validated/normalized numbers in a consistent format (e.g., +2136XXXXXXXX):
      $normalized = (new \CherifGsoul\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumber())->normalize('0612345678');
      $user->phone = $normalized;
      $user->save();
      
  4. API Responses:

    • Return formatted/validated data in API responses:
      return response()->json([
          'phone' => $this->formatPhone($request->phone),
      ]);
      

Integration Tips

  • Laravel Scout: Use the normalized phone number as a searchable field if building a contact system.
  • Laravel Notifications: Validate phone numbers before sending SMS notifications.
  • Middleware: Create middleware to validate phone numbers in routes requiring Algerian contacts:
    public function handle($request, Closure $next)
    {
        $request->validate(['phone' => new AlgerianMobilePhoneNumber]);
        return $next($request);
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • The package hasn’t been updated since 2020. Test thoroughly, especially if using Laravel 10+.
    • Underlying cherif/algerian-mobile-number may have unaddressed bugs (check its GitHub issues).
  2. Carrier Detection:

    • Carrier detection (getCarrier()) may return null for newer or less common operators. Handle gracefully:
      $carrier = $validator->getCarrier($phone) ?? 'Unknown';
      
  3. Format Inconsistencies:

    • The package expects raw Algerian numbers (e.g., 0612345678). Ensure user input is stripped of:
      • Whitespace: " 061 234 5678"0612345678
      • Non-digit characters: "06+123-45678"0612345678
      • Prefixes: "+213612345678"0612345678 (use normalize() to reverse this).
  4. Edge Cases:

    • Short Numbers: Rejects numbers shorter than 9 digits (e.g., 06123).
    • Invalid Prefixes: 05 (landline) or 07 (old mobile) may not be fully supported.

Debugging

  • Validation Errors:

    • The rule throws ValidationException with default Laravel messages. Customize messages in your resources/lang:
      'custom' => [
          'phone' => [
              'algerian_mobile_phone_number' => 'The :attribute must be a valid Algerian mobile number (e.g., 0612345678).',
          ],
      ],
      
  • Log Raw Input:

    \Log::debug('Raw phone input:', ['phone' => $request->phone]);
    

Extension Points

  1. Custom Rules: Extend the base rule for project-specific logic:

    use CherifGsoul\AlgerianMobilePhoneNumber\Rules\AlgerianMobilePhoneNumber as BaseRule;
    
    class ProjectSpecificPhoneRule extends BaseRule {
        public function passes($attribute, $value) {
            $isValid = parent::passes($attribute, $value);
            // Add custom checks (e.g., blacklist numbers)
            return $isValid && !$this->isBlacklisted($value);
        }
    }
    
  2. Carrier Database:

    • The carrier mapping is hardcoded. Fork the package to update Carrier::CARRIERS if new operators emerge.
  3. Testing:

    • Write feature tests for validation:
      public function test_algerian_phone_validation()
      {
          $this->assertTrue((new AlgerianMobilePhoneNumber)->passes('0612345678'));
          $this->assertFalse((new AlgerianMobilePhoneNumber)->passes('051234567')); // Too short
      }
      

Configuration Quirks

  • No Config File: The package doesn’t publish a config file by default. If you need to tweak behavior (e.g., allowed prefixes), you’ll need to modify the service provider or fork the package.

  • Locale-Specific Rules: Assumes Algerian numbers only. If your app supports multiple countries, combine with other validation libraries (e.g., giggsey/libphonenumber-for-php).

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.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium