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 | * @var array |
||
49 | */ |
||
50 | protected $types = []; |
||
51 | |||
52 | /** |
||
53 | * PhoneValidator constructor. |
||
54 | */ |
||
55 | 27 | public function __construct(PhoneNumberUtil $lib) |
|
59 | |||
60 | /** |
||
61 | * Validates a phone number. |
||
62 | * |
||
63 | * @param string $attribute |
||
64 | * @param mixed $value |
||
65 | * @param array $parameters |
||
66 | * @param object $validator |
||
67 | * @return bool |
||
68 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
69 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
70 | */ |
||
71 | 27 | public function validatePhone($attribute, $value, array $parameters, $validator) |
|
98 | |||
99 | /** |
||
100 | * Parses parameters to find one that matches a field for country input. |
||
101 | * |
||
102 | * @param array $parameters |
||
103 | * @param object $validator |
||
104 | * |
||
105 | * @return null|string |
||
106 | */ |
||
107 | 27 | public function checkCountryField(array $parameters, $validator) |
|
119 | |||
120 | /** |
||
121 | * Parses the supplied validator parameters. |
||
122 | * |
||
123 | * @param array $parameters |
||
124 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
125 | */ |
||
126 | 27 | protected function assignParameters(array $parameters) |
|
148 | |||
149 | /** |
||
150 | * Checks the detected countries. Overrides countries if a country field is present. |
||
151 | * When using a country field, we should validate to false if country is empty so no exception |
||
152 | * will be thrown. |
||
153 | * |
||
154 | * @param string $attribute |
||
155 | * @param $validator |
||
156 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
157 | */ |
||
158 | 24 | protected function checkCountries($attribute, $validator) |
|
169 | |||
170 | /** |
||
171 | * Performs the actual validation of the phone number. |
||
172 | * |
||
173 | * @param mixed $number |
||
174 | * @param null $country |
||
175 | * @return bool |
||
176 | */ |
||
177 | 21 | protected function isValidNumber($number, $country = null) |
|
212 | |||
213 | /** |
||
214 | * Parses the supplied phone number types. |
||
215 | * |
||
216 | * @param array $types |
||
217 | * @return array |
||
218 | */ |
||
219 | protected function parseTypes(array $types) |
||
233 | |||
234 | public function isCountryField($field) |
||
238 | |||
239 | /** |
||
240 | * Checks if the supplied string is a valid country code using some arbitrary country validation. |
||
241 | * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'. |
||
242 | * |
||
243 | * @param string $country |
||
244 | * @return bool |
||
245 | */ |
||
246 | 21 | public function isPhoneCountry($country) |
|
250 | |||
251 | /** |
||
252 | * Checks if the supplied string is a valid phone number type. |
||
253 | * |
||
254 | * @param string $type |
||
255 | * @return bool |
||
256 | */ |
||
257 | 18 | public function isPhoneType($type) |
|
264 | |||
265 | } |
||
266 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.