@@ -2,14 +2,14 @@ |
||
2 | 2 | |
3 | 3 | class CountryCodeException extends \Exception |
4 | 4 | { |
5 | - /** |
|
6 | - * Invalid country code static constructor. |
|
7 | - * |
|
8 | - * @param string $country |
|
9 | - * @return static |
|
10 | - */ |
|
11 | - public static function invalid($country) |
|
12 | - { |
|
13 | - return new static('Invalid country code "' . $country . '".'); |
|
14 | - } |
|
5 | + /** |
|
6 | + * Invalid country code static constructor. |
|
7 | + * |
|
8 | + * @param string $country |
|
9 | + * @return static |
|
10 | + */ |
|
11 | + public static function invalid($country) |
|
12 | + { |
|
13 | + return new static('Invalid country code "' . $country . '".'); |
|
14 | + } |
|
15 | 15 | } |
@@ -10,6 +10,6 @@ |
||
10 | 10 | */ |
11 | 11 | public static function invalid($country) |
12 | 12 | { |
13 | - return new static('Invalid country code "' . $country . '".'); |
|
13 | + return new static('Invalid country code "'.$country.'".'); |
|
14 | 14 | } |
15 | 15 | } |
@@ -2,14 +2,14 @@ |
||
2 | 2 | |
3 | 3 | class NumberFormatException extends \Exception |
4 | 4 | { |
5 | - /** |
|
6 | - * Invalid number format static constructor. |
|
7 | - * |
|
8 | - * @param string $format |
|
9 | - * @return static |
|
10 | - */ |
|
11 | - public static function invalid($format) |
|
12 | - { |
|
13 | - return new static('Invalid number format "' . $format . '".'); |
|
14 | - } |
|
5 | + /** |
|
6 | + * Invalid number format static constructor. |
|
7 | + * |
|
8 | + * @param string $format |
|
9 | + * @return static |
|
10 | + */ |
|
11 | + public static function invalid($format) |
|
12 | + { |
|
13 | + return new static('Invalid number format "' . $format . '".'); |
|
14 | + } |
|
15 | 15 | } |
16 | 16 | \ No newline at end of file |
@@ -10,6 +10,6 @@ |
||
10 | 10 | */ |
11 | 11 | public static function invalid($format) |
12 | 12 | { |
13 | - return new static('Invalid number format "' . $format . '".'); |
|
13 | + return new static('Invalid number format "'.$format.'".'); |
|
14 | 14 | } |
15 | 15 | } |
16 | 16 | \ No newline at end of file |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | use Propaganistas\LaravelPhone\PhoneNumber; |
4 | 4 | |
5 | -if (! function_exists('phone')) { |
|
5 | +if (!function_exists('phone')) { |
|
6 | 6 | /** |
7 | 7 | * Get a PhoneNumber instance or a formatted string. |
8 | 8 | * |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | { |
16 | 16 | $phone = PhoneNumber::make($number, $country); |
17 | 17 | |
18 | - if (! is_null($format)) { |
|
18 | + if (!is_null($format)) { |
|
19 | 19 | return $phone->format($format); |
20 | 20 | } |
21 | 21 |
@@ -3,22 +3,22 @@ |
||
3 | 3 | use Propaganistas\LaravelPhone\PhoneNumber; |
4 | 4 | |
5 | 5 | if (! function_exists('phone')) { |
6 | - /** |
|
7 | - * Get a PhoneNumber instance or a formatted string. |
|
8 | - * |
|
9 | - * @param string $number |
|
10 | - * @param string|array $country |
|
11 | - * @param string $format |
|
12 | - * @return string|Propaganistas\LaravelPhone\PhoneNumber |
|
13 | - */ |
|
14 | - function phone($number, $country = [], $format = null) |
|
15 | - { |
|
16 | - $phone = PhoneNumber::make($number, $country); |
|
6 | + /** |
|
7 | + * Get a PhoneNumber instance or a formatted string. |
|
8 | + * |
|
9 | + * @param string $number |
|
10 | + * @param string|array $country |
|
11 | + * @param string $format |
|
12 | + * @return string|Propaganistas\LaravelPhone\PhoneNumber |
|
13 | + */ |
|
14 | + function phone($number, $country = [], $format = null) |
|
15 | + { |
|
16 | + $phone = PhoneNumber::make($number, $country); |
|
17 | 17 | |
18 | - if (! is_null($format)) { |
|
19 | - return $phone->format($format); |
|
20 | - } |
|
18 | + if (! is_null($format)) { |
|
19 | + return $phone->format($format); |
|
20 | + } |
|
21 | 21 | |
22 | - return $phone; |
|
23 | - } |
|
22 | + return $phone; |
|
23 | + } |
|
24 | 24 | } |
@@ -10,63 +10,63 @@ |
||
10 | 10 | |
11 | 11 | class PhoneServiceProvider extends ServiceProvider |
12 | 12 | { |
13 | - /** |
|
14 | - * Bootstrap the application events. |
|
15 | - * |
|
16 | - * @return void |
|
17 | - */ |
|
18 | - public function boot() |
|
19 | - { |
|
20 | - $this->registerValidator(); |
|
13 | + /** |
|
14 | + * Bootstrap the application events. |
|
15 | + * |
|
16 | + * @return void |
|
17 | + */ |
|
18 | + public function boot() |
|
19 | + { |
|
20 | + $this->registerValidator(); |
|
21 | 21 | |
22 | - $this->registerRule(); |
|
23 | - } |
|
22 | + $this->registerRule(); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Register the service provider. |
|
27 | - * |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - public function register() |
|
31 | - { |
|
32 | - $this->app->singleton('libphonenumber', function ($app) { |
|
33 | - return PhoneNumberUtil::getInstance(); |
|
34 | - }); |
|
25 | + /** |
|
26 | + * Register the service provider. |
|
27 | + * |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + public function register() |
|
31 | + { |
|
32 | + $this->app->singleton('libphonenumber', function ($app) { |
|
33 | + return PhoneNumberUtil::getInstance(); |
|
34 | + }); |
|
35 | 35 | |
36 | - $this->app->alias('libphonenumber', PhoneNumberUtil::class); |
|
37 | - } |
|
36 | + $this->app->alias('libphonenumber', PhoneNumberUtil::class); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Register the "phone" validator. |
|
41 | - */ |
|
42 | - protected function registerValidator() |
|
43 | - { |
|
44 | - $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
|
39 | + /** |
|
40 | + * Register the "phone" validator. |
|
41 | + */ |
|
42 | + protected function registerValidator() |
|
43 | + { |
|
44 | + $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
|
45 | 45 | |
46 | - $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate'); |
|
47 | - } |
|
46 | + $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate'); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Register the "phone" rule macro. |
|
51 | - */ |
|
52 | - protected function registerRule() |
|
53 | - { |
|
54 | - if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) { |
|
55 | - Rule::macro('phone', function () { |
|
56 | - return new Rules\Phone; |
|
57 | - }); |
|
58 | - } |
|
59 | - } |
|
49 | + /** |
|
50 | + * Register the "phone" rule macro. |
|
51 | + */ |
|
52 | + protected function registerRule() |
|
53 | + { |
|
54 | + if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) { |
|
55 | + Rule::macro('phone', function () { |
|
56 | + return new Rules\Phone; |
|
57 | + }); |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Determine whether we can register a dependent validator. |
|
63 | - * |
|
64 | - * @return bool |
|
65 | - */ |
|
66 | - public static function canUseDependentValidation() |
|
67 | - { |
|
68 | - $validator = new ReflectionClass('\Illuminate\Validation\Factory'); |
|
61 | + /** |
|
62 | + * Determine whether we can register a dependent validator. |
|
63 | + * |
|
64 | + * @return bool |
|
65 | + */ |
|
66 | + public static function canUseDependentValidation() |
|
67 | + { |
|
68 | + $validator = new ReflectionClass('\Illuminate\Validation\Factory'); |
|
69 | 69 | |
70 | - return $validator->hasMethod('extendDependent'); |
|
71 | - } |
|
70 | + return $validator->hasMethod('extendDependent'); |
|
71 | + } |
|
72 | 72 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function register() |
31 | 31 | { |
32 | - $this->app->singleton('libphonenumber', function ($app) { |
|
32 | + $this->app->singleton('libphonenumber', function($app) { |
|
33 | 33 | return PhoneNumberUtil::getInstance(); |
34 | 34 | }); |
35 | 35 | |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | { |
44 | 44 | $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend'; |
45 | 45 | |
46 | - $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate'); |
|
46 | + $this->app['validator']->{$extend}('phone', Validation\Phone::class.'@validate'); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | protected function registerRule() |
53 | 53 | { |
54 | 54 | if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) { |
55 | - Rule::macro('phone', function () { |
|
55 | + Rule::macro('phone', function() { |
|
56 | 56 | return new Rules\Phone; |
57 | 57 | }); |
58 | 58 | } |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | */ |
169 | 169 | public function formatForCountry($country) |
170 | 170 | { |
171 | - if (! static::isValidCountryCode($country)) { |
|
171 | + if (!static::isValidCountryCode($country)) { |
|
172 | 172 | throw CountryCodeException::invalid($country); |
173 | 173 | } |
174 | 174 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | */ |
189 | 189 | public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
190 | 190 | { |
191 | - if (! static::isValidCountryCode($country)) { |
|
191 | + if (!static::isValidCountryCode($country)) { |
|
192 | 192 | throw CountryCodeException::invalid($country); |
193 | 193 | } |
194 | 194 | |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | */ |
207 | 207 | public function getCountry() |
208 | 208 | { |
209 | - if (! $this->country) { |
|
209 | + if (!$this->country) { |
|
210 | 210 | $this->country = $this->filterValidCountry($this->countries); |
211 | 211 | } |
212 | 212 | |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | protected function filterValidCountry($countries) |
224 | 224 | { |
225 | 225 | $result = Collection::make($countries) |
226 | - ->filter(function ($country) { |
|
226 | + ->filter(function($country) { |
|
227 | 227 | $instance = $this->lib->parse($this->number, $country); |
228 | 228 | |
229 | 229 | return $this->lenient |
@@ -19,382 +19,382 @@ |
||
19 | 19 | |
20 | 20 | class PhoneNumber implements Jsonable, JsonSerializable, Serializable |
21 | 21 | { |
22 | - use ParsesCountries, |
|
23 | - ParsesFormats, |
|
24 | - ParsesTypes; |
|
25 | - |
|
26 | - /** |
|
27 | - * The provided phone number. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - protected $number; |
|
32 | - |
|
33 | - /** |
|
34 | - * The provided phone country. |
|
35 | - * |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $countries = []; |
|
39 | - |
|
40 | - /** |
|
41 | - * The detected phone country. |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $country; |
|
46 | - |
|
47 | - /** |
|
48 | - * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
49 | - * |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - protected $lenient = false; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var \libphonenumber\PhoneNumberUtil |
|
56 | - */ |
|
57 | - protected $lib; |
|
58 | - |
|
59 | - /** |
|
60 | - * Phone constructor. |
|
61 | - * |
|
62 | - * @param string $number |
|
63 | - */ |
|
64 | - public function __construct($number) |
|
65 | - { |
|
66 | - $this->number = $number; |
|
67 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Create a phone instance. |
|
72 | - * |
|
73 | - * @param string $number |
|
74 | - * @param string|array $country |
|
75 | - * @return static |
|
76 | - */ |
|
77 | - public static function make($number, $country = []) |
|
78 | - { |
|
79 | - $instance = new static($number); |
|
80 | - |
|
81 | - return $instance->ofCountry($country); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Set the country to which the phone number belongs to. |
|
86 | - * |
|
87 | - * @param string|array $country |
|
88 | - * @return static |
|
89 | - */ |
|
90 | - public function ofCountry($country) |
|
91 | - { |
|
92 | - $countries = is_array($country) ? $country : func_get_args(); |
|
93 | - |
|
94 | - $instance = clone $this; |
|
95 | - $instance->countries = array_unique( |
|
96 | - array_merge($instance->countries, static::parseCountries($countries)) |
|
97 | - ); |
|
98 | - |
|
99 | - return $instance; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Format the phone number in international format. |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function formatInternational() |
|
108 | - { |
|
109 | - return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Format the phone number in national format. |
|
114 | - * |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function formatNational() |
|
118 | - { |
|
119 | - return $this->format(PhoneNumberFormat::NATIONAL); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Format the phone number in E164 format. |
|
124 | - * |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - public function formatE164() |
|
128 | - { |
|
129 | - return $this->format(PhoneNumberFormat::E164); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Format the phone number in RFC3966 format. |
|
134 | - * |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - public function formatRFC3966() |
|
138 | - { |
|
139 | - return $this->format(PhoneNumberFormat::RFC3966); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Format the phone number in a given format. |
|
144 | - * |
|
145 | - * @param string $format |
|
146 | - * @return string |
|
147 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException |
|
148 | - */ |
|
149 | - public function format($format) |
|
150 | - { |
|
151 | - $parsedFormat = static::parseFormat($format); |
|
152 | - |
|
153 | - if (is_null($parsedFormat)) { |
|
154 | - throw NumberFormatException::invalid($format); |
|
155 | - } |
|
156 | - |
|
157 | - return $this->lib->format( |
|
158 | - $this->getPhoneNumberInstance(), |
|
159 | - $parsedFormat |
|
160 | - ); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Format the phone number in a way that it can be dialled from the provided country. |
|
165 | - * |
|
166 | - * @param string $country |
|
167 | - * @return string |
|
168 | - * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
169 | - */ |
|
170 | - public function formatForCountry($country) |
|
171 | - { |
|
172 | - if (! static::isValidCountryCode($country)) { |
|
173 | - throw CountryCodeException::invalid($country); |
|
174 | - } |
|
175 | - |
|
176 | - return $this->lib->formatOutOfCountryCallingNumber( |
|
177 | - $this->getPhoneNumberInstance(), |
|
178 | - $country |
|
179 | - ); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
184 | - * |
|
185 | - * @param string $country |
|
186 | - * @param bool $removeFormatting |
|
187 | - * @return string |
|
188 | - * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
189 | - */ |
|
190 | - public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
191 | - { |
|
192 | - if (! static::isValidCountryCode($country)) { |
|
193 | - throw CountryCodeException::invalid($country); |
|
194 | - } |
|
195 | - |
|
196 | - return $this->lib->formatNumberForMobileDialing( |
|
197 | - $this->getPhoneNumberInstance(), |
|
198 | - $country, |
|
199 | - $removeFormatting |
|
200 | - ); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Get the phone number's country. |
|
205 | - * |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - public function getCountry() |
|
209 | - { |
|
210 | - if (! $this->country) { |
|
211 | - $this->country = $this->filterValidCountry($this->countries); |
|
212 | - } |
|
213 | - |
|
214 | - return $this->country; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Check if the phone number is of (a) given country(ies). |
|
219 | - * |
|
220 | - * @param string|array $country |
|
221 | - * @return bool |
|
222 | - */ |
|
223 | - public function isOfCountry($country) |
|
224 | - { |
|
225 | - $countries = static::parseCountries($country); |
|
226 | - |
|
227 | - return in_array($this->getCountry(), $countries); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Filter the provided countries to the one that is valid for the number. |
|
232 | - * |
|
233 | - * @param string|array $countries |
|
234 | - * @return string |
|
235 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException |
|
236 | - */ |
|
237 | - protected function filterValidCountry($countries) |
|
238 | - { |
|
239 | - $result = Collection::make($countries) |
|
240 | - ->filter(function ($country) { |
|
241 | - $instance = $this->lib->parse($this->number, $country); |
|
242 | - |
|
243 | - return $this->lenient |
|
244 | - ? $this->lib->isPossibleNumber($instance, $country) |
|
245 | - : $this->lib->isValidNumberForRegion($instance, $country); |
|
246 | - })->first(); |
|
247 | - |
|
248 | - // If we got a new result, return it. |
|
249 | - if ($result) { |
|
250 | - return $result; |
|
251 | - } |
|
252 | - |
|
253 | - // Last resort: try to detect it from an international number. |
|
254 | - if ($this->numberLooksInternational()) { |
|
255 | - $countries[] = null; |
|
256 | - } |
|
257 | - |
|
258 | - foreach ($countries as $country) { |
|
259 | - $instance = $this->lib->parse($this->number, $country); |
|
260 | - |
|
261 | - if ($this->lib->isValidNumber($instance)) { |
|
262 | - return $this->lib->getRegionCodeForNumber($instance); |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - if ($countries = array_filter($countries)) { |
|
267 | - throw NumberParseException::countryMismatch($this->number, $countries); |
|
268 | - } |
|
269 | - |
|
270 | - throw NumberParseException::countryRequired($this->number); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Get the phone number's type. |
|
275 | - * |
|
276 | - * @param bool $asConstant |
|
277 | - * @return string|int|null |
|
278 | - */ |
|
279 | - public function getType($asConstant = false) |
|
280 | - { |
|
281 | - $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
282 | - |
|
283 | - if ($asConstant) { |
|
284 | - return $type; |
|
285 | - } |
|
286 | - |
|
287 | - $stringType = Arr::get(static::parseTypesAsStrings($type), 0); |
|
288 | - |
|
289 | - return $stringType ? strtolower($stringType) : null; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Check if the phone number is of (a) given type(s). |
|
294 | - * |
|
295 | - * @param string $type |
|
296 | - * @return bool |
|
297 | - */ |
|
298 | - public function isOfType($type) |
|
299 | - { |
|
300 | - $types = static::parseTypes($type); |
|
301 | - |
|
302 | - // Add the unsure type when applicable. |
|
303 | - if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { |
|
304 | - $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
305 | - } |
|
306 | - |
|
307 | - return in_array($this->getType(true), $types, true); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Get the PhoneNumber instance of the current number. |
|
312 | - * |
|
313 | - * @return \libphonenumber\PhoneNumber |
|
314 | - */ |
|
315 | - public function getPhoneNumberInstance() |
|
316 | - { |
|
317 | - return $this->lib->parse($this->number, $this->getCountry()); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Determine whether the phone number seems to be in international format. |
|
322 | - * |
|
323 | - * @return bool |
|
324 | - */ |
|
325 | - protected function numberLooksInternational() |
|
326 | - { |
|
327 | - return Str::startsWith($this->number, '+'); |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Enable lenient number parsing. |
|
332 | - * |
|
333 | - * @return $this |
|
334 | - */ |
|
335 | - public function lenient() |
|
336 | - { |
|
337 | - $this->lenient = true; |
|
338 | - |
|
339 | - return $this; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Convert the phone instance to JSON. |
|
344 | - * |
|
345 | - * @param int $options |
|
346 | - * @return string |
|
347 | - */ |
|
348 | - public function toJson($options = 0) |
|
349 | - { |
|
350 | - return json_encode($this->jsonSerialize(), $options); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Convert the phone instance into something JSON serializable. |
|
355 | - * |
|
356 | - * @return string |
|
357 | - */ |
|
358 | - public function jsonSerialize() |
|
359 | - { |
|
360 | - return $this->formatE164(); |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Convert the phone instance into a string representation. |
|
365 | - * |
|
366 | - * @return string |
|
367 | - */ |
|
368 | - public function serialize() |
|
369 | - { |
|
370 | - return $this->formatE164(); |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Reconstructs the phone instance from a string representation. |
|
375 | - * |
|
376 | - * @param string $serialized |
|
377 | - */ |
|
378 | - public function unserialize($serialized) |
|
379 | - { |
|
380 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
381 | - $this->number = $serialized; |
|
382 | - $this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Convert the phone instance to a formatted number. |
|
387 | - * |
|
388 | - * @return string |
|
389 | - */ |
|
390 | - public function __toString() |
|
391 | - { |
|
392 | - // Formatting the phone number could throw an exception, but __toString() doesn't cope well with that. |
|
393 | - // Let's just return the original number in that case. |
|
394 | - try { |
|
395 | - return $this->formatE164(); |
|
396 | - } catch (Exception $exception) { |
|
397 | - return (string) $this->number; |
|
398 | - } |
|
399 | - } |
|
22 | + use ParsesCountries, |
|
23 | + ParsesFormats, |
|
24 | + ParsesTypes; |
|
25 | + |
|
26 | + /** |
|
27 | + * The provided phone number. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + protected $number; |
|
32 | + |
|
33 | + /** |
|
34 | + * The provided phone country. |
|
35 | + * |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $countries = []; |
|
39 | + |
|
40 | + /** |
|
41 | + * The detected phone country. |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $country; |
|
46 | + |
|
47 | + /** |
|
48 | + * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
49 | + * |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + protected $lenient = false; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var \libphonenumber\PhoneNumberUtil |
|
56 | + */ |
|
57 | + protected $lib; |
|
58 | + |
|
59 | + /** |
|
60 | + * Phone constructor. |
|
61 | + * |
|
62 | + * @param string $number |
|
63 | + */ |
|
64 | + public function __construct($number) |
|
65 | + { |
|
66 | + $this->number = $number; |
|
67 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Create a phone instance. |
|
72 | + * |
|
73 | + * @param string $number |
|
74 | + * @param string|array $country |
|
75 | + * @return static |
|
76 | + */ |
|
77 | + public static function make($number, $country = []) |
|
78 | + { |
|
79 | + $instance = new static($number); |
|
80 | + |
|
81 | + return $instance->ofCountry($country); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Set the country to which the phone number belongs to. |
|
86 | + * |
|
87 | + * @param string|array $country |
|
88 | + * @return static |
|
89 | + */ |
|
90 | + public function ofCountry($country) |
|
91 | + { |
|
92 | + $countries = is_array($country) ? $country : func_get_args(); |
|
93 | + |
|
94 | + $instance = clone $this; |
|
95 | + $instance->countries = array_unique( |
|
96 | + array_merge($instance->countries, static::parseCountries($countries)) |
|
97 | + ); |
|
98 | + |
|
99 | + return $instance; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Format the phone number in international format. |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function formatInternational() |
|
108 | + { |
|
109 | + return $this->format(PhoneNumberFormat::INTERNATIONAL); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Format the phone number in national format. |
|
114 | + * |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function formatNational() |
|
118 | + { |
|
119 | + return $this->format(PhoneNumberFormat::NATIONAL); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Format the phone number in E164 format. |
|
124 | + * |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + public function formatE164() |
|
128 | + { |
|
129 | + return $this->format(PhoneNumberFormat::E164); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Format the phone number in RFC3966 format. |
|
134 | + * |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + public function formatRFC3966() |
|
138 | + { |
|
139 | + return $this->format(PhoneNumberFormat::RFC3966); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Format the phone number in a given format. |
|
144 | + * |
|
145 | + * @param string $format |
|
146 | + * @return string |
|
147 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException |
|
148 | + */ |
|
149 | + public function format($format) |
|
150 | + { |
|
151 | + $parsedFormat = static::parseFormat($format); |
|
152 | + |
|
153 | + if (is_null($parsedFormat)) { |
|
154 | + throw NumberFormatException::invalid($format); |
|
155 | + } |
|
156 | + |
|
157 | + return $this->lib->format( |
|
158 | + $this->getPhoneNumberInstance(), |
|
159 | + $parsedFormat |
|
160 | + ); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Format the phone number in a way that it can be dialled from the provided country. |
|
165 | + * |
|
166 | + * @param string $country |
|
167 | + * @return string |
|
168 | + * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
169 | + */ |
|
170 | + public function formatForCountry($country) |
|
171 | + { |
|
172 | + if (! static::isValidCountryCode($country)) { |
|
173 | + throw CountryCodeException::invalid($country); |
|
174 | + } |
|
175 | + |
|
176 | + return $this->lib->formatOutOfCountryCallingNumber( |
|
177 | + $this->getPhoneNumberInstance(), |
|
178 | + $country |
|
179 | + ); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Format the phone number in a way that it can be dialled from the provided country using a cellphone. |
|
184 | + * |
|
185 | + * @param string $country |
|
186 | + * @param bool $removeFormatting |
|
187 | + * @return string |
|
188 | + * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException |
|
189 | + */ |
|
190 | + public function formatForMobileDialingInCountry($country, $removeFormatting = false) |
|
191 | + { |
|
192 | + if (! static::isValidCountryCode($country)) { |
|
193 | + throw CountryCodeException::invalid($country); |
|
194 | + } |
|
195 | + |
|
196 | + return $this->lib->formatNumberForMobileDialing( |
|
197 | + $this->getPhoneNumberInstance(), |
|
198 | + $country, |
|
199 | + $removeFormatting |
|
200 | + ); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Get the phone number's country. |
|
205 | + * |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + public function getCountry() |
|
209 | + { |
|
210 | + if (! $this->country) { |
|
211 | + $this->country = $this->filterValidCountry($this->countries); |
|
212 | + } |
|
213 | + |
|
214 | + return $this->country; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Check if the phone number is of (a) given country(ies). |
|
219 | + * |
|
220 | + * @param string|array $country |
|
221 | + * @return bool |
|
222 | + */ |
|
223 | + public function isOfCountry($country) |
|
224 | + { |
|
225 | + $countries = static::parseCountries($country); |
|
226 | + |
|
227 | + return in_array($this->getCountry(), $countries); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Filter the provided countries to the one that is valid for the number. |
|
232 | + * |
|
233 | + * @param string|array $countries |
|
234 | + * @return string |
|
235 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException |
|
236 | + */ |
|
237 | + protected function filterValidCountry($countries) |
|
238 | + { |
|
239 | + $result = Collection::make($countries) |
|
240 | + ->filter(function ($country) { |
|
241 | + $instance = $this->lib->parse($this->number, $country); |
|
242 | + |
|
243 | + return $this->lenient |
|
244 | + ? $this->lib->isPossibleNumber($instance, $country) |
|
245 | + : $this->lib->isValidNumberForRegion($instance, $country); |
|
246 | + })->first(); |
|
247 | + |
|
248 | + // If we got a new result, return it. |
|
249 | + if ($result) { |
|
250 | + return $result; |
|
251 | + } |
|
252 | + |
|
253 | + // Last resort: try to detect it from an international number. |
|
254 | + if ($this->numberLooksInternational()) { |
|
255 | + $countries[] = null; |
|
256 | + } |
|
257 | + |
|
258 | + foreach ($countries as $country) { |
|
259 | + $instance = $this->lib->parse($this->number, $country); |
|
260 | + |
|
261 | + if ($this->lib->isValidNumber($instance)) { |
|
262 | + return $this->lib->getRegionCodeForNumber($instance); |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + if ($countries = array_filter($countries)) { |
|
267 | + throw NumberParseException::countryMismatch($this->number, $countries); |
|
268 | + } |
|
269 | + |
|
270 | + throw NumberParseException::countryRequired($this->number); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Get the phone number's type. |
|
275 | + * |
|
276 | + * @param bool $asConstant |
|
277 | + * @return string|int|null |
|
278 | + */ |
|
279 | + public function getType($asConstant = false) |
|
280 | + { |
|
281 | + $type = $this->lib->getNumberType($this->getPhoneNumberInstance()); |
|
282 | + |
|
283 | + if ($asConstant) { |
|
284 | + return $type; |
|
285 | + } |
|
286 | + |
|
287 | + $stringType = Arr::get(static::parseTypesAsStrings($type), 0); |
|
288 | + |
|
289 | + return $stringType ? strtolower($stringType) : null; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Check if the phone number is of (a) given type(s). |
|
294 | + * |
|
295 | + * @param string $type |
|
296 | + * @return bool |
|
297 | + */ |
|
298 | + public function isOfType($type) |
|
299 | + { |
|
300 | + $types = static::parseTypes($type); |
|
301 | + |
|
302 | + // Add the unsure type when applicable. |
|
303 | + if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) { |
|
304 | + $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
305 | + } |
|
306 | + |
|
307 | + return in_array($this->getType(true), $types, true); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Get the PhoneNumber instance of the current number. |
|
312 | + * |
|
313 | + * @return \libphonenumber\PhoneNumber |
|
314 | + */ |
|
315 | + public function getPhoneNumberInstance() |
|
316 | + { |
|
317 | + return $this->lib->parse($this->number, $this->getCountry()); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Determine whether the phone number seems to be in international format. |
|
322 | + * |
|
323 | + * @return bool |
|
324 | + */ |
|
325 | + protected function numberLooksInternational() |
|
326 | + { |
|
327 | + return Str::startsWith($this->number, '+'); |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Enable lenient number parsing. |
|
332 | + * |
|
333 | + * @return $this |
|
334 | + */ |
|
335 | + public function lenient() |
|
336 | + { |
|
337 | + $this->lenient = true; |
|
338 | + |
|
339 | + return $this; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Convert the phone instance to JSON. |
|
344 | + * |
|
345 | + * @param int $options |
|
346 | + * @return string |
|
347 | + */ |
|
348 | + public function toJson($options = 0) |
|
349 | + { |
|
350 | + return json_encode($this->jsonSerialize(), $options); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Convert the phone instance into something JSON serializable. |
|
355 | + * |
|
356 | + * @return string |
|
357 | + */ |
|
358 | + public function jsonSerialize() |
|
359 | + { |
|
360 | + return $this->formatE164(); |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Convert the phone instance into a string representation. |
|
365 | + * |
|
366 | + * @return string |
|
367 | + */ |
|
368 | + public function serialize() |
|
369 | + { |
|
370 | + return $this->formatE164(); |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Reconstructs the phone instance from a string representation. |
|
375 | + * |
|
376 | + * @param string $serialized |
|
377 | + */ |
|
378 | + public function unserialize($serialized) |
|
379 | + { |
|
380 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
381 | + $this->number = $serialized; |
|
382 | + $this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance()); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Convert the phone instance to a formatted number. |
|
387 | + * |
|
388 | + * @return string |
|
389 | + */ |
|
390 | + public function __toString() |
|
391 | + { |
|
392 | + // Formatting the phone number could throw an exception, but __toString() doesn't cope well with that. |
|
393 | + // Let's just return the original number in that case. |
|
394 | + try { |
|
395 | + return $this->formatE164(); |
|
396 | + } catch (Exception $exception) { |
|
397 | + return (string) $this->number; |
|
398 | + } |
|
399 | + } |
|
400 | 400 | } |
@@ -61,7 +61,7 @@ |
||
61 | 61 | $phoneNumber = PhoneNumber::make($value, $country); |
62 | 62 | |
63 | 63 | // Type validation. |
64 | - if (! empty($types) && ! $phoneNumber->isOfType($types)) { |
|
64 | + if (!empty($types) && !$phoneNumber->isOfType($types)) { |
|
65 | 65 | continue; |
66 | 66 | } |
67 | 67 |
@@ -11,122 +11,122 @@ |
||
11 | 11 | |
12 | 12 | class Phone |
13 | 13 | { |
14 | - use ParsesCountries, |
|
15 | - ParsesTypes; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var \libphonenumber\PhoneNumberUtil |
|
19 | - */ |
|
20 | - protected $lib; |
|
21 | - |
|
22 | - /** |
|
23 | - * Phone constructor. |
|
24 | - */ |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->lib = PhoneNumberUtil::getInstance(); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * Validates a phone number. |
|
32 | - * |
|
33 | - * @param string $attribute |
|
34 | - * @param mixed $value |
|
35 | - * @param array $parameters |
|
36 | - * @param object $validator |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function validate($attribute, $value, array $parameters, $validator) |
|
40 | - { |
|
41 | - $data = $validator->getData(); |
|
42 | - |
|
43 | - list( |
|
44 | - $countries, |
|
45 | - $types, |
|
46 | - $detect, |
|
47 | - $lenient) = $this->extractParameters($attribute, $parameters, $data); |
|
48 | - |
|
49 | - // A "null" country is prepended: |
|
50 | - // 1. In case of auto-detection to have the validation run first without supplying a country. |
|
51 | - // 2. In case of lenient validation without provided countries; we still might have some luck... |
|
52 | - if ($detect || ($lenient && empty($countries))) { |
|
53 | - array_unshift($countries, null); |
|
54 | - } |
|
55 | - |
|
56 | - foreach ($countries as $country) { |
|
57 | - try { |
|
58 | - // Parsing the phone number also validates the country, so no need to do this explicitly. |
|
59 | - // It'll throw a PhoneCountryException upon failure. |
|
60 | - $phoneNumber = PhoneNumber::make($value, $country); |
|
61 | - |
|
62 | - // Type validation. |
|
63 | - if (! empty($types) && ! $phoneNumber->isOfType($types)) { |
|
64 | - continue; |
|
65 | - } |
|
66 | - |
|
67 | - $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance(); |
|
68 | - |
|
69 | - // Lenient validation. |
|
70 | - if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) { |
|
71 | - return true; |
|
72 | - } |
|
73 | - |
|
74 | - $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance(); |
|
75 | - |
|
76 | - // Country detection. |
|
77 | - if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) { |
|
78 | - return true; |
|
79 | - } |
|
80 | - |
|
81 | - // Default number+country validation. |
|
82 | - if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) { |
|
83 | - return true; |
|
84 | - } |
|
85 | - } catch (NumberParseException $e) { |
|
86 | - continue; |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - return false; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Parse and extract parameters in the appropriate validation arguments. |
|
95 | - * |
|
96 | - * @param string $attribute |
|
97 | - * @param array $parameters |
|
98 | - * @param array $data |
|
99 | - * @return array |
|
100 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
101 | - */ |
|
102 | - protected function extractParameters($attribute, array $parameters, array $data) |
|
103 | - { |
|
104 | - $parameters = array_map('strtolower', $parameters); |
|
105 | - |
|
106 | - // Discover if an input field was provided. If not, guess the field's name. |
|
107 | - $inputField = Collection::make($parameters) |
|
108 | - ->intersect(array_keys(Arr::dot($data))) |
|
109 | - ->first() ?: "${attribute}_country"; |
|
110 | - |
|
111 | - // Attempt to retrieve the field's value. |
|
112 | - if ($inputCountry = Arr::get($data, $inputField)) { |
|
113 | - |
|
114 | - if (static::isValidType($inputField)) { |
|
115 | - throw InvalidParameterException::ambiguous($inputField); |
|
116 | - } |
|
117 | - |
|
118 | - // Invalid country field values should just validate to false. |
|
119 | - // This will also prevent parameter hijacking through the country field. |
|
120 | - if (static::isValidCountryCode($inputCountry)) { |
|
121 | - $parameters[] = $inputCountry; |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - return [ |
|
126 | - static::parseCountries($parameters), |
|
127 | - static::parseTypes($parameters), |
|
128 | - in_array('auto', $parameters), |
|
129 | - in_array('lenient', $parameters) |
|
130 | - ]; |
|
131 | - } |
|
14 | + use ParsesCountries, |
|
15 | + ParsesTypes; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var \libphonenumber\PhoneNumberUtil |
|
19 | + */ |
|
20 | + protected $lib; |
|
21 | + |
|
22 | + /** |
|
23 | + * Phone constructor. |
|
24 | + */ |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->lib = PhoneNumberUtil::getInstance(); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Validates a phone number. |
|
32 | + * |
|
33 | + * @param string $attribute |
|
34 | + * @param mixed $value |
|
35 | + * @param array $parameters |
|
36 | + * @param object $validator |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function validate($attribute, $value, array $parameters, $validator) |
|
40 | + { |
|
41 | + $data = $validator->getData(); |
|
42 | + |
|
43 | + list( |
|
44 | + $countries, |
|
45 | + $types, |
|
46 | + $detect, |
|
47 | + $lenient) = $this->extractParameters($attribute, $parameters, $data); |
|
48 | + |
|
49 | + // A "null" country is prepended: |
|
50 | + // 1. In case of auto-detection to have the validation run first without supplying a country. |
|
51 | + // 2. In case of lenient validation without provided countries; we still might have some luck... |
|
52 | + if ($detect || ($lenient && empty($countries))) { |
|
53 | + array_unshift($countries, null); |
|
54 | + } |
|
55 | + |
|
56 | + foreach ($countries as $country) { |
|
57 | + try { |
|
58 | + // Parsing the phone number also validates the country, so no need to do this explicitly. |
|
59 | + // It'll throw a PhoneCountryException upon failure. |
|
60 | + $phoneNumber = PhoneNumber::make($value, $country); |
|
61 | + |
|
62 | + // Type validation. |
|
63 | + if (! empty($types) && ! $phoneNumber->isOfType($types)) { |
|
64 | + continue; |
|
65 | + } |
|
66 | + |
|
67 | + $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance(); |
|
68 | + |
|
69 | + // Lenient validation. |
|
70 | + if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) { |
|
71 | + return true; |
|
72 | + } |
|
73 | + |
|
74 | + $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance(); |
|
75 | + |
|
76 | + // Country detection. |
|
77 | + if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) { |
|
78 | + return true; |
|
79 | + } |
|
80 | + |
|
81 | + // Default number+country validation. |
|
82 | + if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) { |
|
83 | + return true; |
|
84 | + } |
|
85 | + } catch (NumberParseException $e) { |
|
86 | + continue; |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + return false; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Parse and extract parameters in the appropriate validation arguments. |
|
95 | + * |
|
96 | + * @param string $attribute |
|
97 | + * @param array $parameters |
|
98 | + * @param array $data |
|
99 | + * @return array |
|
100 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
101 | + */ |
|
102 | + protected function extractParameters($attribute, array $parameters, array $data) |
|
103 | + { |
|
104 | + $parameters = array_map('strtolower', $parameters); |
|
105 | + |
|
106 | + // Discover if an input field was provided. If not, guess the field's name. |
|
107 | + $inputField = Collection::make($parameters) |
|
108 | + ->intersect(array_keys(Arr::dot($data))) |
|
109 | + ->first() ?: "${attribute}_country"; |
|
110 | + |
|
111 | + // Attempt to retrieve the field's value. |
|
112 | + if ($inputCountry = Arr::get($data, $inputField)) { |
|
113 | + |
|
114 | + if (static::isValidType($inputField)) { |
|
115 | + throw InvalidParameterException::ambiguous($inputField); |
|
116 | + } |
|
117 | + |
|
118 | + // Invalid country field values should just validate to false. |
|
119 | + // This will also prevent parameter hijacking through the country field. |
|
120 | + if (static::isValidCountryCode($inputCountry)) { |
|
121 | + $parameters[] = $inputCountry; |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + return [ |
|
126 | + static::parseCountries($parameters), |
|
127 | + static::parseTypes($parameters), |
|
128 | + in_array('auto', $parameters), |
|
129 | + in_array('lenient', $parameters) |
|
130 | + ]; |
|
131 | + } |
|
132 | 132 | } |
@@ -25,10 +25,10 @@ |
||
25 | 25 | protected function parseCountries($countries) |
26 | 26 | { |
27 | 27 | return Collection::make(is_array($countries) ? $countries : func_get_args()) |
28 | - ->map(function ($country) { |
|
28 | + ->map(function($country) { |
|
29 | 29 | return strtoupper($country); |
30 | 30 | }) |
31 | - ->filter(function ($value) { |
|
31 | + ->filter(function($value) { |
|
32 | 32 | return static::isValidCountryCode($value); |
33 | 33 | })->toArray(); |
34 | 34 | } |
@@ -5,39 +5,39 @@ |
||
5 | 5 | |
6 | 6 | trait ParsesCountries |
7 | 7 | { |
8 | - /** |
|
9 | - * Determine whether the given country code is valid. |
|
10 | - * |
|
11 | - * @param string $country |
|
12 | - * @return bool |
|
13 | - */ |
|
14 | - public static function isValidCountryCode($country) |
|
15 | - { |
|
16 | - $iso3166 = new ISO3166; |
|
8 | + /** |
|
9 | + * Determine whether the given country code is valid. |
|
10 | + * |
|
11 | + * @param string $country |
|
12 | + * @return bool |
|
13 | + */ |
|
14 | + public static function isValidCountryCode($country) |
|
15 | + { |
|
16 | + $iso3166 = new ISO3166; |
|
17 | 17 | |
18 | - try { |
|
19 | - $iso3166->alpha2($country); |
|
18 | + try { |
|
19 | + $iso3166->alpha2($country); |
|
20 | 20 | |
21 | - return true; |
|
22 | - } catch (\Exception $e) { |
|
23 | - return false; |
|
24 | - } |
|
25 | - } |
|
21 | + return true; |
|
22 | + } catch (\Exception $e) { |
|
23 | + return false; |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Parse the provided phone countries to a valid array. |
|
29 | - * |
|
30 | - * @param string|array $countries |
|
31 | - * @return array |
|
32 | - */ |
|
33 | - protected function parseCountries($countries) |
|
34 | - { |
|
35 | - return Collection::make(is_array($countries) ? $countries : func_get_args()) |
|
36 | - ->map(function ($country) { |
|
37 | - return strtoupper($country); |
|
38 | - }) |
|
39 | - ->filter(function ($value) { |
|
40 | - return static::isValidCountryCode($value); |
|
41 | - })->toArray(); |
|
42 | - } |
|
27 | + /** |
|
28 | + * Parse the provided phone countries to a valid array. |
|
29 | + * |
|
30 | + * @param string|array $countries |
|
31 | + * @return array |
|
32 | + */ |
|
33 | + protected function parseCountries($countries) |
|
34 | + { |
|
35 | + return Collection::make(is_array($countries) ? $countries : func_get_args()) |
|
36 | + ->map(function ($country) { |
|
37 | + return strtoupper($country); |
|
38 | + }) |
|
39 | + ->filter(function ($value) { |
|
40 | + return static::isValidCountryCode($value); |
|
41 | + })->toArray(); |
|
42 | + } |
|
43 | 43 | } |
44 | 44 | \ No newline at end of file |
@@ -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 isValidFormat($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 isValidFormat($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, true)) { |
|
39 | - return $format; |
|
40 | - } |
|
37 | + // If the format equals a constant's value, just return it. |
|
38 | + if (in_array($format, static::$formats, true)) { |
|
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 isValidFormat($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 | } |
@@ -6,147 +6,147 @@ |
||
6 | 6 | |
7 | 7 | class Phone |
8 | 8 | { |
9 | - /** |
|
10 | - * The provided phone countries. |
|
11 | - * |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected $countries = []; |
|
15 | - |
|
16 | - /** |
|
17 | - * The input field name to check for a country value. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $countryField; |
|
22 | - |
|
23 | - /** |
|
24 | - * The provided phone types. |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $types = []; |
|
29 | - |
|
30 | - /** |
|
31 | - * Whether the number's country should be auto-detected. |
|
32 | - * |
|
33 | - * @var bool |
|
34 | - */ |
|
35 | - protected $detect = false; |
|
36 | - |
|
37 | - /** |
|
38 | - * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
39 | - * |
|
40 | - * @var bool |
|
41 | - */ |
|
42 | - protected $lenient = false; |
|
43 | - |
|
44 | - /** |
|
45 | - * Set the phone countries. |
|
46 | - * |
|
47 | - * @param string|array $country |
|
48 | - * @return $this |
|
49 | - */ |
|
50 | - public function country($country) |
|
51 | - { |
|
52 | - $countries = is_array($country) ? $country : func_get_args(); |
|
53 | - |
|
54 | - $this->countries = array_merge($this->countries, $countries); |
|
55 | - |
|
56 | - return $this; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Set the country input field. |
|
61 | - * |
|
62 | - * @param string $name |
|
63 | - * @return $this |
|
64 | - */ |
|
65 | - public function countryField($name) |
|
66 | - { |
|
67 | - $this->countryField = $name; |
|
68 | - |
|
69 | - return $this; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Set the phone types. |
|
74 | - * |
|
75 | - * @param string|array $type |
|
76 | - * @return $this |
|
77 | - */ |
|
78 | - public function type($type) |
|
79 | - { |
|
80 | - $types = is_array($type) ? $type : func_get_args(); |
|
81 | - |
|
82 | - $this->types = array_merge($this->types, $types); |
|
83 | - |
|
84 | - return $this; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Shortcut method for mobile type restriction. |
|
89 | - * |
|
90 | - * @return $this |
|
91 | - */ |
|
92 | - public function mobile() |
|
93 | - { |
|
94 | - $this->type(PhoneNumberType::MOBILE); |
|
95 | - |
|
96 | - return $this; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Shortcut method for fixed line type restriction. |
|
101 | - * |
|
102 | - * @return $this |
|
103 | - */ |
|
104 | - public function fixedLine() |
|
105 | - { |
|
106 | - $this->type(PhoneNumberType::FIXED_LINE); |
|
107 | - |
|
108 | - return $this; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Enable automatic country detection. |
|
113 | - * |
|
114 | - * @return $this |
|
115 | - */ |
|
116 | - public function detect() |
|
117 | - { |
|
118 | - $this->detect = true; |
|
119 | - |
|
120 | - return $this; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Enable lenient number checking. |
|
125 | - * |
|
126 | - * @return $this |
|
127 | - */ |
|
128 | - public function lenient() |
|
129 | - { |
|
130 | - $this->lenient = true; |
|
131 | - |
|
132 | - return $this; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Convert the rule to a validation string. |
|
137 | - * |
|
138 | - * @return string |
|
139 | - */ |
|
140 | - public function __toString() |
|
141 | - { |
|
142 | - $parameters = implode(',', array_merge( |
|
143 | - $this->countries, |
|
144 | - $this->types, |
|
145 | - ($this->countryField ? [$this->countryField]: []), |
|
146 | - ($this->detect ? ['AUTO'] : []), |
|
147 | - ($this->lenient ? ['LENIENT'] : []) |
|
148 | - )); |
|
149 | - |
|
150 | - return 'phone' . (! empty($parameters) ? ":$parameters" : ''); |
|
151 | - } |
|
9 | + /** |
|
10 | + * The provided phone countries. |
|
11 | + * |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected $countries = []; |
|
15 | + |
|
16 | + /** |
|
17 | + * The input field name to check for a country value. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $countryField; |
|
22 | + |
|
23 | + /** |
|
24 | + * The provided phone types. |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $types = []; |
|
29 | + |
|
30 | + /** |
|
31 | + * Whether the number's country should be auto-detected. |
|
32 | + * |
|
33 | + * @var bool |
|
34 | + */ |
|
35 | + protected $detect = false; |
|
36 | + |
|
37 | + /** |
|
38 | + * Whether to allow lenient checks (i.e. landline numbers without area codes). |
|
39 | + * |
|
40 | + * @var bool |
|
41 | + */ |
|
42 | + protected $lenient = false; |
|
43 | + |
|
44 | + /** |
|
45 | + * Set the phone countries. |
|
46 | + * |
|
47 | + * @param string|array $country |
|
48 | + * @return $this |
|
49 | + */ |
|
50 | + public function country($country) |
|
51 | + { |
|
52 | + $countries = is_array($country) ? $country : func_get_args(); |
|
53 | + |
|
54 | + $this->countries = array_merge($this->countries, $countries); |
|
55 | + |
|
56 | + return $this; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Set the country input field. |
|
61 | + * |
|
62 | + * @param string $name |
|
63 | + * @return $this |
|
64 | + */ |
|
65 | + public function countryField($name) |
|
66 | + { |
|
67 | + $this->countryField = $name; |
|
68 | + |
|
69 | + return $this; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Set the phone types. |
|
74 | + * |
|
75 | + * @param string|array $type |
|
76 | + * @return $this |
|
77 | + */ |
|
78 | + public function type($type) |
|
79 | + { |
|
80 | + $types = is_array($type) ? $type : func_get_args(); |
|
81 | + |
|
82 | + $this->types = array_merge($this->types, $types); |
|
83 | + |
|
84 | + return $this; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Shortcut method for mobile type restriction. |
|
89 | + * |
|
90 | + * @return $this |
|
91 | + */ |
|
92 | + public function mobile() |
|
93 | + { |
|
94 | + $this->type(PhoneNumberType::MOBILE); |
|
95 | + |
|
96 | + return $this; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Shortcut method for fixed line type restriction. |
|
101 | + * |
|
102 | + * @return $this |
|
103 | + */ |
|
104 | + public function fixedLine() |
|
105 | + { |
|
106 | + $this->type(PhoneNumberType::FIXED_LINE); |
|
107 | + |
|
108 | + return $this; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Enable automatic country detection. |
|
113 | + * |
|
114 | + * @return $this |
|
115 | + */ |
|
116 | + public function detect() |
|
117 | + { |
|
118 | + $this->detect = true; |
|
119 | + |
|
120 | + return $this; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Enable lenient number checking. |
|
125 | + * |
|
126 | + * @return $this |
|
127 | + */ |
|
128 | + public function lenient() |
|
129 | + { |
|
130 | + $this->lenient = true; |
|
131 | + |
|
132 | + return $this; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Convert the rule to a validation string. |
|
137 | + * |
|
138 | + * @return string |
|
139 | + */ |
|
140 | + public function __toString() |
|
141 | + { |
|
142 | + $parameters = implode(',', array_merge( |
|
143 | + $this->countries, |
|
144 | + $this->types, |
|
145 | + ($this->countryField ? [$this->countryField]: []), |
|
146 | + ($this->detect ? ['AUTO'] : []), |
|
147 | + ($this->lenient ? ['LENIENT'] : []) |
|
148 | + )); |
|
149 | + |
|
150 | + return 'phone' . (! empty($parameters) ? ":$parameters" : ''); |
|
151 | + } |
|
152 | 152 | } |
153 | 153 | \ No newline at end of file |
@@ -142,11 +142,11 @@ |
||
142 | 142 | $parameters = implode(',', array_merge( |
143 | 143 | $this->countries, |
144 | 144 | $this->types, |
145 | - ($this->countryField ? [$this->countryField]: []), |
|
145 | + ($this->countryField ? [$this->countryField] : []), |
|
146 | 146 | ($this->detect ? ['AUTO'] : []), |
147 | 147 | ($this->lenient ? ['LENIENT'] : []) |
148 | 148 | )); |
149 | 149 | |
150 | - return 'phone' . (! empty($parameters) ? ":$parameters" : ''); |
|
150 | + return 'phone'.(!empty($parameters) ? ":$parameters" : ''); |
|
151 | 151 | } |
152 | 152 | } |
153 | 153 | \ No newline at end of file |