diff --git a/.travis.yml b/.travis.yml index 02ad7a8..98371d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,13 +3,14 @@ language: php # Versions of PHP you want your project run with. php: + - 7.2 - 7.3 - 7.4 # Commands to be run before your environment runs. before_script: - composer self-update - - composer install --prefer-source --no-interaction --dev + - composer install # Commands you want to run that will verify your build. script: phpunit diff --git a/src/GsmDetector.php b/src/GsmDetector.php index 8972c44..060ce2b 100644 --- a/src/GsmDetector.php +++ b/src/GsmDetector.php @@ -15,7 +15,9 @@ class GsmDetector private static $mobileKeyName = "mobile"; - private static $telPrefixLength = 2; + private static $mobilePrefixLength = 2; + + private static $fixPrefixLength = 3; /** * GsmDetector constructor. @@ -83,9 +85,7 @@ public function isGsm($name, string $value) $prefix = call_user_func_array('array_merge', $gsmConfig); - $numberPrefix = $this->getNumberPrefix($value); - - return in_array($numberPrefix, $prefix); + return $this->hasValue($prefix, $value); } /** @@ -98,11 +98,9 @@ public function isGsmWithType($gsm, $type, $value) { $gsmConfig = self::$config[$gsm]; - $typePrefix = $gsmConfig[$type]; - - $numberPrefix = $this->getNumberPrefix($value); + $prefix = $gsmConfig[$type]; - return in_array($numberPrefix, $typePrefix); + return $this->hasValue($prefix, $value); } /** @@ -129,15 +127,14 @@ private function gsmHasType($gsm, $type) */ public function isType($value, $type) { - $typeArray = []; + $prefix = []; - $numberPrefix = $this->getNumberPrefix($value); foreach (self::$config as $config) { - $typeArray = array_merge($typeArray, $config[$type]); + $prefix = array_merge($prefix, $config[$type]); } - return in_array($numberPrefix, $typeArray); + return $this->hasValue($prefix, $value); } /** @@ -146,12 +143,10 @@ public function isType($value, $type) */ public function getGsmName($value) { - $numberPrefix = $this->getNumberPrefix($value); - foreach (self::$config as $key => $config) { $prefix = call_user_func_array('array_merge', $config); - if (in_array($numberPrefix, $prefix)) { + if ($this->hasValue($prefix, $value)) { return $key; } } @@ -169,9 +164,14 @@ public static function setConfig(array $config) self::$config = $config; } - public static function setTelPrefixLength(int $length) + public static function setMobilePrefixLength(int $length) + { + self::$mobilePrefixLength = $length; + } + + public static function setFixPrefixLength(int $length) { - self::$telPrefixLength = $length; + self::$fixPrefixLength = $length; } /** @@ -213,8 +213,14 @@ private static function validateConfigFormat($config) * @param $value * @return false|string */ - private function getNumberPrefix($value) + private function getNumberPrefix($value, $prefixLength) + { + return substr($value, 0, $prefixLength); + } + + private function hasValue($prefix, $value) { - return substr($value, 0, self::$telPrefixLength); + return in_array($this->getNumberPrefix($value, self::$mobilePrefixLength), $prefix) || + in_array($this->getNumberPrefix($value, self::$fixPrefixLength), $prefix); } } diff --git a/tests/GsmDetectorTest.php b/tests/GsmDetectorTest.php index 34f94e2..25e80c8 100644 --- a/tests/GsmDetectorTest.php +++ b/tests/GsmDetectorTest.php @@ -28,13 +28,17 @@ public function test_is_gsm() $this->assertTrue($gsmDetector->isGsm('orange', '35000000')); $this->assertFalse($gsmDetector->isGsm('togocel', '26000000')); $this->assertFalse($gsmDetector->isGsm('orange', '29000000')); + + GsmDetector::setFixPrefixLength(3); + + $this->assertFalse($gsmDetector->isGsm('orange', '215000000')); } public function test_is_gsm_with_type() { $gsmDetector = new GsmDetector([ 'orange' => [ - 'fix' => ['22', '35'], + 'fix' => ['22', '35', '215'], 'mobile' => ['88', '87'] ], 'togocel' => [ @@ -49,13 +53,17 @@ public function test_is_gsm_with_type() $this->assertFalse($gsmDetector->isGsmWithType('orange', 'mobile', '35000000')); $this->assertFalse($gsmDetector->isGsmWithType('togocel', 'fix', '01000000')); $this->assertTrue($gsmDetector->isGsmWithType('orange','fix', '35000000')); + + GsmDetector::setFixPrefixLength(3); + + $this->assertTrue($gsmDetector->isGsmWithType('orange','fix', '21500000')); } public function test_is_gsm_name() { $gsmDetector = new GsmDetector([ 'orange' => [ - 'fix' => ['22', '35'], + 'fix' => ['22', '35', '215'], 'mobile' => ['09', '88'] ], 'togocel' => [ @@ -72,6 +80,10 @@ public function test_is_gsm_name() $this->assertTrue($gsmDetector->isOrange('88000000')); $this->assertTrue($gsmDetector->isOrange('09000000')); $this->assertTrue($gsmDetector->isOrange('22000000')); + + GsmDetector::setFixPrefixLength(3); + + $this->assertTrue($gsmDetector->isOrange('21500000')); } public function test_is_gsm_name_with_type()