PHP code example of moonlydays / laravel-mno

1. Go to this page and download the library: Download moonlydays/laravel-mno library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moonlydays / laravel-mno example snippets


use MoonlyDays\MNO\Values\Msisdn;

// Parse, throwing InvalidMsisdnException on failure
$phone = Msisdn::from('+79101234567');
$phone = Msisdn::from('9101234567', 'RU');
$phone = Msisdn::from(79101234567, 'RU'); // integers are accepted

// Safe parse, returning null on failure
$phone = Msisdn::tryFrom('invalid'); // null

// Global helper
$phone = msisdn('+79101234567');

$phone = Msisdn::from('+79101234567');

$phone->e164();          // "+79101234567"
$phone->national();      // "8 (910) 123-45-67"
$phone->international(); // "+7 910 123-45-67"
$phone->toInteger();     // 79101234567  (E.164 digits without the leading plus)
(string) $phone;         // "+79101234567"

$phone = Msisdn::from('+79101234567');

$phone->countryCode();      // 7
$phone->countryIso();       // "RU"
$phone->nationalNumber();   // "9101234567"
$phone->networkCode();      // "910"
$phone->subscriberNumber(); // "1234567"
$phone->toPhoneNumber();    // underlying libphonenumber\PhoneNumber

$phone = Msisdn::from('+79101234567');

$phone->timezone();  // "Europe/Moscow" — primary IANA identifier, or null if unknown
$phone->timezones(); // ["Europe/Moscow", ...] — all IANA identifiers for the number

use Illuminate\Validation\Rule;

// Use the Rule::msisdn() macro — picks up defaults from config
$request->validate([
    'phone' => ['

use MoonlyDays\MNO\Rules\MsisdnRule;

// Customize the rule fluently
$request->validate([
    'phone' => [
        '     ->minLength(10)
            ->maxLength(10),
    ],
]);

use MoonlyDays\MNO\Rules\MsisdnRule;

MsisdnRule::defaults(fn () => (new MsisdnRule())
    ->country('RU')
    ->minLength(10)
    ->maxLength(10));

$phone = $request->msisdn('phone');           // Msisdn or null
$phone = $request->msisdn('phone', $default); // with fallback (value or closure)

use Illuminate\Database\Eloquent\Model;
use MoonlyDays\MNO\Values\Msisdn;

class User extends Model
{
    protected $casts = [
        'phone' => Msisdn::class, // or MsisdnCast::class
    ];
}

$user->phone = '+79101234567';
$user->save(); // Stored as unsigned bigInteger: 79101234567

$user->phone instanceof Msisdn; // true
$user->phone->national();            // "8 (910) 123-45-67"

$faker = fake();

$faker->msisdn();             // Msisdn value object
$faker->msisdn()->e164();     // "+79101234567"
$faker->msisdn()->national(); // "8 (910) 123-45-67"

use MoonlyDays\MNO\Resources\MsisdnFormatResource;

return [
    'format' => MsisdnFormatResource::make(),
];
// {
//   "countryCode": 7,
//   "country": "RU",
//   "minLength": 10,
//   "maxLength": 10,
//   "networkCodes": ["910", "911", "912"]
// }

use MoonlyDays\MNO\Facades\MNO;

MNO::countryIsoCode(); // "RU"
MNO::country();        // Country instance for "RU"
MNO::countryCode();    // 7
MNO::carrierName();    // "MTS"
MNO::carrier();        // Carrier instance for the configured MNO
MNO::networkCodes();   // ["910", "911", "912"]
MNO::minLength();      // 10
MNO::maxLength();      // 10
MNO::exampleNumber();  // Msisdn|null
MNO::numberTypes();    // array<NumberType>

use MoonlyDays\MNO\Values\Country;
use MoonlyDays\MNO\Values\Carrier;

// Country — wraps an ISO 3166-1 alpha-2 code
$country = Country::from('RU');       // throws InvalidCountryException on unknown code
$country = Country::tryFrom('RU');    // returns null on failure

$country->isoCode();                // "RU"
$country->countryCode();            // 7
$country->name();                   // "Russia"
$country->exampleNumber();          // Msisdn|null
$country->isMobileNumberPortable(); // bool
$country->carriers();               // array<string, Carrier> — all carriers with allocations

// Carrier — a carrier within a country
$carrier = Carrier::from('RU', 'MTS');    // throws InvalidCarrierException on miss
$carrier = Carrier::tryFrom('RU', 'MTS'); // returns null on failure

$carrier->name();             // "MTS"
$carrier->country();          // Country instance
$carrier->networkCodes();     // ["910", "911", "912"] — NDC prefixes
$carrier->prefixes();         // ["7910", "7911", "7912"] — with country code
$carrier->matches($phone);    // true if the phone number belongs to this carrier
$carrier->owns('910');        // true if the carrier owns this NDC

use MoonlyDays\MNO\Values\Msisdn;

Msisdn::macro('isRussian', function (): bool {
    /** @var Msisdn $this */
    return $this->countryIso() === 'RU';
});

Msisdn::from('+79101234567')->isRussian(); // true
bash
php artisan vendor:publish --tag="mno-config"