Skip to content

Commit

Permalink
Implemented phone number identificator.
Browse files Browse the repository at this point in the history
  • Loading branch information
juanparati authored and Juan Lago committed Mar 23, 2020
1 parent 185c3e8 commit 082e2af
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 24 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ Using the helper method "getAllDefinitions" it will obtain the information about
The definitions include country prefix code, country code and country flag information.


## Identify phone numbers

Using the helper method "identifyNumber" is possible the identify the country which belongs the phone number.
The phone number should contains the international prefix.

Example:

Helper::identifyNumber('+4560514180'); // Returns DK

## Add your country definition

1. Fork this project
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"description": "An international mobile number validator",
"type": "library",
"require": {
"php": ">=7.1",
"haydenpierce/class-finder": "^0.4.0"
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "~6.0"
Expand Down
12 changes: 12 additions & 0 deletions src/Exceptions/ValidatorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Juanparati\MobileNumbers\Exceptions;


/**
* Class ValidatorException.
*
* @package Juanparati\MobileNumbers\Exceptions
*/
class ValidatorException extends \Exception {}
55 changes: 36 additions & 19 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@


use HaydenPierce\ClassFinder\ClassFinder;
use Juanparati\MobileNumbers\Definitions\Contracts\MobileNumbers;
use Juanparati\MobileNumbers\Definitions\MobileNumbersDK;


/**
* Class Helper.
*
* @package Juanparati\MobileNumbers
*/
class Helper
{

/**
* Internal cache used by getAllDefinitions.
*
* @var array|null
*/
protected static $definitions_cache = null;


/**
* Check if phone number has an international prefix code.
*
Expand Down Expand Up @@ -60,6 +60,30 @@ public static function convertToE164($number) : string
}


/**
* Identify a phone number.
*
* @param string $number
* @return string|null
* @throws Exceptions\ValidatorException
*/
public static function identifyNumber(string $number) : ?string
{
$codes = array_keys(Register::getAll());


foreach ($codes as $country_code)
{
$validator = Validator::country($country_code);

if ($validator->hasValidCountryCode($number) && $validator->isValid($number))
return $country_code;
}

return null;
}


/**
* Obtain all the available definitions.
*
Expand All @@ -68,20 +92,13 @@ public static function convertToE164($number) : string
*/
public static function getAllDefinitions() : array
{
// Reflection is very expensive, so it's a good idea to use a static cache.
if (static::$definitions_cache)
return static::$definitions_cache;

$classes = ClassFinder::getClassesInNamespace('Juanparati\MobileNumbers\Definitions');

$classes = array_filter($classes, function ($class) {
return preg_match('/^Juanparati\\\\MobileNumbers\\\\Definitions\\\\MobileNumbers[A-Z][A-Z]$/', $class);
});
$classes = Register::getAll();

static::$definitions_cache = array_map(function ($class) {
/** @var array $definitions */
$definitions = array_map(function (string $class) {
return (new $class)->getDefinition();
}, $classes);

return static::$definitions_cache;
return $definitions;
}
}
91 changes: 91 additions & 0 deletions src/Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php


namespace Juanparati\MobileNumbers;


use Juanparati\MobileNumbers\Definitions\Contracts\MobileNumbers as MobileNumbersContract;


/**
* Class Register.
*
* @package Juanparati\MobileNumbers
*/
class Register
{

/**
* Default MobileNumber classes.
*
* @var array
*/
protected static $defaults =
[
'DK' => \Juanparati\MobileNumbers\Definitions\MobileNumbersDK::class,
'ES' => \Juanparati\MobileNumbers\Definitions\MobileNumbersES::class,
'FI' => \Juanparati\MobileNumbers\Definitions\MobileNumbersFI::class,
'NO' => \Juanparati\MobileNumbers\Definitions\MobileNumbersNO::class,
'PL' => \Juanparati\MobileNumbers\Definitions\MobileNumbersPL::class,
'SE' => \Juanparati\MobileNumbers\Definitions\MobileNumbersSE::class,
];


/**
* Register a new MobileNumber class.
*
* @param string $country_code
* @param MobileNumbersContract $definition
*/
public static function register(string $country_code, MobileNumbersContract $definition) : void
{
static::$defaults[strtoupper($country_code)] = $definition;
}


/**
* Unregister a MobileNumber class.
*
* @param string $country_code
*/
public static function unregister(string $country_code) : void
{
unset(static::$defaults[strtoupper($country_code)]);
}


/**
* Check if MobileNumber class was registered.
*
* @param string $country_code
* @return bool
*/
public static function isDefined(string $country_code) : bool
{
return isset(static::$defaults[strtoupper($country_code)]);
}


/**
* Obtain the MobileNumber class.
*
* @param string $country_code
* @return string|null
*/
public static function get(string $country_code) : ?string
{
return static::isDefined($country_code) ? static::$defaults[strtoupper($country_code)] : null;
}


/**
* Obtain all the registered MobileNumber classes.
*
* @return MobileNumbersContract[]
*/
public static function getAll() : array
{
return static::$defaults;
}

}
14 changes: 11 additions & 3 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Juanparati\MobileNumbers;



use Juanparati\MobileNumbers\Definitions\Contracts\MobileNumbers as MobileNumbersContract;
use Juanparati\MobileNumbers\Exceptions\ValidatorException;

/**
* Validator helper class.
Expand Down Expand Up @@ -31,10 +33,15 @@ class Validator
* Validator constructor.
*
* @param $country_code
* @throws ValidatorException
*/
public function __construct($country_code)
{
$definition_class = 'Juanparati\\MobileNumbers\\Definitions\\MobileNumbers' . strtoupper($country_code);
$definition_class = Register::get($country_code);

if (!$definition_class)
throw new ValidatorException("Class definition for $country_code not found", 0);

$this->definition = new $definition_class();

$this->helper = Helper::class;
Expand All @@ -46,6 +53,7 @@ public function __construct($country_code)
*
* @param $country_code
* @return static
* @throws ValidatorException
*/
public static function country($country_code) : Validator
{
Expand Down Expand Up @@ -103,12 +111,12 @@ public function addCountryCode($number) : string

/**
* Return definition info.
*
*
* @return array
*/
public function getDefinition() : array
{
return $this->definition->getDefinition();
}

}
}
15 changes: 15 additions & 0 deletions tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@ public function test_definitions()
}
}
}


/**
* Test number identificator.
*
* @group Juanparati.mobilenumbers
* @throws \Juanparati\MobileNumbers\Exceptions\ValidatorException
*/
public function test_identify_number()
{
$this->assertEquals('DK', Helper::identifyNumber('+4560514180'));
$this->assertEquals('ES', Helper::identifyNumber('0034717163140'));
$this->assertEquals('FI', Helper::identifyNumber('+358501234567'));
$this->assertNull(Helper::identifyNumber('34717163140'));
}
}

0 comments on commit 082e2af

Please sign in to comment.