Skip to content

Commit

Permalink
Merge pull request #5 from yondifon/add_camtel_support
Browse files Browse the repository at this point in the history
Camtel Support fixes #2
  • Loading branch information
yondifon authored Jul 22, 2021
2 parents ab0fd48 + e932448 commit 09b4abd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
24 changes: 17 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

Determine mobile telephone operator from user number (Cameroon)

### Installation

### Installation
```composer require malico/mobile-cm-php```
```bash
composer require malico/mobile-cm-php
```

### Usage
### Usage

```php

Expand All @@ -22,15 +24,23 @@ $phone = '00237653956703';
// $phone = '653956703';

echo Network::check($phone);
// nexttel | mtn | orange
// nexttel | mtn | orange | camtel

if (Network::isOrange($phone)) {
echo 'Orange';
}
// Network::isMTN($phone)
// Network::isNexttel($phone)
if (Network::IsNexttel($phone)) {
echo 'Nextel';
}

if (Network::isCamtel($phone)) {
echo 'Camtel';
}


?>
```
Simple. But useful

Simple. But useful

* Camtel numbers are tricky. Not sure. Feel feel free to send in a PR for that.
67 changes: 42 additions & 25 deletions src/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@

/**
* @author Malico <[email protected]>
*
*/

namespace Malico\MobileCM;

/**
* Network
* Network.
*/
class Network
{
/**
* Country Prefix
* Country Prefix.
*/
const PREFIX = '237';

/**
* Operator Prefixes
* Operator Prefixes.
*/
const OPERATOR_PREFIXES = [
'mtn' => [
67, 650, 651, 652, 653, 654, 680, 681, 682, 683
67, 650, 651, 652, 653, 654, 680, 681, 682, 683,
],
'orange' => [
69, 655, 656, 657, 658, 659
69, 655, 656, 657, 658, 659,
],
'nexttel' => [
66
]
66,
],
'camtel' => [
'233', '222', '242', '243',
],
];

/**
* Check if phone is valid length
* Check if phone is valid length.
*
* @param string|int $tel
*
Expand All @@ -42,81 +45,95 @@ protected static function validLength($tel) : bool
{
$len = strlen($tel);

if ($len == 9 || $len == 12 || $len = 13 || $len == 14) {
if ($len == 9 || $len == 12 || $len = 13 || $len == 14) {
return true;
}

return false;
}

/**
* Match Tel to Prefix
* Match Tel to Prefix.
*
* @param string|int $tel
* @param string $key
*
* @return bool
*/
protected static function checkNumber($tel, $key)
protected static function checkNumber(string | int $tel, $key)
{
if (!self::validLength($tel)) {
$tel = \str_replace(' ', '', (string) $tel);

if (! self::validLength($tel)) {
return false;
}

$operator_prefixes = trim(
join("|", self::OPERATOR_PREFIXES[$key]),
"|"
implode('|', self::OPERATOR_PREFIXES[$key]),
'|'
);
$expression = "/^((\+|(0{2}))?". self::PREFIX . ")?((". $operator_prefixes . ")([0-9]{6,7}))$/";

$expression = "/^((\+|(0{2}))?" . self::PREFIX . ')?((' . $operator_prefixes . ')([0-9]{6,7}))$/';

return preg_match($expression, $tel) ? true : false;
}

/**
* Check if Number is Orange
* Check if Number is Orange.
*
* @param string | int $tel
*
* @return bool
*/
public static function isOrange($tel) : bool
public static function isOrange(string | int $tel) : bool
{
return self::checkNumber($tel, 'orange');
}

/**
* Check if Number is MTN
* Check if Number is MTN.
*
* @param string | int $tel
*
* @return bool
*/
public static function isMTN($tel) : bool
public static function isMTN(string | int $tel) : bool
{
return self::checkNumber($tel, 'mtn');
}

/**
* Check if Number is Nexttel
* Check if Number is Nexttel.
*
* @param string | int $tel
*
* @return bool
*/
public static function isNexttel($tel) : bool
public static function isNexttel(string | int $tel) : bool
{
return self::checkNumber($tel, 'nexttel');
}

/**
* Match Number to Operator
* Check if Number is Camtel.
*
* @param string | int $tel
*
* @return bool
*/
public static function isCamtel(string | int $tel) : bool
{
return self::checkNumber($tel, 'camtel');
}

/**
* Match Number to Operator.
*
* @param string | int $tel
*
* @return bool
*/
public static function check($tel)
public static function check($tel) : string | null
{
foreach (self::OPERATOR_PREFIXES as $key => $value) {
if (self::checkNumber($tel, $key)) {
Expand Down

0 comments on commit 09b4abd

Please sign in to comment.