1 | <?php namespace Propaganistas\LaravelPhone; |
||
9 | class PhoneValidator |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var \libphonenumber\PhoneNumberUtil |
||
14 | */ |
||
15 | protected $lib; |
||
16 | |||
17 | /** |
||
18 | * Dotted validator data. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $data; |
||
23 | |||
24 | /** |
||
25 | * Whether the country should be auto-detected. |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $autodetect = false; |
||
30 | |||
31 | /** |
||
32 | * Whether to allow lenient checking of numbers (i.e. landline without area codes). |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $lenient = false; |
||
37 | |||
38 | /** |
||
39 | * The field to use for country if not passed as a parameter. |
||
40 | * |
||
41 | * @var string|null |
||
42 | */ |
||
43 | protected $countryField = null; |
||
44 | |||
45 | /** |
||
46 | * Countries to validate against. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $countries = []; |
||
51 | |||
52 | /** |
||
53 | * Transformed phone number types to validate against. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $types = []; |
||
58 | |||
59 | /** |
||
60 | * PhoneValidator constructor. |
||
61 | */ |
||
62 | 27 | public function __construct(PhoneNumberUtil $lib) |
|
66 | |||
67 | /** |
||
68 | * Validates a phone number. |
||
69 | * |
||
70 | * @param string $attribute |
||
71 | * @param mixed $value |
||
72 | * @param array $parameters |
||
73 | * @param object $validator |
||
74 | * @return bool |
||
75 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
76 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
77 | */ |
||
78 | 27 | public function validatePhone($attribute, $value, array $parameters, $validator) |
|
101 | |||
102 | /** |
||
103 | * Parses the supplied validator parameters. |
||
104 | * |
||
105 | * @param array $parameters |
||
106 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
107 | */ |
||
108 | 27 | protected function assignParameters(array $parameters) |
|
136 | |||
137 | /** |
||
138 | * Checks the detected countries. Overrides countries if a country field is present. |
||
139 | * When using a country field, we should validate to false if country is empty so no exception |
||
140 | * will be thrown. |
||
141 | * |
||
142 | * @param string $attribute |
||
143 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
144 | */ |
||
145 | 24 | protected function checkCountries($attribute) |
|
155 | |||
156 | /** |
||
157 | * Performs the actual validation of the phone number. |
||
158 | * |
||
159 | * @param mixed $number |
||
160 | * @param null $country |
||
161 | * @return bool |
||
162 | */ |
||
163 | 21 | protected function isValidNumber($number, $country = null) |
|
198 | |||
199 | /** |
||
200 | * Parses the supplied phone number types. |
||
201 | * |
||
202 | * @param array $types |
||
203 | * @return array |
||
204 | */ |
||
205 | protected function parseTypes(array $types) |
||
219 | |||
220 | /** |
||
221 | * Checks if the given field is an actual input field and returns the value if applicable. |
||
222 | * Null otherwise. |
||
223 | * |
||
224 | * @param string $field |
||
225 | * @return mixed|null |
||
226 | */ |
||
227 | 27 | public function isInputField($field) |
|
231 | |||
232 | /** |
||
233 | * Checks if the supplied string is a valid country code using some arbitrary country validation. |
||
234 | * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'. |
||
235 | * |
||
236 | * @param string $country |
||
237 | * @return bool |
||
238 | */ |
||
239 | 21 | public function isPhoneCountry($country) |
|
243 | |||
244 | /** |
||
245 | * Checks if the supplied string is a valid phone number type. |
||
246 | * |
||
247 | * @param string $type |
||
248 | * @return bool |
||
249 | */ |
||
250 | 18 | public function isPhoneType($type) |
|
257 | |||
258 | } |
||
259 |