PHP code example of gordonsarah / tz-phone

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

    

gordonsarah / tz-phone example snippets


use GordonSarah\TzPhone\PhoneNumber;

$phone = PhoneNumber::parse('255 754 123 456');

$phone->isValid();              // true
$phone->getCountryCode();       // 255
$phone->getNetworkOperator();   // Vodacom
$phone->getOperatorId();        // vodacom
$phone->getCompany();           // Vodacom Tanzania PLC
$phone->getMobileMoney();       // M-Pesa
$phone->getPrefix();            // 075
$phone->getNumber();            // 255754123456
$phone->getE164();              // +255754123456
$phone->getLocalNumber();       // 0754123456
$phone->getInternational();     // +255 754 123 456

tz_phone('0712345678')->getMobileMoney(); // Mixx by Yas

$phone = PhoneNumber::parse('0712345678');

$phone->belongsTo('Yas');           // true
$phone->belongsTo('Mixx by Yas');   // true
$phone->belongsTo('Tigo');          // true (former brand)
$phone->belongsTo('vodacom');       // false

use GordonSarah\TzPhone\Laravel\Models\TzPhoneOperator;
use GordonSarah\TzPhone\Laravel\Models\TzPhonePrefix;

$yas = TzPhoneOperator::where('slug', 'yas')->first();

TzPhonePrefix::create([
    'operator_id' => $yas->id,
    'prefix' => '070',
    'status' => 'operational',
    'notes' => 'Added after TCRA gazette',
]);

use GordonSarah\TzPhone\OperatorRegistry;
use GordonSarah\TzPhone\Stores\DatabaseOperatorStore;

$store = DatabaseOperatorStore::sqlite(__DIR__.'/tz-phone.sqlite');
$store->seedFromConfig(OperatorRegistry::defaultConfig());

$registry = OperatorRegistry::instance()->useStore($store);
$registry->addPrefix('vodacom', '070'); // written to SQLite

use GordonSarah\TzPhone\Laravel\Rules\TzPhoneRule;

$request->validate([
    'phone' => ['le::make('vodacom', 'airtel')],
]);

$request->validate([
    'phone' => '_phone:vodacom',
]);

use GordonSarah\TzPhone\Laravel\Facades\TzPhone;

TzPhone::parse('0784123456')->getNetworkOperator(); // Airtel
TzPhone::addPrefix('vodacom', '070');
bash
php artisan vendor:publish --tag=tz-phone-config
php artisan migrate
php artisan tz-phone:seed
bash
php artisan tz-phone:list
php artisan tz-phone:add-operator smile --name=Smile --company="Smile Communications Tanzania Limited"
php artisan tz-phone:add-prefix vodacom 070 --status=operational --notes="New TCRA assignment"
php artisan tz-phone:remove-prefix 062
php artisan tz-phone:seed