Laravel Myanmar Tools

website·Indexed 31 days ago

https://www.laravel-myanmar-tools.com/

A utility package for Laravel that provides specialized tools for Myanmar-specific data, including Myanmar font Unicode checks, phone number validation, and NRC handling. It extends core Laravel components such as Illuminate\Support\Str, Illuminate\Support\Collection, Illuminate\Database\Eloquent\Builder, and Illuminate\Support\Facades\Validator via custom macros.

Tokens
3.6K
Snippets
28
Records
31
Agent score
60%

What's inside Laravel Myanmar Tools

  1. Overview of Laravel Myanmar Tools

    Laravel Myanmar Tools is a package designed to extend Laravel's core functionality to support Myanmar-specific data and formatting. It adds specialized features for Myanmar Font, Phone Numbers, NRC (National Registration Card), and Dates by extending the following Laravel core components: Str, Eloquent Builder, Query Builder, Request, Collection, and Validator.
  2. Customize laravel-myanmar-tools validation messages

    To modify the default validation messages provided by the package, publish the language files to your application. After publishing, the validation messages can be edited at /lang/vendor/laravelMyanmarTools/en/validation.php.
    php artisan vendor:publish --provider="PyaeSoneAung\LaravelMyanmarTools\LaravelMyanmarToolsServiceProvider"
  3. Validate Myanmar phone numbers using Laravel Validator

    Use the myanmar_phone_number validation rule to ensure a field contains a valid phone number from Myanmar. You can also use carrier-specific rules to validate numbers from MPT, Ooredoo, Telenor, MEC, and Mytel.
    // Generic Myanmar phone validation
    $request->validate([
        'phone' => ['required', 'myanmar_phone_number'],
    ]);
    
    // Carrier-specific validation
    $request->validate(['phone' => ['required', 'mpt']]);     // MPT
    $request->validate(['phone' => ['required', 'ooredoo']]);   // Ooredoo
    $request->validate(['phone' => ['required', 'telenor']]);   // Telenor
    $request->validate(['phone' => ['required', 'mec']]);      // MEC
    $request->validate(['phone' => ['required', 'mytel']]);     // Mytel
  4. Validate Myanmar National Registration Card (NRC) numbers

    Use the nrc validation rule within the Laravel Validator to verify that a field contains a valid Myanmar National Registration Card number.
    $request->validate([
        'nrc' => ['required', 'nrc'],
    ]);
  5. Implement isAtom macro for phone number validation

    Create a custom isAtom macro for the Str helper to identify Atom phone numbers (implemented via the isTelenor check).
    use Illuminate'Support'Str;
    
    public function boot()
    {
        Str::macro('isAtom', function ($phone) {
            return Str::isTelenor($phone);
        });
    }
  6. Implement isMptOrTelenor macro for phone number validation

    Create a custom isMptOrTelenor macro for the Str helper to check if a phone number belongs to either MPT or Telenor providers by combining existing isMpt and isTelenor checks.
    use Illuminate'Support'Str;
    
    public function boot()
    {
        Str::macro('isMptOrTelenor', function ($phone) {
            return Str::isMpt($phone) || Str::isTelenor($phone);
        });
    }
  7. Validate and normalize Myanmar NRC numbers

    The Illuminate\Support\Str class provides utilities to validate Myanmar National Registration Card (NRC) numbers and normalize them between English and Myanmar scripts.
    use Illuminate\Support\Str;
    
    // Validate NRC (supports English and Myanmar scripts)
    Str::isNrc('12/OUKAMA(N)123456'); // true
    Str::isNrc('၁၂/ဥကမ(နိုင်)၁၂၃၄၅၆'); // true
    
    // Normalize to English (default)
    Str::normalizeNrc('၁၂/ဥကမ(နိုင်)၁၂၃၄၅၆'); // '12/OUKAMA(N)123456'
    
    // Normalize to Myanmar script using 'mm' parameter
    Str::normalizeNrc('12/OUKAMA(N)123456', 'mm'); // '၁၂/ဥကမ(နိုင်)၁၂၃၄၅၆'
  8. Validate and identify Myanmar phone numbers

    Use these methods to validate if a string is a Myanmar phone number and to identify the specific telecom operator (MPT, Ooredoo, Telenor, MEC, Mytel) or network type (GSM, WCDMA).
    use Illuminate\Support\Str;
    
    // General validation
    Str::isMyanmarPhoneNumber('09250000000'); // true
    
    // Operator specific validation
    Str::isMpt('09250000000');     // true
    Str::isOoredoo('09970000000'); // true
    Str::isTelenor('09790000000'); // true
    Str::isMec('0930000000');      // true
    Str::isMytel('09690000000');   // true
    
    // Get operator name
    Str::telecomName('09250000000'); // 'mpt'
    
    // Get network type
    Str::telecomNetworkType('09250000000'); // 'gsm'
    Str::telecomNetworkType('09440000000'); // 'wcdma'
  9. Filter Myanmar phone numbers in a Collection

    Use the whereMyanmarPhoneNumber() method to filter a collection, keeping only valid Myanmar phone numbers. It accepts an optional key or dot-notation path to target specific fields in a collection of arrays.
    // Simple array
    $data = ['09250000000', 'abc'];
    collect($data)->whereMyanmarPhoneNumber()->toArray(); // ['09250000000']
    
    // Keyed array
    $data = [['phone' => '09250000000'], ['phone' => 'abc']];
    collect($data)->whereMyanmarPhoneNumber('phone')->toArray();
    
    // Nested dot-notation
    $data = [['user' => ['phone' => '09250000000']], ['user' => ['phone' => 'abc']]];
    collect($data)->whereMyanmarPhoneNumber('user.phone')->toArray();