1 | <?php namespace Propaganistas\LaravelPhone; |
||
10 | class PhoneValidator |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var \libphonenumber\PhoneNumberUtil |
||
15 | */ |
||
16 | protected $lib; |
||
17 | |||
18 | /** |
||
19 | * Dotted validator data. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $data; |
||
24 | |||
25 | /** |
||
26 | * Whether the country should be auto-detected. |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $autodetect = false; |
||
31 | |||
32 | /** |
||
33 | * Whether to allow lenient checking of numbers (i.e. landline without area codes). |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $lenient = false; |
||
38 | |||
39 | /** |
||
40 | * The field to use for country if not passed as a parameter. |
||
41 | * |
||
42 | * @var string|null |
||
43 | */ |
||
44 | protected $countryField = null; |
||
45 | |||
46 | /** |
||
47 | * Countries to validate against. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $countries = []; |
||
52 | |||
53 | /** |
||
54 | * Transformed phone number types to validate against. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $types = []; |
||
59 | |||
60 | /** |
||
61 | * PhoneValidator constructor. |
||
62 | */ |
||
63 | 20 | public function __construct(PhoneNumberUtil $lib) |
|
64 | { |
||
65 | 20 | $this->lib = $lib; |
|
66 | 18 | } |
|
67 | |||
68 | /** |
||
69 | * Validates a phone number. |
||
70 | * |
||
71 | * @param string $attribute |
||
72 | * @param mixed $value |
||
73 | * @param array $parameters |
||
74 | * @param object $validator |
||
75 | * @return bool |
||
76 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
77 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
78 | */ |
||
79 | 20 | public function validatePhone($attribute, $value, array $parameters, $validator) |
|
80 | { |
||
81 | 18 | $this->data = array_dot($validator->getData()); |
|
82 | |||
83 | 19 | $this->assignParameters($parameters); |
|
84 | |||
85 | 17 | $this->checkCountries($attribute); |
|
86 | |||
87 | // If autodetecting, let's first try without a country. |
||
88 | // Otherwise use provided countries as default. |
||
89 | 14 | if ($this->autodetect || ($this->lenient && empty($this->countries))) { |
|
90 | 4 | array_unshift($this->countries, null); |
|
91 | 4 | } |
|
92 | |||
93 | 14 | foreach ($this->countries as $country) { |
|
94 | 14 | if ($this->isValidNumber($value, $country)) { |
|
95 | 14 | return true; |
|
96 | } |
||
97 | 14 | } |
|
98 | |||
99 | // All specified country validations have failed. |
||
100 | 14 | return false; |
|
101 | 2 | } |
|
102 | |||
103 | /** |
||
104 | * Parses the supplied validator parameters. |
||
105 | * |
||
106 | * @param array $parameters |
||
107 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
108 | */ |
||
109 | 20 | protected function assignParameters(array $parameters) |
|
135 | |||
136 | /** |
||
137 | * Checks the detected countries. Overrides countries if a country field is present. |
||
138 | * When using a country field, we should validate to false if country is empty so no exception |
||
139 | * will be thrown. |
||
140 | * |
||
141 | * @param string $attribute |
||
142 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
143 | */ |
||
144 | 17 | protected function checkCountries($attribute) |
|
154 | |||
155 | /** |
||
156 | * Performs the actual validation of the phone number. |
||
157 | * |
||
158 | * @param mixed $number |
||
159 | * @param null $country |
||
160 | * @return bool |
||
161 | */ |
||
162 | 14 | protected function isValidNumber($number, $country = null) |
|
197 | |||
198 | /** |
||
199 | * Parses the supplied phone number types. |
||
200 | * |
||
201 | * @param array $types |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function parseTypes(array $types) |
||
218 | |||
219 | /** |
||
220 | * Checks if the given field is an actual input field. |
||
221 | * |
||
222 | * @param string $field |
||
223 | * @return bool |
||
224 | */ |
||
225 | 18 | public function isInputField($field) |
|
229 | |||
230 | /** |
||
231 | * Checks if the supplied string is a valid country code. |
||
232 | * |
||
233 | * @param string $country |
||
234 | * @return bool |
||
235 | */ |
||
236 | 12 | public function isPhoneCountry($country) |
|
240 | |||
241 | /** |
||
242 | * Checks if the supplied string is a valid phone number type. |
||
243 | * |
||
244 | * @param string $type |
||
245 | * @return bool |
||
246 | */ |
||
247 | 14 | public function isPhoneType($type) |
|
254 | |||
255 | } |
||
256 |