Skip to content

Commit

Permalink
add mobile and prefix length config
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeta committed Dec 26, 2020
1 parent 9309c9e commit ac9276b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 25 additions & 19 deletions src/GsmDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class GsmDetector

private static $mobileKeyName = "mobile";

private static $telPrefixLength = 2;
private static $mobilePrefixLength = 2;

private static $fixPrefixLength = 3;

/**
* GsmDetector constructor.
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
}
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
16 changes: 14 additions & 2 deletions tests/GsmDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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()
Expand Down

0 comments on commit ac9276b

Please sign in to comment.