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 | * The field to use for country if not passed as a parameter. |
||
33 | * |
||
34 | * @var null|string |
||
35 | */ |
||
36 | protected $countryField = null; |
||
37 | |||
38 | /** |
||
39 | * Countries to validate against. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $countries = []; |
||
44 | |||
45 | /** |
||
46 | * Transformed phone number types to validate against. |
||
47 | * |
||
48 | 18 | * @var array |
|
49 | */ |
||
50 | 18 | protected $types = []; |
|
51 | 16 | ||
52 | /** |
||
53 | * PhoneValidator constructor. |
||
54 | */ |
||
55 | public function __construct(PhoneNumberUtil $lib) |
||
59 | |||
60 | /** |
||
61 | * Validates a phone number. |
||
62 | * |
||
63 | * @param string $attribute |
||
64 | 18 | * @param mixed $value |
|
65 | * @param array $parameters |
||
66 | 16 | * @param object $validator |
|
67 | * @return bool |
||
68 | 15 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
69 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
70 | */ |
||
71 | public function validatePhone($attribute, $value, array $parameters, $validator) |
||
98 | 11 | ||
99 | 12 | /** |
|
100 | 7 | * Parses parameters to find one that matches a field for country input. |
|
101 | 11 | * |
|
102 | 2 | * @param array $parameters |
|
103 | 7 | * @param object $validator |
|
104 | 2 | * |
|
105 | 2 | * @return null|string |
|
106 | */ |
||
107 | 2 | public function checkCountryField(array $parameters, $validator) |
|
119 | |||
120 | /** |
||
121 | * Parses the supplied validator parameters. |
||
122 | * |
||
123 | * @param array $parameters |
||
124 | 15 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
125 | */ |
||
126 | 14 | protected function assignParameters(array $parameters) |
|
148 | 12 | ||
149 | /** |
||
150 | * Checks the detected countries. Overrides countries if a country field is present. |
||
151 | 12 | * When using a country field, we should validate to false if country is empty so no exception |
|
152 | * will be thrown. |
||
153 | * |
||
154 | 12 | * @param string $attribute |
|
155 | 2 | * @param $validator |
|
156 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
157 | */ |
||
158 | protected function checkCountries($attribute, $validator) |
||
169 | |||
170 | /** |
||
171 | * Performs the actual validation of the phone number. |
||
172 | 8 | * |
|
173 | * @param mixed $number |
||
174 | * @param null $country |
||
175 | * @return bool |
||
176 | 8 | */ |
|
177 | protected function isValidNumber($number, $country = null) |
||
212 | |||
213 | /** |
||
214 | * Parses the supplied phone number types. |
||
215 | * |
||
216 | * @param array $types |
||
217 | * @return array |
||
218 | 10 | */ |
|
219 | protected function parseTypes(array $types) |
||
233 | |||
234 | /** |
||
235 | * Checks if the supplied string is a valid country code using some arbitrary country validation. |
||
236 | * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'. |
||
237 | * |
||
238 | * @param string $country |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function isPhoneCountry($country) |
||
245 | |||
246 | /** |
||
247 | * Checks if the supplied string is a valid phone number type. |
||
248 | * |
||
249 | * @param string $type |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function isPhoneType($type) |
||
259 | |||
260 | } |
||
261 |