PHP code example of gmarsano / chilean-phone-tool

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

    

gmarsano / chilean-phone-tool example snippets


use Gmarsano\ChileanPhoneTool\Phone;

$phone = Phone::parse("+56 9 87-654-321");
$phone->validate();   // true
$phone->number();     // "987654321"
$phone->fullNumber(); // "56987654321"
$phone->prefix()      // "9"
$phone->format();     // "+56 9 87-654-321"

Phone::parse("+56 9 87-654-321")->number();
// => "987654321"

Phone::parse("+56-32-7-654-321")->prefix();
// => "32"

Phone::parse("2 (09) 987654321")->quiet()->get();
// => "209987654321"

// yes work with integer or float, too.
Phone::parse(1.23)->quiet()->number();
// => "123"

Phone::setPhone("56987654321")->quiet()->validate();
// => true

Phone::setPhone("987654321")->quiet()->validate();
// => true

Phone::setPhone("+56 9 87-654-321")->quiet()->validate();
// => false

Phone::parse("+56-32-7-654-321")->validate();
// => true

Phone::parse("+56 1 87-654-321")->validate();
// Exception with message 'Invalid prefix.'

$phone = Phone::parse("+56 1 87-654-321");
$phone->quiet()->validate();
// => false

$phone->errors()
/*
=> [
     5 => "Invalid prefix.",
   ]
*/

$phone->messages()
/*
=> [
     "Invalid prefix.",
   ]
*/

$phone = Phone::parse("+56 1 87-654-321");
$phone->quiet()->validate();
// => false

$phone = Phone::parse("+56 1 87-654-321");
$phone->ignorePrefix()->validate();
// => true

$phone->get()
// => "187654321"

$phone->prefix()
// => "18"

$phone = Phone::setPhone("+56 032 7-654-321");
$phone->quiet()->validate();
// => false

$phone->fix()->validate();
// => true

$phone->get();
// => "327654321"

$phone->getOld();
// => "+56 032 7-654-321"

$phone = Phone::parse("+56-37-654-321");
$phone->quiet()->validate();
// => false

$phone->luckyFix()->validate();
// => true

$phone->format();
// => "+56 2 37-654-321"

$phone->errors();
/*
=> [
     3 => "Invalid phone number format.",
   ]
*/

$phone = Phone::setPhone("987654321");
$phone->quiet()->validate();
// => true

$phone->format();
// => "+56 9 87-654-321"

use Gmarsano\ChileanPhoneTool\Contracts\FormatterInterface;

Phone::setPhone("987654321")->quiet()
  ->format(FormatterInterface::PREFIX_FORMAT);
// => "(9) 87-654-321"

Phone::factory()->make()->first();
// => "451036552"

Phone::factory()->make(5)->all();
/*
=> [
     "523677441",
     "243584227",
     "712584943",
     "671108073",
     "633943870",
   ]
*/

Phone::factory()->cellPhone()->make()->first();
// => "973986533"

Phone::factory()->unique()->landLine()->make(3)->all();
/*
=> [
     "649800571",
     "572435282",
     "805066069",
   ]
*/

Phone::factory()->unique()->prefix(2)->make(3)->all();
/*
=> [
     "249404634",
     "250034633",
     "243960969",
   ]
*/

Phone::factory()->make()->format()->first();
// => "+56 68 4-987-466"

Phone::factory()->unique()->make(3)
  ->format(FormatterInterface::PREFIX_FORMAT)
  ->all();
/*
=> [
     "(9) 76-488-717",
     "(45) 1-806-739",
     "(42) 1-444-163",
   ]
*/

// try this if you quickly need digits with country code
Phone::factory()->make(1, true)->first();
// => "56555182350"