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 | * Countries to validate against. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $countries = []; |
||
30 | |||
31 | /** |
||
32 | * Transformed phone number types to validate against. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $types = []; |
||
37 | |||
38 | /** |
||
39 | * PhoneValidator constructor. |
||
40 | */ |
||
41 | 21 | public function __construct() |
|
45 | |||
46 | /** |
||
47 | * Validates a phone number. |
||
48 | * |
||
49 | * @param string $attribute |
||
50 | * @param mixed $value |
||
51 | * @param array $parameters |
||
52 | * @param object $validator |
||
53 | * @return bool |
||
54 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
55 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
56 | */ |
||
57 | 21 | public function validatePhone($attribute, $value, array $parameters, $validator) |
|
78 | |||
79 | /** |
||
80 | * Checks the detected countries. Overrides countries if a country field is present. |
||
81 | * When using a country field, we should validate to false if country is empty so no exception |
||
82 | * will be thrown. |
||
83 | * |
||
84 | * @param $attribute |
||
85 | * @param $validator |
||
86 | * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
||
87 | */ |
||
88 | 18 | protected function checkCountries($attribute, $validator) |
|
99 | |||
100 | /** |
||
101 | * Performs the actual validation of the phone number. |
||
102 | * |
||
103 | * @param mixed $number |
||
104 | * @param null $country |
||
105 | * @return bool |
||
106 | */ |
||
107 | 15 | protected function isValidNumber($number, $country = null) |
|
134 | |||
135 | /** |
||
136 | * Parses the supplied validator parameters. |
||
137 | * |
||
138 | * @param array $parameters |
||
139 | * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
||
140 | */ |
||
141 | 21 | protected function assignParameters(array $parameters) |
|
161 | |||
162 | /** |
||
163 | * Parses the supplied phone number types. |
||
164 | * |
||
165 | * @param array $types |
||
166 | * @return array |
||
167 | */ |
||
168 | protected function parseTypes(array $types) |
||
182 | |||
183 | /** |
||
184 | * Checks if the supplied string is a valid country code using some arbitrary country validation. |
||
185 | * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'. |
||
186 | * |
||
187 | * @param string $country |
||
188 | * @return bool |
||
189 | */ |
||
190 | 15 | public function isPhoneCountry($country) |
|
194 | |||
195 | /** |
||
196 | * Checks if the supplied string is a valid phone number type. |
||
197 | * |
||
198 | * @param string $type |
||
199 | * @return bool |
||
200 | */ |
||
201 | 12 | public function isPhoneType($type) |
|
208 | |||
209 | /** |
||
210 | * Constructs the corresponding namespaced class constant for a phone number type. |
||
211 | * |
||
212 | * @param string $type |
||
213 | * @return string |
||
214 | */ |
||
215 | 12 | protected function constructPhoneTypeConstant($type) |
|
219 | |||
220 | } |
||
221 |