@@ -4,13 +4,13 @@ |
||
4 | 4 | |
5 | 5 | class LaravelPhoneFacade extends Facade |
6 | 6 | { |
7 | - /** |
|
8 | - * Get the registered name of the component. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - protected static function getFacadeAccessor() |
|
13 | - { |
|
14 | - return 'libphonenumber'; |
|
15 | - } |
|
7 | + /** |
|
8 | + * Get the registered name of the component. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + protected static function getFacadeAccessor() |
|
13 | + { |
|
14 | + return 'libphonenumber'; |
|
15 | + } |
|
16 | 16 | } |
@@ -10,246 +10,246 @@ |
||
10 | 10 | class PhoneValidator |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @var \libphonenumber\PhoneNumberUtil |
|
15 | - */ |
|
16 | - protected $lib; |
|
17 | - |
|
18 | - /** |
|
19 | - * Dotted validator data. |
|
20 | - * |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $data; |
|
24 | - |
|
25 | - /** |
|
26 | - * Whether the country should be auto-detected. |
|
27 | - * |
|
28 | - * @var bool |
|
29 | - */ |
|
30 | - protected $autodetect = false; |
|
31 | - |
|
32 | - /** |
|
33 | - * Whether to allow lenient checking of numbers (i.e. landline without area codes). |
|
34 | - * |
|
35 | - * @var bool |
|
36 | - */ |
|
37 | - protected $lenient = false; |
|
38 | - |
|
39 | - /** |
|
40 | - * The field to use for country if not passed as a parameter. |
|
41 | - * |
|
42 | - * @var string|null |
|
43 | - */ |
|
44 | - protected $countryField = null; |
|
45 | - |
|
46 | - /** |
|
47 | - * Countries to validate against. |
|
48 | - * |
|
49 | - * @var array |
|
50 | - */ |
|
51 | - protected $countries = []; |
|
52 | - |
|
53 | - /** |
|
54 | - * Transformed phone number types to validate against. |
|
55 | - * |
|
56 | - * @var array |
|
57 | - */ |
|
58 | - protected $types = []; |
|
59 | - |
|
60 | - /** |
|
61 | - * PhoneValidator constructor. |
|
62 | - */ |
|
63 | - public function __construct(PhoneNumberUtil $lib) |
|
64 | - { |
|
65 | - $this->lib = $lib; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Validates a phone number. |
|
70 | - * |
|
71 | - * @param string $attribute |
|
72 | - * @param mixed $value |
|
73 | - * @param array $parameters |
|
74 | - * @param object $validator |
|
75 | - * @return bool |
|
76 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
77 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
78 | - */ |
|
79 | - public function validatePhone($attribute, $value, array $parameters, $validator) |
|
80 | - { |
|
81 | - $this->data = array_dot($validator->getData()); |
|
82 | - |
|
83 | - $this->assignParameters($parameters); |
|
84 | - |
|
85 | - $this->checkCountries($attribute); |
|
86 | - |
|
87 | - // If autodetecting, let's first try without a country. |
|
88 | - // Otherwise use provided countries as default. |
|
89 | - if ($this->autodetect || ($this->lenient && empty($this->countries))) { |
|
90 | - array_unshift($this->countries, null); |
|
91 | - } |
|
92 | - |
|
93 | - foreach ($this->countries as $country) { |
|
94 | - if ($this->isValidNumber($value, $country)) { |
|
95 | - return true; |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - // All specified country validations have failed. |
|
100 | - return false; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Parses the supplied validator parameters. |
|
105 | - * |
|
106 | - * @param array $parameters |
|
107 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
108 | - */ |
|
109 | - protected function assignParameters(array $parameters) |
|
110 | - { |
|
111 | - $types = []; |
|
112 | - |
|
113 | - foreach ($parameters as $parameter) { |
|
114 | - // First check if the parameter is some phone type configuration. |
|
115 | - if ($this->isPhoneType($parameter)) { |
|
116 | - $types[] = strtoupper($parameter); |
|
117 | - } elseif ($this->isPhoneCountry($parameter)) { |
|
118 | - $this->countries[] = strtoupper($parameter); |
|
119 | - } elseif ($parameter == 'AUTO') { |
|
120 | - $this->autodetect = true; |
|
121 | - } elseif ($parameter == 'LENIENT') { |
|
122 | - $this->lenient = true; |
|
123 | - } // Lastly check if it is an input field containing the country. |
|
124 | - elseif ($this->isInputField($parameter)) { |
|
125 | - $this->countryField = $parameter; |
|
126 | - } else { |
|
127 | - // Force developers to write proper code. |
|
128 | - throw new InvalidParameterException($parameter); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - $this->types = $this->parseTypes($types); |
|
133 | - |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Checks the detected countries. Overrides countries if a country field is present. |
|
138 | - * When using a country field, we should validate to false if country is empty so no exception |
|
139 | - * will be thrown. |
|
140 | - * |
|
141 | - * @param string $attribute |
|
142 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
143 | - */ |
|
144 | - protected function checkCountries($attribute) |
|
145 | - { |
|
146 | - $countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField); |
|
147 | - |
|
148 | - if ($this->isInputField($countryField)) { |
|
149 | - $this->countries = [array_get($this->data, $countryField)]; |
|
150 | - } elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) { |
|
151 | - throw new NoValidCountryFoundException; |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Performs the actual validation of the phone number. |
|
157 | - * |
|
158 | - * @param mixed $number |
|
159 | - * @param null $country |
|
160 | - * @return bool |
|
161 | - */ |
|
162 | - protected function isValidNumber($number, $country = null) |
|
163 | - { |
|
164 | - try { |
|
165 | - // Throws NumberParseException if not parsed correctly against the supplied country. |
|
166 | - // If no country was given, tries to discover the country code from the number itself. |
|
167 | - $phoneNumber = $this->lib->parse($number, $country); |
|
168 | - |
|
169 | - // Check if type is allowed. |
|
170 | - if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) { |
|
171 | - |
|
172 | - // Lenient validation. |
|
173 | - if ($this->lenient) { |
|
174 | - return $this->lib->isPossibleNumber($phoneNumber, $country); |
|
175 | - } |
|
176 | - |
|
177 | - // For automatic detection, the number should have a country code. |
|
178 | - if ($phoneNumber->hasCountryCode()) { |
|
179 | - |
|
180 | - // Automatic detection: |
|
181 | - if ($this->autodetect) { |
|
182 | - // Validate if the international phone number is valid for its contained country. |
|
183 | - return $this->lib->isValidNumber($phoneNumber); |
|
184 | - } |
|
185 | - |
|
186 | - // Validate number against the specified country. |
|
187 | - return $this->lib->isValidNumberForRegion($phoneNumber, $country); |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - } catch (NumberParseException $e) { |
|
192 | - // Proceed to default validation error. |
|
193 | - } |
|
194 | - |
|
195 | - return false; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Parses the supplied phone number types. |
|
200 | - * |
|
201 | - * @param array $types |
|
202 | - * @return array |
|
203 | - */ |
|
204 | - protected function parseTypes(array $types) |
|
205 | - { |
|
206 | - // Transform types to their namespaced class constant. |
|
207 | - array_walk($types, function (&$type) { |
|
208 | - $type = constant('\libphonenumber\PhoneNumberType::' . $type); |
|
209 | - }); |
|
210 | - |
|
211 | - // Add in the unsure number type if applicable. |
|
212 | - if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { |
|
213 | - $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
214 | - } |
|
215 | - |
|
216 | - return $types; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Checks if the given field is an actual input field. |
|
221 | - * |
|
222 | - * @param string $field |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function isInputField($field) |
|
226 | - { |
|
227 | - return ! is_null(array_get($this->data, $field)); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Checks if the supplied string is a valid country code. |
|
232 | - * |
|
233 | - * @param string $country |
|
234 | - * @return bool |
|
235 | - */ |
|
236 | - public function isPhoneCountry($country) |
|
237 | - { |
|
238 | - return ISO3166::isValid(strtoupper($country)); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Checks if the supplied string is a valid phone number type. |
|
243 | - * |
|
244 | - * @param string $type |
|
245 | - * @return bool |
|
246 | - */ |
|
247 | - public function isPhoneType($type) |
|
248 | - { |
|
249 | - // Legacy support. |
|
250 | - $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type); |
|
251 | - |
|
252 | - return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type)); |
|
253 | - } |
|
13 | + /** |
|
14 | + * @var \libphonenumber\PhoneNumberUtil |
|
15 | + */ |
|
16 | + protected $lib; |
|
17 | + |
|
18 | + /** |
|
19 | + * Dotted validator data. |
|
20 | + * |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $data; |
|
24 | + |
|
25 | + /** |
|
26 | + * Whether the country should be auto-detected. |
|
27 | + * |
|
28 | + * @var bool |
|
29 | + */ |
|
30 | + protected $autodetect = false; |
|
31 | + |
|
32 | + /** |
|
33 | + * Whether to allow lenient checking of numbers (i.e. landline without area codes). |
|
34 | + * |
|
35 | + * @var bool |
|
36 | + */ |
|
37 | + protected $lenient = false; |
|
38 | + |
|
39 | + /** |
|
40 | + * The field to use for country if not passed as a parameter. |
|
41 | + * |
|
42 | + * @var string|null |
|
43 | + */ |
|
44 | + protected $countryField = null; |
|
45 | + |
|
46 | + /** |
|
47 | + * Countries to validate against. |
|
48 | + * |
|
49 | + * @var array |
|
50 | + */ |
|
51 | + protected $countries = []; |
|
52 | + |
|
53 | + /** |
|
54 | + * Transformed phone number types to validate against. |
|
55 | + * |
|
56 | + * @var array |
|
57 | + */ |
|
58 | + protected $types = []; |
|
59 | + |
|
60 | + /** |
|
61 | + * PhoneValidator constructor. |
|
62 | + */ |
|
63 | + public function __construct(PhoneNumberUtil $lib) |
|
64 | + { |
|
65 | + $this->lib = $lib; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Validates a phone number. |
|
70 | + * |
|
71 | + * @param string $attribute |
|
72 | + * @param mixed $value |
|
73 | + * @param array $parameters |
|
74 | + * @param object $validator |
|
75 | + * @return bool |
|
76 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
77 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
78 | + */ |
|
79 | + public function validatePhone($attribute, $value, array $parameters, $validator) |
|
80 | + { |
|
81 | + $this->data = array_dot($validator->getData()); |
|
82 | + |
|
83 | + $this->assignParameters($parameters); |
|
84 | + |
|
85 | + $this->checkCountries($attribute); |
|
86 | + |
|
87 | + // If autodetecting, let's first try without a country. |
|
88 | + // Otherwise use provided countries as default. |
|
89 | + if ($this->autodetect || ($this->lenient && empty($this->countries))) { |
|
90 | + array_unshift($this->countries, null); |
|
91 | + } |
|
92 | + |
|
93 | + foreach ($this->countries as $country) { |
|
94 | + if ($this->isValidNumber($value, $country)) { |
|
95 | + return true; |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + // All specified country validations have failed. |
|
100 | + return false; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Parses the supplied validator parameters. |
|
105 | + * |
|
106 | + * @param array $parameters |
|
107 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
108 | + */ |
|
109 | + protected function assignParameters(array $parameters) |
|
110 | + { |
|
111 | + $types = []; |
|
112 | + |
|
113 | + foreach ($parameters as $parameter) { |
|
114 | + // First check if the parameter is some phone type configuration. |
|
115 | + if ($this->isPhoneType($parameter)) { |
|
116 | + $types[] = strtoupper($parameter); |
|
117 | + } elseif ($this->isPhoneCountry($parameter)) { |
|
118 | + $this->countries[] = strtoupper($parameter); |
|
119 | + } elseif ($parameter == 'AUTO') { |
|
120 | + $this->autodetect = true; |
|
121 | + } elseif ($parameter == 'LENIENT') { |
|
122 | + $this->lenient = true; |
|
123 | + } // Lastly check if it is an input field containing the country. |
|
124 | + elseif ($this->isInputField($parameter)) { |
|
125 | + $this->countryField = $parameter; |
|
126 | + } else { |
|
127 | + // Force developers to write proper code. |
|
128 | + throw new InvalidParameterException($parameter); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + $this->types = $this->parseTypes($types); |
|
133 | + |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Checks the detected countries. Overrides countries if a country field is present. |
|
138 | + * When using a country field, we should validate to false if country is empty so no exception |
|
139 | + * will be thrown. |
|
140 | + * |
|
141 | + * @param string $attribute |
|
142 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
143 | + */ |
|
144 | + protected function checkCountries($attribute) |
|
145 | + { |
|
146 | + $countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField); |
|
147 | + |
|
148 | + if ($this->isInputField($countryField)) { |
|
149 | + $this->countries = [array_get($this->data, $countryField)]; |
|
150 | + } elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) { |
|
151 | + throw new NoValidCountryFoundException; |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Performs the actual validation of the phone number. |
|
157 | + * |
|
158 | + * @param mixed $number |
|
159 | + * @param null $country |
|
160 | + * @return bool |
|
161 | + */ |
|
162 | + protected function isValidNumber($number, $country = null) |
|
163 | + { |
|
164 | + try { |
|
165 | + // Throws NumberParseException if not parsed correctly against the supplied country. |
|
166 | + // If no country was given, tries to discover the country code from the number itself. |
|
167 | + $phoneNumber = $this->lib->parse($number, $country); |
|
168 | + |
|
169 | + // Check if type is allowed. |
|
170 | + if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) { |
|
171 | + |
|
172 | + // Lenient validation. |
|
173 | + if ($this->lenient) { |
|
174 | + return $this->lib->isPossibleNumber($phoneNumber, $country); |
|
175 | + } |
|
176 | + |
|
177 | + // For automatic detection, the number should have a country code. |
|
178 | + if ($phoneNumber->hasCountryCode()) { |
|
179 | + |
|
180 | + // Automatic detection: |
|
181 | + if ($this->autodetect) { |
|
182 | + // Validate if the international phone number is valid for its contained country. |
|
183 | + return $this->lib->isValidNumber($phoneNumber); |
|
184 | + } |
|
185 | + |
|
186 | + // Validate number against the specified country. |
|
187 | + return $this->lib->isValidNumberForRegion($phoneNumber, $country); |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + } catch (NumberParseException $e) { |
|
192 | + // Proceed to default validation error. |
|
193 | + } |
|
194 | + |
|
195 | + return false; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Parses the supplied phone number types. |
|
200 | + * |
|
201 | + * @param array $types |
|
202 | + * @return array |
|
203 | + */ |
|
204 | + protected function parseTypes(array $types) |
|
205 | + { |
|
206 | + // Transform types to their namespaced class constant. |
|
207 | + array_walk($types, function (&$type) { |
|
208 | + $type = constant('\libphonenumber\PhoneNumberType::' . $type); |
|
209 | + }); |
|
210 | + |
|
211 | + // Add in the unsure number type if applicable. |
|
212 | + if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { |
|
213 | + $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
214 | + } |
|
215 | + |
|
216 | + return $types; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Checks if the given field is an actual input field. |
|
221 | + * |
|
222 | + * @param string $field |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function isInputField($field) |
|
226 | + { |
|
227 | + return ! is_null(array_get($this->data, $field)); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Checks if the supplied string is a valid country code. |
|
232 | + * |
|
233 | + * @param string $country |
|
234 | + * @return bool |
|
235 | + */ |
|
236 | + public function isPhoneCountry($country) |
|
237 | + { |
|
238 | + return ISO3166::isValid(strtoupper($country)); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Checks if the supplied string is a valid phone number type. |
|
243 | + * |
|
244 | + * @param string $type |
|
245 | + * @return bool |
|
246 | + */ |
|
247 | + public function isPhoneType($type) |
|
248 | + { |
|
249 | + // Legacy support. |
|
250 | + $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type); |
|
251 | + |
|
252 | + return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type)); |
|
253 | + } |
|
254 | 254 | |
255 | 255 | } |
@@ -143,11 +143,11 @@ discard block |
||
143 | 143 | */ |
144 | 144 | protected function checkCountries($attribute) |
145 | 145 | { |
146 | - $countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField); |
|
146 | + $countryField = (is_null($this->countryField) ? $attribute.'_country' : $this->countryField); |
|
147 | 147 | |
148 | 148 | if ($this->isInputField($countryField)) { |
149 | 149 | $this->countries = [array_get($this->data, $countryField)]; |
150 | - } elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) { |
|
150 | + } elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) { |
|
151 | 151 | throw new NoValidCountryFoundException; |
152 | 152 | } |
153 | 153 | } |
@@ -204,8 +204,8 @@ discard block |
||
204 | 204 | protected function parseTypes(array $types) |
205 | 205 | { |
206 | 206 | // Transform types to their namespaced class constant. |
207 | - array_walk($types, function (&$type) { |
|
208 | - $type = constant('\libphonenumber\PhoneNumberType::' . $type); |
|
207 | + array_walk($types, function(&$type) { |
|
208 | + $type = constant('\libphonenumber\PhoneNumberType::'.$type); |
|
209 | 209 | }); |
210 | 210 | |
211 | 211 | // Add in the unsure number type if applicable. |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | */ |
225 | 225 | public function isInputField($field) |
226 | 226 | { |
227 | - return ! is_null(array_get($this->data, $field)); |
|
227 | + return !is_null(array_get($this->data, $field)); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | // Legacy support. |
250 | 250 | $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type); |
251 | 251 | |
252 | - return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type)); |
|
252 | + return defined('\libphonenumber\PhoneNumberType::'.strtoupper($type)); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | use Illuminate\Support\Facades\App; |
4 | 4 | use libphonenumber\PhoneNumberFormat; |
5 | 5 | |
6 | -if (! function_exists('phone')) { |
|
6 | +if (!function_exists('phone')) { |
|
7 | 7 | /** |
8 | 8 | * Get the PhoneNumberUtil or format a phone number for display. |
9 | 9 | * |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | { |
14 | 14 | $lib = App::make('libphonenumber'); |
15 | 15 | |
16 | - if (! $arguments = func_get_args()) { |
|
16 | + if (!$arguments = func_get_args()) { |
|
17 | 17 | return $lib; |
18 | 18 | } |
19 | 19 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | } |
29 | 29 | } |
30 | 30 | |
31 | -if (! function_exists('phone_format')) { |
|
31 | +if (!function_exists('phone_format')) { |
|
32 | 32 | /** |
33 | 33 | * Formats a phone number and country for display. |
34 | 34 | * |
@@ -4,43 +4,43 @@ |
||
4 | 4 | use libphonenumber\PhoneNumberFormat; |
5 | 5 | |
6 | 6 | if (! function_exists('phone')) { |
7 | - /** |
|
8 | - * Get the PhoneNumberUtil or format a phone number for display. |
|
9 | - * |
|
10 | - * @return \libphonenumber\PhoneNumberUtil|string |
|
11 | - */ |
|
12 | - function phone() |
|
13 | - { |
|
14 | - $lib = App::make('libphonenumber'); |
|
7 | + /** |
|
8 | + * Get the PhoneNumberUtil or format a phone number for display. |
|
9 | + * |
|
10 | + * @return \libphonenumber\PhoneNumberUtil|string |
|
11 | + */ |
|
12 | + function phone() |
|
13 | + { |
|
14 | + $lib = App::make('libphonenumber'); |
|
15 | 15 | |
16 | - if (! $arguments = func_get_args()) { |
|
17 | - return $lib; |
|
18 | - } |
|
16 | + if (! $arguments = func_get_args()) { |
|
17 | + return $lib; |
|
18 | + } |
|
19 | 19 | |
20 | - $phone = $arguments[0]; |
|
21 | - $country = isset($arguments[1]) ? $arguments[1] : App::getLocale(); |
|
22 | - $format = isset($arguments[2]) ? $arguments[2] : PhoneNumberFormat::INTERNATIONAL; |
|
20 | + $phone = $arguments[0]; |
|
21 | + $country = isset($arguments[1]) ? $arguments[1] : App::getLocale(); |
|
22 | + $format = isset($arguments[2]) ? $arguments[2] : PhoneNumberFormat::INTERNATIONAL; |
|
23 | 23 | |
24 | - return $lib->format( |
|
25 | - $lib->parse($phone, $country), |
|
26 | - $format |
|
27 | - ); |
|
28 | - } |
|
24 | + return $lib->format( |
|
25 | + $lib->parse($phone, $country), |
|
26 | + $format |
|
27 | + ); |
|
28 | + } |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | if (! function_exists('phone_format')) { |
32 | - /** |
|
33 | - * Formats a phone number and country for display. |
|
34 | - * |
|
35 | - * @param string $phone |
|
36 | - * @param string $country |
|
37 | - * @param int|null $format |
|
38 | - * @return string |
|
39 | - * |
|
40 | - * @deprecated 2.8.0 |
|
41 | - */ |
|
42 | - function phone_format($phone, $country = null, $format = PhoneNumberFormat::INTERNATIONAL) |
|
43 | - { |
|
44 | - return phone($phone, $country, $format); |
|
45 | - } |
|
32 | + /** |
|
33 | + * Formats a phone number and country for display. |
|
34 | + * |
|
35 | + * @param string $phone |
|
36 | + * @param string $country |
|
37 | + * @param int|null $format |
|
38 | + * @return string |
|
39 | + * |
|
40 | + * @deprecated 2.8.0 |
|
41 | + */ |
|
42 | + function phone_format($phone, $country = null, $format = PhoneNumberFormat::INTERNATIONAL) |
|
43 | + { |
|
44 | + return phone($phone, $country, $format); |
|
45 | + } |
|
46 | 46 | } |
@@ -23,7 +23,7 @@ |
||
23 | 23 | */ |
24 | 24 | public function register() |
25 | 25 | { |
26 | - $this->app->singleton('libphonenumber', function ($app) { |
|
26 | + $this->app->singleton('libphonenumber', function($app) { |
|
27 | 27 | return PhoneNumberUtil::getInstance(); |
28 | 28 | }); |
29 | 29 |
@@ -6,41 +6,41 @@ |
||
6 | 6 | |
7 | 7 | class LaravelPhoneServiceProvider extends ServiceProvider |
8 | 8 | { |
9 | - /** |
|
10 | - * Bootstrap the application events. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function boot() |
|
15 | - { |
|
16 | - $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
|
9 | + /** |
|
10 | + * Bootstrap the application events. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function boot() |
|
15 | + { |
|
16 | + $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
|
17 | 17 | |
18 | - $this->app['validator']->{$extend}('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone'); |
|
19 | - } |
|
18 | + $this->app['validator']->{$extend}('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone'); |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * Register the service provider. |
|
23 | - * |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public function register() |
|
27 | - { |
|
28 | - $this->app->singleton('libphonenumber', function ($app) { |
|
29 | - return PhoneNumberUtil::getInstance(); |
|
30 | - }); |
|
21 | + /** |
|
22 | + * Register the service provider. |
|
23 | + * |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public function register() |
|
27 | + { |
|
28 | + $this->app->singleton('libphonenumber', function ($app) { |
|
29 | + return PhoneNumberUtil::getInstance(); |
|
30 | + }); |
|
31 | 31 | |
32 | - $this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil'); |
|
33 | - } |
|
32 | + $this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil'); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Determine whether we can register a dependent validator. |
|
37 | - * |
|
38 | - * @return bool |
|
39 | - */ |
|
40 | - public static function canUseDependentValidation() |
|
41 | - { |
|
42 | - $validator = new ReflectionClass('\Illuminate\Validation\Factory'); |
|
35 | + /** |
|
36 | + * Determine whether we can register a dependent validator. |
|
37 | + * |
|
38 | + * @return bool |
|
39 | + */ |
|
40 | + public static function canUseDependentValidation() |
|
41 | + { |
|
42 | + $validator = new ReflectionClass('\Illuminate\Validation\Factory'); |
|
43 | 43 | |
44 | - return $validator->hasMethod('extendDependent'); |
|
45 | - } |
|
44 | + return $validator->hasMethod('extendDependent'); |
|
45 | + } |
|
46 | 46 | } |
@@ -232,7 +232,7 @@ |
||
232 | 232 | /** |
233 | 233 | * Throw a IndeterminablePhoneCountryException. |
234 | 234 | * |
235 | - * @param $message |
|
235 | + * @param string $message |
|
236 | 236 | * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneCountryException |
237 | 237 | */ |
238 | 238 | protected function throwCountryException($message) |
@@ -16,290 +16,290 @@ |
||
16 | 16 | |
17 | 17 | class Phone implements Jsonable, JsonSerializable, Serializable |
18 | 18 | { |
19 | - use ParsesCountries, |
|
20 | - ParsesFormats, |
|
21 | - ParsesTypes; |
|
22 | - |
|
23 | - /** |
|
24 | - * The provided phone number. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $number; |
|
29 | - |
|
30 | - /** |
|
31 | - * The provided phone countries. |
|
32 | - * |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $countries = []; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var \libphonenumber\PhoneNumberUtil |
|
39 | - */ |
|
40 | - protected $lib; |
|
41 | - |
|
42 | - /** |
|
43 | - * Phone constructor. |
|
44 | - * |
|
45 | - * @param string $number |
|
46 | - */ |
|
47 | - public function __construct($number) |
|
48 | - { |
|
49 | - $this->number = $number; |
|
50 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Create a phone instance. |
|
55 | - * |
|
56 | - * @param string $number |
|
57 | - * @param string|array $country |
|
58 | - * @return static |
|
59 | - */ |
|
60 | - public static function make($number, $country = null) |
|
61 | - { |
|
62 | - $instance = new static($number); |
|
63 | - |
|
64 | - return $instance->ofCountry($country); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Set the country to which the phone number belongs to. |
|
69 | - * |
|
70 | - * @param string|array $country |
|
71 | - * @return $this |
|
72 | - */ |
|
73 | - public function ofCountry($country) |
|
74 | - { |
|
75 | - $instance = clone $this; |
|
76 | - $instance->countries = $instance->parseCountries($country); |
|
77 | - |
|
78 | - return $instance; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Format the phone number in international format. |
|
83 | - * |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function formatInternational() |
|
87 | - { |
|
88 | - return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Format the phone number in national format. |
|
93 | - * |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public function formatNational() |
|
97 | - { |
|
98 | - return $this->format(PhoneNumberFormat::NATIONAL); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Format the phone number in E164 format. |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function formatE164() |
|
107 | - { |
|
108 | - return $this->format(PhoneNumberFormat::E164); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Format the phone number in RFC3966 format. |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function formatRFC3966() |
|
117 | - { |
|
118 | - return $this->format(PhoneNumberFormat::RFC3966); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Format the phone number in a given format. |
|
123 | - * |
|
124 | - * @param string $format |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - public function format($format) |
|
128 | - { |
|
129 | - if (! ($format = static::parseFormat($format))) { |
|
130 | - return $this->throwFormatException('Unknown format "' . (string) $format . '"'); |
|
131 | - } |
|
132 | - |
|
133 | - $country = Arr::get($this->countries, 0); |
|
134 | - |
|
135 | - if (! $country && ! Str::startsWith($this->number, '+')) { |
|
136 | - return $this->throwFormatException('A country should be provided or the number should be in international format'); |
|
137 | - } |
|
138 | - |
|
139 | - return $this->lib->format( |
|
140 | - $this->getPhoneNumberInstance(), |
|
141 | - $format |
|
142 | - ); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Format the phone number in a way that it can be dialled from the provided country. |
|
147 | - * |
|
148 | - * @param string $country |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function formatForCountry($country) |
|
152 | - { |
|
153 | - if (! static::isCountryCode($country)) { |
|
154 | - return $this->throwCountryException($country); |
|
155 | - } |
|
156 | - |
|
157 | - return $this->lib->formatOutOfCountryCallingNumber( |
|
158 | - $this->getPhoneNumberInstance(), |
|
159 | - $country |
|
160 | - ); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
165 | - * |
|
166 | - * @param string $country |
|
167 | - * @param bool $removeFormatting |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
171 | - { |
|
172 | - if (! static::isCountryCode($country)) { |
|
173 | - return $this->throwCountryException($country); |
|
174 | - } |
|
175 | - |
|
176 | - return $this->lib->formatNumberForMobileDialing( |
|
177 | - $this->getPhoneNumberInstance(), |
|
178 | - $country, |
|
179 | - $removeFormatting |
|
180 | - ); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Get the phone number's country. |
|
185 | - * |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function getCountry() |
|
189 | - { |
|
190 | - return $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Get the phone number's type. |
|
195 | - * |
|
196 | - * @param bool $asConstant |
|
197 | - * @return string|int |
|
198 | - */ |
|
199 | - public function getType($asConstant = false) |
|
200 | - { |
|
201 | - $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
202 | - |
|
203 | - return $asConstant ? $type : array_search($type, static::$types); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Get the PhoneNumber instance of the current number. |
|
208 | - * |
|
209 | - * @return \libphonenumber\PhoneNumber |
|
210 | - */ |
|
211 | - public function getPhoneNumberInstance() |
|
212 | - { |
|
213 | - // Let's try each provided country. |
|
214 | - foreach ($this->countries as $country) { |
|
215 | - try { |
|
216 | - return $this->lib->parse($this->number, $country); |
|
217 | - } catch (NumberParseException $exception) { |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - // Otherwise let's try to autodetect the country if the number is in international format. |
|
222 | - if (Str::startsWith($this->number, '+')) { |
|
223 | - try { |
|
224 | - return $this->lib->parse($this->number, null); |
|
225 | - } catch (NumberParseException $exception) { |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - return $this->throwCountryException($this->number); |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Throw a IndeterminablePhoneCountryException. |
|
234 | - * |
|
235 | - * @param $message |
|
236 | - * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneCountryException |
|
237 | - */ |
|
238 | - protected function throwCountryException($message) |
|
239 | - { |
|
240 | - throw new PhoneCountryException($message); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Throw a PhoneFormatException. |
|
245 | - * |
|
246 | - * @param string $message |
|
247 | - * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneFormatException |
|
248 | - */ |
|
249 | - protected function throwFormatException($message) |
|
250 | - { |
|
251 | - throw new PhoneFormatException($message); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Convert the phone instance to JSON. |
|
256 | - * |
|
257 | - * @param int $options |
|
258 | - * @return string |
|
259 | - */ |
|
260 | - public function toJson($options = 0) |
|
261 | - { |
|
262 | - return json_encode($this->jsonSerialize(), $options); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Convert the phone instance into something JSON serializable. |
|
267 | - * |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - public function jsonSerialize() |
|
271 | - { |
|
272 | - return $this->formatE164(); |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Convert the phone instance into a string representation. |
|
277 | - * |
|
278 | - * @return string |
|
279 | - */ |
|
280 | - public function serialize() |
|
281 | - { |
|
282 | - return $this->formatE164(); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * Reconstructs the phone instance from a string representation. |
|
287 | - * |
|
288 | - * @param string $serialized |
|
289 | - */ |
|
290 | - public function unserialize($serialized) |
|
291 | - { |
|
292 | - $this->number = $serialized; |
|
293 | - $this->countries = []; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Convert the phone instance to a formatted number. |
|
298 | - * |
|
299 | - * @return string |
|
300 | - */ |
|
301 | - public function __toString() |
|
302 | - { |
|
303 | - return $this->formatE164(); |
|
304 | - } |
|
19 | + use ParsesCountries, |
|
20 | + ParsesFormats, |
|
21 | + ParsesTypes; |
|
22 | + |
|
23 | + /** |
|
24 | + * The provided phone number. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $number; |
|
29 | + |
|
30 | + /** |
|
31 | + * The provided phone countries. |
|
32 | + * |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $countries = []; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var \libphonenumber\PhoneNumberUtil |
|
39 | + */ |
|
40 | + protected $lib; |
|
41 | + |
|
42 | + /** |
|
43 | + * Phone constructor. |
|
44 | + * |
|
45 | + * @param string $number |
|
46 | + */ |
|
47 | + public function __construct($number) |
|
48 | + { |
|
49 | + $this->number = $number; |
|
50 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Create a phone instance. |
|
55 | + * |
|
56 | + * @param string $number |
|
57 | + * @param string|array $country |
|
58 | + * @return static |
|
59 | + */ |
|
60 | + public static function make($number, $country = null) |
|
61 | + { |
|
62 | + $instance = new static($number); |
|
63 | + |
|
64 | + return $instance->ofCountry($country); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Set the country to which the phone number belongs to. |
|
69 | + * |
|
70 | + * @param string|array $country |
|
71 | + * @return $this |
|
72 | + */ |
|
73 | + public function ofCountry($country) |
|
74 | + { |
|
75 | + $instance = clone $this; |
|
76 | + $instance->countries = $instance->parseCountries($country); |
|
77 | + |
|
78 | + return $instance; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Format the phone number in international format. |
|
83 | + * |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function formatInternational() |
|
87 | + { |
|
88 | + return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Format the phone number in national format. |
|
93 | + * |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public function formatNational() |
|
97 | + { |
|
98 | + return $this->format(PhoneNumberFormat::NATIONAL); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Format the phone number in E164 format. |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function formatE164() |
|
107 | + { |
|
108 | + return $this->format(PhoneNumberFormat::E164); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Format the phone number in RFC3966 format. |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function formatRFC3966() |
|
117 | + { |
|
118 | + return $this->format(PhoneNumberFormat::RFC3966); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Format the phone number in a given format. |
|
123 | + * |
|
124 | + * @param string $format |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + public function format($format) |
|
128 | + { |
|
129 | + if (! ($format = static::parseFormat($format))) { |
|
130 | + return $this->throwFormatException('Unknown format "' . (string) $format . '"'); |
|
131 | + } |
|
132 | + |
|
133 | + $country = Arr::get($this->countries, 0); |
|
134 | + |
|
135 | + if (! $country && ! Str::startsWith($this->number, '+')) { |
|
136 | + return $this->throwFormatException('A country should be provided or the number should be in international format'); |
|
137 | + } |
|
138 | + |
|
139 | + return $this->lib->format( |
|
140 | + $this->getPhoneNumberInstance(), |
|
141 | + $format |
|
142 | + ); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Format the phone number in a way that it can be dialled from the provided country. |
|
147 | + * |
|
148 | + * @param string $country |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function formatForCountry($country) |
|
152 | + { |
|
153 | + if (! static::isCountryCode($country)) { |
|
154 | + return $this->throwCountryException($country); |
|
155 | + } |
|
156 | + |
|
157 | + return $this->lib->formatOutOfCountryCallingNumber( |
|
158 | + $this->getPhoneNumberInstance(), |
|
159 | + $country |
|
160 | + ); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
165 | + * |
|
166 | + * @param string $country |
|
167 | + * @param bool $removeFormatting |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
171 | + { |
|
172 | + if (! static::isCountryCode($country)) { |
|
173 | + return $this->throwCountryException($country); |
|
174 | + } |
|
175 | + |
|
176 | + return $this->lib->formatNumberForMobileDialing( |
|
177 | + $this->getPhoneNumberInstance(), |
|
178 | + $country, |
|
179 | + $removeFormatting |
|
180 | + ); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Get the phone number's country. |
|
185 | + * |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function getCountry() |
|
189 | + { |
|
190 | + return $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Get the phone number's type. |
|
195 | + * |
|
196 | + * @param bool $asConstant |
|
197 | + * @return string|int |
|
198 | + */ |
|
199 | + public function getType($asConstant = false) |
|
200 | + { |
|
201 | + $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
202 | + |
|
203 | + return $asConstant ? $type : array_search($type, static::$types); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Get the PhoneNumber instance of the current number. |
|
208 | + * |
|
209 | + * @return \libphonenumber\PhoneNumber |
|
210 | + */ |
|
211 | + public function getPhoneNumberInstance() |
|
212 | + { |
|
213 | + // Let's try each provided country. |
|
214 | + foreach ($this->countries as $country) { |
|
215 | + try { |
|
216 | + return $this->lib->parse($this->number, $country); |
|
217 | + } catch (NumberParseException $exception) { |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + // Otherwise let's try to autodetect the country if the number is in international format. |
|
222 | + if (Str::startsWith($this->number, '+')) { |
|
223 | + try { |
|
224 | + return $this->lib->parse($this->number, null); |
|
225 | + } catch (NumberParseException $exception) { |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + return $this->throwCountryException($this->number); |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Throw a IndeterminablePhoneCountryException. |
|
234 | + * |
|
235 | + * @param $message |
|
236 | + * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneCountryException |
|
237 | + */ |
|
238 | + protected function throwCountryException($message) |
|
239 | + { |
|
240 | + throw new PhoneCountryException($message); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Throw a PhoneFormatException. |
|
245 | + * |
|
246 | + * @param string $message |
|
247 | + * @throws \Propaganistas\LaravelPhone\Exceptions\PhoneFormatException |
|
248 | + */ |
|
249 | + protected function throwFormatException($message) |
|
250 | + { |
|
251 | + throw new PhoneFormatException($message); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Convert the phone instance to JSON. |
|
256 | + * |
|
257 | + * @param int $options |
|
258 | + * @return string |
|
259 | + */ |
|
260 | + public function toJson($options = 0) |
|
261 | + { |
|
262 | + return json_encode($this->jsonSerialize(), $options); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Convert the phone instance into something JSON serializable. |
|
267 | + * |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + public function jsonSerialize() |
|
271 | + { |
|
272 | + return $this->formatE164(); |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Convert the phone instance into a string representation. |
|
277 | + * |
|
278 | + * @return string |
|
279 | + */ |
|
280 | + public function serialize() |
|
281 | + { |
|
282 | + return $this->formatE164(); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Reconstructs the phone instance from a string representation. |
|
287 | + * |
|
288 | + * @param string $serialized |
|
289 | + */ |
|
290 | + public function unserialize($serialized) |
|
291 | + { |
|
292 | + $this->number = $serialized; |
|
293 | + $this->countries = []; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Convert the phone instance to a formatted number. |
|
298 | + * |
|
299 | + * @return string |
|
300 | + */ |
|
301 | + public function __toString() |
|
302 | + { |
|
303 | + return $this->formatE164(); |
|
304 | + } |
|
305 | 305 | } |
306 | 306 | \ No newline at end of file |
@@ -126,13 +126,13 @@ discard block |
||
126 | 126 | */ |
127 | 127 | public function format($format) |
128 | 128 | { |
129 | - if (! ($format = static::parseFormat($format))) { |
|
130 | - return $this->throwFormatException('Unknown format "' . (string) $format . '"'); |
|
129 | + if (!($format = static::parseFormat($format))) { |
|
130 | + return $this->throwFormatException('Unknown format "'.(string) $format.'"'); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | $country = Arr::get($this->countries, 0); |
134 | 134 | |
135 | - if (! $country && ! Str::startsWith($this->number, '+')) { |
|
135 | + if (!$country && !Str::startsWith($this->number, '+')) { |
|
136 | 136 | return $this->throwFormatException('A country should be provided or the number should be in international format'); |
137 | 137 | } |
138 | 138 | |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | */ |
151 | 151 | public function formatForCountry($country) |
152 | 152 | { |
153 | - if (! static::isCountryCode($country)) { |
|
153 | + if (!static::isCountryCode($country)) { |
|
154 | 154 | return $this->throwCountryException($country); |
155 | 155 | } |
156 | 156 | |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | */ |
170 | 170 | public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
171 | 171 | { |
172 | - if (! static::isCountryCode($country)) { |
|
172 | + if (!static::isCountryCode($country)) { |
|
173 | 173 | return $this->throwCountryException($country); |
174 | 174 | } |
175 | 175 |
@@ -1,3 +1,3 @@ |
||
1 | 1 | <?php namespace Propaganistas\LaravelPhone\Exceptions; |
2 | 2 | |
3 | -class PhoneCountryException extends \Exception{} |
|
3 | +class PhoneCountryException extends \Exception {} |
@@ -4,31 +4,31 @@ |
||
4 | 4 | |
5 | 5 | trait ParsesCountries |
6 | 6 | { |
7 | - /** |
|
8 | - * Determine whether the given country code is valid. |
|
9 | - * |
|
10 | - * @param string $country |
|
11 | - * @return bool |
|
12 | - */ |
|
13 | - public static function isCountryCode($country) |
|
14 | - { |
|
15 | - return ISO3166::isValid($country); |
|
16 | - } |
|
7 | + /** |
|
8 | + * Determine whether the given country code is valid. |
|
9 | + * |
|
10 | + * @param string $country |
|
11 | + * @return bool |
|
12 | + */ |
|
13 | + public static function isCountryCode($country) |
|
14 | + { |
|
15 | + return ISO3166::isValid($country); |
|
16 | + } |
|
17 | 17 | |
18 | - /** |
|
19 | - * Parse the provided phone countries to a valid array. |
|
20 | - * |
|
21 | - * @param string|array $countries |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - protected function parseCountries($countries) |
|
25 | - { |
|
26 | - $countries = is_array($countries) ? $countries : func_get_args(); |
|
18 | + /** |
|
19 | + * Parse the provided phone countries to a valid array. |
|
20 | + * |
|
21 | + * @param string|array $countries |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + protected function parseCountries($countries) |
|
25 | + { |
|
26 | + $countries = is_array($countries) ? $countries : func_get_args(); |
|
27 | 27 | |
28 | - return array_filter($countries, function ($code) { |
|
29 | - $code = strtoupper($code); |
|
28 | + return array_filter($countries, function ($code) { |
|
29 | + $code = strtoupper($code); |
|
30 | 30 | |
31 | - return static::isCountryCode($code) ? $code : null; |
|
32 | - }); |
|
33 | - } |
|
31 | + return static::isCountryCode($code) ? $code : null; |
|
32 | + }); |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -25,7 +25,7 @@ |
||
25 | 25 | { |
26 | 26 | $countries = is_array($countries) ? $countries : func_get_args(); |
27 | 27 | |
28 | - return array_filter($countries, function ($code) { |
|
28 | + return array_filter($countries, function($code) { |
|
29 | 29 | $code = strtoupper($code); |
30 | 30 | |
31 | 31 | return static::isCountryCode($code) ? $code : null; |
@@ -6,80 +6,80 @@ |
||
6 | 6 | |
7 | 7 | trait ParsesTypes |
8 | 8 | { |
9 | - /** |
|
10 | - * Array of available phone formats. |
|
11 | - * |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected static $types; |
|
9 | + /** |
|
10 | + * Array of available phone formats. |
|
11 | + * |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected static $types; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Determine whether the given type is valid. |
|
18 | - * |
|
19 | - * @param string $type |
|
20 | - * @return bool |
|
21 | - */ |
|
22 | - public static function isType($type) |
|
23 | - { |
|
24 | - return ! is_null(static::parseTypes($type)); |
|
25 | - } |
|
16 | + /** |
|
17 | + * Determine whether the given type is valid. |
|
18 | + * |
|
19 | + * @param string $type |
|
20 | + * @return bool |
|
21 | + */ |
|
22 | + public static function isType($type) |
|
23 | + { |
|
24 | + return ! is_null(static::parseTypes($type)); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Parse a phone type. |
|
29 | - * |
|
30 | - * @param string|array $types |
|
31 | - * @return array |
|
32 | - */ |
|
33 | - protected static function parseTypes($types) |
|
34 | - { |
|
35 | - static::loadTypes(); |
|
27 | + /** |
|
28 | + * Parse a phone type. |
|
29 | + * |
|
30 | + * @param string|array $types |
|
31 | + * @return array |
|
32 | + */ |
|
33 | + protected static function parseTypes($types) |
|
34 | + { |
|
35 | + static::loadTypes(); |
|
36 | 36 | |
37 | - $types = is_array($types) ? $types : func_get_args(); |
|
37 | + $types = is_array($types) ? $types : func_get_args(); |
|
38 | 38 | |
39 | - return array_filter($types, function ($type) { |
|
40 | - // If the type equals a constant's value, just return it. |
|
41 | - if (in_array($type, static::$types)) { |
|
42 | - return $type; |
|
43 | - } |
|
39 | + return array_filter($types, function ($type) { |
|
40 | + // If the type equals a constant's value, just return it. |
|
41 | + if (in_array($type, static::$types)) { |
|
42 | + return $type; |
|
43 | + } |
|
44 | 44 | |
45 | - // Otherwise we'll assume the type is the constant's name. |
|
46 | - return Arr::get(static::$types, strtoupper($type)); |
|
47 | - }); |
|
48 | - } |
|
45 | + // Otherwise we'll assume the type is the constant's name. |
|
46 | + return Arr::get(static::$types, strtoupper($type)); |
|
47 | + }); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Parse a phone type into its string representation. |
|
52 | - * |
|
53 | - * @param string|array $types |
|
54 | - * @return array |
|
55 | - */ |
|
56 | - protected static function parseTypesAsStrings($types) |
|
57 | - { |
|
58 | - static::loadTypes(); |
|
50 | + /** |
|
51 | + * Parse a phone type into its string representation. |
|
52 | + * |
|
53 | + * @param string|array $types |
|
54 | + * @return array |
|
55 | + */ |
|
56 | + protected static function parseTypesAsStrings($types) |
|
57 | + { |
|
58 | + static::loadTypes(); |
|
59 | 59 | |
60 | - $types = is_array($types) ? $types : func_get_args(); |
|
60 | + $types = is_array($types) ? $types : func_get_args(); |
|
61 | 61 | |
62 | - return array_filter($types, function ($type) { |
|
63 | - $type = strtoupper($type); |
|
62 | + return array_filter($types, function ($type) { |
|
63 | + $type = strtoupper($type); |
|
64 | 64 | |
65 | - // If the type equals a constant's name, just return it. |
|
66 | - if (isset(static::$types[$type])) { |
|
67 | - return static::$types[$type]; |
|
68 | - } |
|
65 | + // If the type equals a constant's name, just return it. |
|
66 | + if (isset(static::$types[$type])) { |
|
67 | + return static::$types[$type]; |
|
68 | + } |
|
69 | 69 | |
70 | - // Otherwise we'll assume the type is the constant's value. |
|
71 | - return array_search($type, static::$types); |
|
72 | - }); |
|
73 | - } |
|
70 | + // Otherwise we'll assume the type is the constant's value. |
|
71 | + return array_search($type, static::$types); |
|
72 | + }); |
|
73 | + } |
|
74 | 74 | |
75 | 75 | |
76 | - /** |
|
77 | - * Load all available formats once. |
|
78 | - */ |
|
79 | - private static function loadTypes() |
|
80 | - { |
|
81 | - if (! static::$types) { |
|
82 | - static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
|
83 | - } |
|
84 | - } |
|
76 | + /** |
|
77 | + * Load all available formats once. |
|
78 | + */ |
|
79 | + private static function loadTypes() |
|
80 | + { |
|
81 | + if (! static::$types) { |
|
82 | + static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
|
83 | + } |
|
84 | + } |
|
85 | 85 | } |
86 | 86 | \ No newline at end of file |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public static function isType($type) |
23 | 23 | { |
24 | - return ! is_null(static::parseTypes($type)); |
|
24 | + return !is_null(static::parseTypes($type)); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | |
37 | 37 | $types = is_array($types) ? $types : func_get_args(); |
38 | 38 | |
39 | - return array_filter($types, function ($type) { |
|
39 | + return array_filter($types, function($type) { |
|
40 | 40 | // If the type equals a constant's value, just return it. |
41 | 41 | if (in_array($type, static::$types)) { |
42 | 42 | return $type; |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | |
60 | 60 | $types = is_array($types) ? $types : func_get_args(); |
61 | 61 | |
62 | - return array_filter($types, function ($type) { |
|
62 | + return array_filter($types, function($type) { |
|
63 | 63 | $type = strtoupper($type); |
64 | 64 | |
65 | 65 | // If the type equals a constant's name, just return it. |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | */ |
79 | 79 | private static function loadTypes() |
80 | 80 | { |
81 | - if (! static::$types) { |
|
81 | + if (!static::$types) { |
|
82 | 82 | static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants(); |
83 | 83 | } |
84 | 84 | } |
@@ -6,50 +6,50 @@ |
||
6 | 6 | |
7 | 7 | trait ParsesFormats |
8 | 8 | { |
9 | - /** |
|
10 | - * Array of available phone formats. |
|
11 | - * |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected static $formats; |
|
9 | + /** |
|
10 | + * Array of available phone formats. |
|
11 | + * |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected static $formats; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Determine whether the given format is valid. |
|
18 | - * |
|
19 | - * @param string $format |
|
20 | - * @return bool |
|
21 | - */ |
|
22 | - public static function isFormat($format) |
|
23 | - { |
|
24 | - return ! is_null(static::parseFormat($format)); |
|
25 | - } |
|
16 | + /** |
|
17 | + * Determine whether the given format is valid. |
|
18 | + * |
|
19 | + * @param string $format |
|
20 | + * @return bool |
|
21 | + */ |
|
22 | + public static function isFormat($format) |
|
23 | + { |
|
24 | + return ! is_null(static::parseFormat($format)); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Parse a phone format. |
|
29 | - * |
|
30 | - * @param string $format |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - protected static function parseFormat($format) |
|
34 | - { |
|
35 | - static::loadFormats(); |
|
27 | + /** |
|
28 | + * Parse a phone format. |
|
29 | + * |
|
30 | + * @param string $format |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + protected static function parseFormat($format) |
|
34 | + { |
|
35 | + static::loadFormats(); |
|
36 | 36 | |
37 | - // If the format equals a constant's value, just return it. |
|
38 | - if (in_array($format, static::$formats)) { |
|
39 | - return $format; |
|
40 | - } |
|
37 | + // If the format equals a constant's value, just return it. |
|
38 | + if (in_array($format, static::$formats)) { |
|
39 | + return $format; |
|
40 | + } |
|
41 | 41 | |
42 | - // Otherwise we'll assume the format is the constant's name. |
|
43 | - return Arr::get(static::$formats, strtoupper($format)); |
|
44 | - } |
|
42 | + // Otherwise we'll assume the format is the constant's name. |
|
43 | + return Arr::get(static::$formats, strtoupper($format)); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Load all available formats once. |
|
48 | - */ |
|
49 | - private static function loadFormats() |
|
50 | - { |
|
51 | - if (! static::$formats) { |
|
52 | - static::$formats = with(new ReflectionClass(PhoneNumberFormat::class))->getConstants(); |
|
53 | - } |
|
54 | - } |
|
46 | + /** |
|
47 | + * Load all available formats once. |
|
48 | + */ |
|
49 | + private static function loadFormats() |
|
50 | + { |
|
51 | + if (! static::$formats) { |
|
52 | + static::$formats = with(new ReflectionClass(PhoneNumberFormat::class))->getConstants(); |
|
53 | + } |
|
54 | + } |
|
55 | 55 | } |
56 | 56 | \ No newline at end of file |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public static function isFormat($format) |
23 | 23 | { |
24 | - return ! is_null(static::parseFormat($format)); |
|
24 | + return !is_null(static::parseFormat($format)); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | */ |
49 | 49 | private static function loadFormats() |
50 | 50 | { |
51 | - if (! static::$formats) { |
|
51 | + if (!static::$formats) { |
|
52 | 52 | static::$formats = with(new ReflectionClass(PhoneNumberFormat::class))->getConstants(); |
53 | 53 | } |
54 | 54 | } |