1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* |
5
|
|
|
* @author giggsey |
6
|
|
|
* @created: 02/10/13 16:52 |
7
|
|
|
* @project libphonenumber-for-php |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace libphonenumber; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use Giggsey\Locale\Locale; |
14
|
|
|
use libphonenumber\prefixmapper\PrefixFileReader; |
15
|
|
|
|
16
|
|
|
class PhoneNumberToCarrierMapper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var PhoneNumberToCarrierMapper[] |
20
|
|
|
*/ |
21
|
|
|
protected static $instance = array(); |
22
|
|
|
|
23
|
|
|
const MAPPING_DATA_DIRECTORY = '/carrier/data/'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PhoneNumberUtil |
27
|
|
|
*/ |
28
|
|
|
protected $phoneUtil; |
29
|
|
|
/** |
30
|
|
|
* @var PrefixFileReader |
31
|
|
|
*/ |
32
|
|
|
protected $prefixFileReader; |
33
|
|
|
|
34
|
2 |
|
protected function __construct($phonePrefixDataDirectory) |
35
|
|
|
{ |
36
|
2 |
|
$this->prefixFileReader = new PrefixFileReader(dirname(__FILE__) . $phonePrefixDataDirectory); |
37
|
2 |
|
$this->phoneUtil = PhoneNumberUtil::getInstance(); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets a {@link PhoneNumberToCarrierMapper} instance to carry out international carrier lookup. |
42
|
|
|
* |
43
|
|
|
* <p> The {@link PhoneNumberToCarrierMapper} is implemented as a singleton. Therefore, calling |
44
|
|
|
* this method multiple times will only result in one instance being created. |
45
|
|
|
* |
46
|
|
|
* @param string $mappingDir |
47
|
|
|
* @return PhoneNumberToCarrierMapper |
48
|
|
|
*/ |
49
|
9 |
|
public static function getInstance($mappingDir = self::MAPPING_DATA_DIRECTORY) |
50
|
|
|
{ |
51
|
9 |
|
if (!array_key_exists($mappingDir, static::$instance)) { |
52
|
2 |
|
static::$instance[$mappingDir] = new static($mappingDir); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
9 |
|
return static::$instance[$mappingDir]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns a carrier name for the given phone number, in the language provided. The carrier name |
60
|
|
|
* is the one the number was originally allocated to, however if the country supports mobile |
61
|
|
|
* number portability the number might not belong to the returned carrier anymore. If no mapping |
62
|
|
|
* is found an empty string is returned. |
63
|
|
|
* |
64
|
|
|
* <p>This method assumes the validity of the number passed in has already been checked, and that |
65
|
|
|
* the number is suitable for carrier lookup. We consider mobile and pager numbers possible |
66
|
|
|
* candidates for carrier lookup. |
67
|
|
|
* |
68
|
|
|
* @param PhoneNumber $number a valid phone number for which we want to get a carrier name |
69
|
|
|
* @param string $languageCode the language code in which the name should be written |
70
|
|
|
* @return string a carrier name for the given phone number |
71
|
|
|
*/ |
72
|
8 |
|
public function getNameForValidNumber(PhoneNumber $number, $languageCode) |
73
|
|
|
{ |
74
|
8 |
|
$languageStr = Locale::getPrimaryLanguage($languageCode); |
75
|
8 |
|
$scriptStr = ""; |
76
|
8 |
|
$regionStr = Locale::getRegion($languageCode); |
77
|
|
|
|
78
|
8 |
|
return $this->prefixFileReader->getDescriptionForNumber($number, $languageStr, $scriptStr, $regionStr); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets the name of the carrier for the given phone number, in the language provided. As per |
84
|
|
|
* {@link #getNameForValidNumber(PhoneNumber, Locale)} but explicitly checks the validity of |
85
|
|
|
* the number passed in. |
86
|
|
|
* |
87
|
|
|
* @param PhoneNumber $number The phone number for which we want to get a carrier name |
88
|
|
|
* @param string $languageCode Language code for which the description should be written |
89
|
|
|
* @return string a carrier name for the given phone number, or empty string if the number passed in is |
90
|
|
|
* invalid |
91
|
|
|
*/ |
92
|
9 |
|
public function getNameForNumber(PhoneNumber $number, $languageCode) |
93
|
|
|
{ |
94
|
9 |
|
$numberType = $this->phoneUtil->getNumberType($number); |
95
|
9 |
|
if ($this->isMobile($numberType)) { |
96
|
6 |
|
return $this->getNameForValidNumber($number, $languageCode); |
97
|
|
|
} |
98
|
3 |
|
return ""; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets the name of the carrier for the given phone number only when it is 'safe' to display to |
103
|
|
|
* users. A carrier name is considered safe if the number is valid and for a region that doesn't |
104
|
|
|
* support |
105
|
|
|
* {@linkplain http://en.wikipedia.org/wiki/Mobile_number_portability mobile number portability}. |
106
|
|
|
* |
107
|
|
|
* @param $number PhoneNumber the phone number for which we want to get a carrier name |
108
|
|
|
* @param $languageCode String the language code in which the name should be written |
109
|
|
|
* @return string a carrier name that is safe to display to users, or the empty string |
110
|
|
|
*/ |
111
|
2 |
|
public function getSafeDisplayName(PhoneNumber $number, $languageCode) |
112
|
|
|
{ |
113
|
2 |
|
if ($this->phoneUtil->isMobileNumberPortableRegion($this->phoneUtil->getRegionCodeForNumber($number))) { |
114
|
1 |
|
return ""; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $this->getNameForNumber($number, $languageCode); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Checks if the supplied number type supports carrier lookup. |
122
|
|
|
* @param int $numberType A PhoneNumberType int |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
9 |
|
protected function isMobile($numberType) |
126
|
|
|
{ |
127
|
9 |
|
return ($numberType === PhoneNumberType::MOBILE || |
128
|
5 |
|
$numberType === PhoneNumberType::FIXED_LINE_OR_MOBILE || |
129
|
|
|
$numberType === PhoneNumberType::PAGER |
130
|
9 |
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|