1 | <?php namespace Propaganistas\LaravelPhone; |
||
9 | class PhoneValidator |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var \libphonenumber\PhoneNumberUtil |
||
14 | */ |
||
15 | protected $lib; |
||
16 | |||
17 | /** |
||
18 | * Whether the country should be auto-detected. |
||
19 | * |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $autodetect = false; |
||
23 | |||
24 | /** |
||
25 | * Whether to allow lenient checking of numbers (i.e. landline without area codes). |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $lenient = false; |
||
30 | |||
31 | /** |
||
32 | * Countries to validate against. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $countries = []; |
||
37 | |||
38 | /** |
||
39 | * Transformed phone number types to validate against. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $types = []; |
||
44 | |||
45 | /** |
||
46 | * PhoneValidator constructor. |
||
47 | */ |
||
48 | 18 | public function __construct(PhoneNumberUtil $lib) |
|
52 | |||
53 | /** |
||
54 | * Validates a phone number. |
||
55 | * |
||
56 | * @param string $attribute |
||
57 | * @param mixed $value |
||
58 | * @param array $parameters |
||
59 | * @param object $validator |
||
60 | * @return bool |
||
61 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
62 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
63 | */ |
||
64 | 18 | public function validatePhone($attribute, $value, array $parameters, $validator) |
|
65 | { |
||
66 | 16 | $this->assignParameters(array_map('strtoupper', $parameters)); |
|
67 | |||
68 | 15 | $this->checkCountries($attribute, $validator); |
|
69 | |||
70 | // If autodetecting, let's first try without a country. |
||
71 | // Otherwise use provided countries as default. |
||
72 | 12 | if ($this->autodetect || ($this->lenient && empty($this->countries))) { |
|
73 | 4 | array_unshift($this->countries, null); |
|
74 | 2 | } |
|
75 | |||
76 | 12 | foreach ($this->countries as $country) { |
|
77 | 12 | if ($this->isValidNumber($value, $country)) { |
|
78 | 14 | return true; |
|
79 | } |
||
80 | 6 | } |
|
81 | |||
82 | // All specified country validations have failed. |
||
83 | 13 | return false; |
|
84 | 2 | } |
|
85 | |||
86 | /** |
||
87 | * Parses the supplied validator parameters. |
||
88 | * |
||
89 | * @param array $parameters |
||
90 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
91 | */ |
||
92 | 18 | protected function assignParameters(array $parameters) |
|
114 | |||
115 | /** |
||
116 | * Checks the detected countries. Overrides countries if a country field is present. |
||
117 | * When using a country field, we should validate to false if country is empty so no exception |
||
118 | * will be thrown. |
||
119 | * |
||
120 | * @param string $attribute |
||
121 | * @param $validator |
||
122 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
123 | */ |
||
124 | 15 | protected function checkCountries($attribute, $validator) |
|
135 | |||
136 | /** |
||
137 | * Performs the actual validation of the phone number. |
||
138 | * |
||
139 | * @param mixed $number |
||
140 | * @param null $country |
||
141 | * @return bool |
||
142 | */ |
||
143 | 14 | protected function isValidNumber($number, $country = null) |
|
178 | |||
179 | /** |
||
180 | * Parses the supplied phone number types. |
||
181 | * |
||
182 | * @param array $types |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function parseTypes(array $types) |
||
199 | |||
200 | /** |
||
201 | * Checks if the supplied string is a valid country code using some arbitrary country validation. |
||
202 | * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'. |
||
203 | * |
||
204 | * @param string $country |
||
205 | * @return bool |
||
206 | */ |
||
207 | 12 | public function isPhoneCountry($country) |
|
211 | |||
212 | /** |
||
213 | * Checks if the supplied string is a valid phone number type. |
||
214 | * |
||
215 | * @param string $type |
||
216 | * @return bool |
||
217 | */ |
||
218 | 10 | public function isPhoneType($type) |
|
225 | |||
226 | } |
||
227 |