PHP code example of nanayawkumi / gh-phone-validator

1. Go to this page and download the library: Download nanayawkumi/gh-phone-validator 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/ */

    

nanayawkumi / gh-phone-validator example snippets


use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
    'phone' => '

use Nanayawkumi\GhPhoneValidator\Rules\GhPhone;

$validator = Validator::make($request->all(), [
    'phone' => ['

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;

GhPhoneValidator::normalize('241234567');        // Returns: '0241234567'
GhPhoneValidator::normalize('+233241234567');    // Returns: '0241234567'
GhPhoneValidator::normalize('233241234567');     // Returns: '0241234567'
GhPhoneValidator::normalize('024-123-4567');     // Returns: '0241234567'
GhPhoneValidator::normalize('024 123 4567');     // Returns: '0241234567'
GhPhoneValidator::normalize('invalid');          // Returns: null

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;
use Nanayawkumi\GhPhoneValidator\Enums\Network;

$network = GhPhoneValidator::network('0241234567');
// Returns: Network::MTN

$network = GhPhoneValidator::network('0201234567');
// Returns: Network::TELECEL

$network = GhPhoneValidator::network('0261234567');
// Returns: Network::AIRTELTIGO

$network = GhPhoneValidator::network('invalid');
// Returns: null

$network = GhPhoneValidator::networkInfo('0241234567');
// Returns: ['name' => 'MTN', 'slug' => 'mtn']

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;

GhPhoneValidator::validate('0241234567');  // Returns: true
GhPhoneValidator::validate('0201234567');  // Returns: true
GhPhoneValidator::validate('9991234567');  // Returns: false (invalid prefix)
GhPhoneValidator::validate('12345');       // Returns: false (invalid length)

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;

GhPhoneValidator::formatRaw('+233241234567');  // Returns: '0241234567'
GhPhoneValidator::formatRaw('024 123 4567');   // Returns: '0241234567'
GhPhoneValidator::formatRaw('invalid');        // Returns: null

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;

GhPhoneValidator::formatNational('0241234567');     // Returns: '024 123 4567'
GhPhoneValidator::formatNational('+233241234567');  // Returns: '024 123 4567'
GhPhoneValidator::formatNational('invalid');         // Returns: null

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;

GhPhoneValidator::formatInternational('0241234567');     // Returns: '+233 24 123 4567'
GhPhoneValidator::formatInternational('+233241234567');  // Returns: '+233 24 123 4567'
GhPhoneValidator::formatInternational('invalid');         // Returns: null

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;

GhPhoneValidator::formatE164('0241234567');     // Returns: '+233241234567'
GhPhoneValidator::formatE164('024 123 4567');   // Returns: '+233241234567'
GhPhoneValidator::formatE164('invalid');        // Returns: null

// Using the cast
$msisdn = $user->phone->e164();

// Or using the validator directly
$msisdn = GhPhoneValidator::formatE164($phone);

use Nanayawkumi\GhPhoneValidator\Casts\GhPhoneCast;

class User extends Model
{
    protected $casts = [
        'phone' => GhPhoneCast::class,
    ];
}

// Direct string access (returns raw format)
echo $user->phone;                // Output: '0241234567'
$phone = $user->phone;            // String: '0241234567'

// Formatting methods
$user->phone->national();         // Returns: '024 123 4567'
$user->phone->international();   // Returns: '+233 24 123 4567'
$user->phone->e164();            // Returns: '+233241234567'
$user->phone->network();         // Returns: Network::MTN (enum)
$user->phone->raw();             // Returns: '0241234567'

// Saving with messy input
$user = new User();
$user->phone = '024 123 4567';  // Accepts various formats
$user->save();                   // Stored as '0241234567' (raw format)

// Retrieving and using
$user = User::find(1);

// Use as string (automatically returns raw format)
echo $user->phone;                    // Output: '0241234567'

// Use formatting methods
echo $user->phone->national();        // Output: '024 123 4567'
echo $user->phone->international();  // Output: '+233 24 123 4567'
echo $user->phone->e164();           // Output: '+233241234567'

use Nanayawkumi\GhPhoneValidator\Casts\GhPhoneCast;

protected $casts = [
    'phone' => GhPhoneCast::class,
];

use Nanayawkumi\GhPhoneValidator\Casts\GhPhoneCast;

protected $casts = [
    'phone' => GhPhoneCast::class . ':e164',
];

// Store as raw (default)
$user = new User();
$user->phone = '024 123 4567';
$user->save();  // Stored as '0241234567'

// Store as E.164 format
use Nanayawkumi\GhPhoneValidator\Casts\GhPhoneCast;

class User extends Model
{
    protected $casts = [
        'phone' => GhPhoneCast::class . ':e164',
    ];
}

$user = new User();
$user->phone = '024 123 4567';
$user->save();  // Stored as '+233241234567'

// Retrieval works the same way regardless of storage format
$user = User::find(1);
echo $user->phone;                // Output: '0241234567' (always raw when used as string)
echo $user->phone->e164();        // Output: '+233241234567'

use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;
use Nanayawkumi\GhPhoneValidator\Enums\Network;

$network = GhPhoneValidator::network('0241234567');

if ($network === Network::MTN) {
    // Do something
}

$network->label(); // MTN
$network->slug();  // mtn

use Nanayawkumi\GhPhoneValidator\Enums\Network;

$user->phone->network(); // Network enum (Network::MTN, Network::TELECEL, etc.)

// Use it in conditionals
if ($user->phone->network() === Network::MTN) {
    // User is on MTN network
}

// Get network label
echo $user->phone->network()?->label(); // MTN

// Get network slug
echo $user->phone->network()?->slug();  // mtn

use Nanayawkumi\GhPhoneValidator\Enums\Network;

$network = Network::fromPhone('0241234567');

if ($network) {
    echo $network->label();        // MTN
    echo $network->slug();        // mtn
    print_r($network->codes());    // ['024', '054', '055', ...]
}

// In config/gh-phone-validator.php
'strict' => true,  // Reject unknown prefixes
'strict' => false, // Accept any valid 10-digit number (default)

// With strict mode enabled
config()->set('gh-phone-validator.strict', true);
GhPhoneValidator::normalize('0291234567');  // Returns: null (unknown prefix)

// With strict mode disabled
config()->set('gh-phone-validator.strict', false);
GhPhoneValidator::normalize('0291234567');  // Returns: '0291234567' (accepted)
bash
php artisan vendor:publish --tag=gh-phone-validator-config