@@ -9,206 +9,206 @@ |
||
9 | 9 | class PhoneValidator |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * The phone number attribute. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $attribute; |
|
18 | - |
|
19 | - /** |
|
20 | - * Data from the validator instance. |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $data; |
|
25 | - |
|
26 | - /** |
|
27 | - * Countries to validate against. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $allowedCountries = []; |
|
32 | - |
|
33 | - /** |
|
34 | - * Untransformed phone number types to validate against. |
|
35 | - * |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $untransformedTypes = []; |
|
39 | - |
|
40 | - /** |
|
41 | - * Transformed phone number types to validate against. |
|
42 | - * |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - protected $allowedTypes = []; |
|
46 | - |
|
47 | - /** |
|
48 | - * Supplied validator parameters. |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $parameters; |
|
53 | - |
|
54 | - /** |
|
55 | - * Validates a phone number. |
|
56 | - * |
|
57 | - * @param string $attribute |
|
58 | - * @param mixed $value |
|
59 | - * @param array $parameters |
|
60 | - * @param object $validator |
|
61 | - * @return bool |
|
62 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
63 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
64 | - */ |
|
65 | - public function validatePhone($attribute, $value, array $parameters, $validator) |
|
66 | - { |
|
67 | - $this->attribute = $attribute; |
|
68 | - $this->data = $validator->getData(); |
|
69 | - $this->parameters = array_map('strtoupper', $parameters); |
|
70 | - |
|
71 | - $this->determineCountries(); |
|
72 | - $this->determineTypes(); |
|
73 | - $this->checkLeftoverParameters(); |
|
74 | - |
|
75 | - $phoneUtil = PhoneNumberUtil::getInstance(); |
|
76 | - |
|
77 | - // Perform validation. |
|
78 | - foreach ($this->allowedCountries as $country) { |
|
79 | - try { |
|
80 | - // For default countries or country field, the following throws NumberParseException if |
|
81 | - // not parsed correctly against the supplied country. |
|
82 | - // For automatic detection: tries to discover the country code using from the number itself. |
|
83 | - $phoneProto = $phoneUtil->parse($value, $country); |
|
84 | - |
|
85 | - // For automatic detection, the number should have a country code. |
|
86 | - // Check if type is allowed. |
|
87 | - if ($phoneProto->hasCountryCode() && empty($this->allowedTypes) || in_array($phoneUtil->getNumberType($phoneProto), |
|
88 | - $this->allowedTypes) |
|
89 | - ) { |
|
90 | - |
|
91 | - // Automatic detection: |
|
92 | - if ($country == 'ZZ') { |
|
93 | - // Validate if the international phone number is valid for its contained country. |
|
94 | - return $phoneUtil->isValidNumber($phoneProto); |
|
95 | - } |
|
96 | - |
|
97 | - // Validate number against the specified country. Return only if success. |
|
98 | - // If failure, continue loop to next specfied country |
|
99 | - $success = $phoneUtil->isValidNumberForRegion($phoneProto, $country); |
|
100 | - |
|
101 | - if ($success) { |
|
102 | - return true; |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - } catch (NumberParseException $e) { |
|
107 | - // Proceed to default validation error. |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - // All specified country validations have failed. |
|
112 | - return false; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Checks if the supplied string is a valid country code using some arbitrary country validation. |
|
117 | - * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'. |
|
118 | - * |
|
119 | - * @param string $country |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public function isPhoneCountry($country) |
|
123 | - { |
|
124 | - return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country) && $country != 'ZZ'); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Checks if the supplied string is a valid phone number type. |
|
129 | - * |
|
130 | - * @param string $type |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function isPhoneType($type) |
|
134 | - { |
|
135 | - // Legacy support. |
|
136 | - $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type); |
|
137 | - |
|
138 | - return defined($this->constructPhoneTypeConstant($type)); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Constructs the corresponding namespaced class constant for a phone number type. |
|
143 | - * |
|
144 | - * @param string $type |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - protected function constructPhoneTypeConstant($type) |
|
148 | - { |
|
149 | - return '\libphonenumber\PhoneNumberType::' . $type; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Sets the countries to validate against. |
|
154 | - * |
|
155 | - * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
156 | - */ |
|
157 | - protected function determineCountries() |
|
158 | - { |
|
159 | - // Check for the existence of a country field. |
|
160 | - $field = $this->attribute . '_country'; |
|
161 | - if (isset($this->data[$field])) { |
|
162 | - $this->allowedCountries = ($this->isPhoneCountry($this->data[$field])) ? [$this->data[$field]] : []; |
|
163 | - // No exception should be thrown since empty country fields should validate to false. |
|
164 | - } // Or if we need to parse for automatic detection. |
|
165 | - elseif (in_array('AUTO', $this->parameters)) { |
|
166 | - $this->allowedCountries = ['ZZ']; |
|
167 | - } // Else use the supplied parameters. |
|
168 | - else { |
|
169 | - $this->allowedCountries = array_filter($this->parameters, function ($item) { |
|
170 | - return $this->isPhoneCountry($item); |
|
171 | - }); |
|
172 | - |
|
173 | - if (empty($this->allowedCountries)) { |
|
174 | - throw new NoValidCountryFoundException; |
|
175 | - } |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Sets the phone number types to validate against. |
|
181 | - */ |
|
182 | - protected function determineTypes() |
|
183 | - { |
|
184 | - // Get phone types. |
|
185 | - $this->untransformedTypes = array_filter($this->parameters, function ($item) { |
|
186 | - return $this->isPhoneType($item); |
|
187 | - }); |
|
188 | - |
|
189 | - // Transform valid types to their namespaced class constant. |
|
190 | - $this->allowedTypes = array_map(function ($item) { |
|
191 | - return constant($this->constructPhoneTypeConstant($item)); |
|
192 | - }, $this->untransformedTypes); |
|
193 | - |
|
194 | - // Add in the unsure number type if applicable. |
|
195 | - if (array_intersect(['FIXED_LINE', 'MOBILE'], $this->parameters)) { |
|
196 | - $this->allowedTypes[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Checks for parameter leftovers to force developers to write proper code. |
|
202 | - * |
|
203 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
204 | - */ |
|
205 | - protected function checkLeftoverParameters() |
|
206 | - { |
|
207 | - // Remove the automatic detection option if applicable. |
|
208 | - $leftovers = array_diff($this->parameters, $this->allowedCountries, $this->untransformedTypes, array('AUTO')); |
|
209 | - if (!empty($leftovers)) { |
|
210 | - throw new InvalidParameterException(implode(', ', $leftovers)); |
|
211 | - } |
|
212 | - } |
|
12 | + /** |
|
13 | + * The phone number attribute. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $attribute; |
|
18 | + |
|
19 | + /** |
|
20 | + * Data from the validator instance. |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $data; |
|
25 | + |
|
26 | + /** |
|
27 | + * Countries to validate against. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $allowedCountries = []; |
|
32 | + |
|
33 | + /** |
|
34 | + * Untransformed phone number types to validate against. |
|
35 | + * |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $untransformedTypes = []; |
|
39 | + |
|
40 | + /** |
|
41 | + * Transformed phone number types to validate against. |
|
42 | + * |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + protected $allowedTypes = []; |
|
46 | + |
|
47 | + /** |
|
48 | + * Supplied validator parameters. |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $parameters; |
|
53 | + |
|
54 | + /** |
|
55 | + * Validates a phone number. |
|
56 | + * |
|
57 | + * @param string $attribute |
|
58 | + * @param mixed $value |
|
59 | + * @param array $parameters |
|
60 | + * @param object $validator |
|
61 | + * @return bool |
|
62 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
63 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
64 | + */ |
|
65 | + public function validatePhone($attribute, $value, array $parameters, $validator) |
|
66 | + { |
|
67 | + $this->attribute = $attribute; |
|
68 | + $this->data = $validator->getData(); |
|
69 | + $this->parameters = array_map('strtoupper', $parameters); |
|
70 | + |
|
71 | + $this->determineCountries(); |
|
72 | + $this->determineTypes(); |
|
73 | + $this->checkLeftoverParameters(); |
|
74 | + |
|
75 | + $phoneUtil = PhoneNumberUtil::getInstance(); |
|
76 | + |
|
77 | + // Perform validation. |
|
78 | + foreach ($this->allowedCountries as $country) { |
|
79 | + try { |
|
80 | + // For default countries or country field, the following throws NumberParseException if |
|
81 | + // not parsed correctly against the supplied country. |
|
82 | + // For automatic detection: tries to discover the country code using from the number itself. |
|
83 | + $phoneProto = $phoneUtil->parse($value, $country); |
|
84 | + |
|
85 | + // For automatic detection, the number should have a country code. |
|
86 | + // Check if type is allowed. |
|
87 | + if ($phoneProto->hasCountryCode() && empty($this->allowedTypes) || in_array($phoneUtil->getNumberType($phoneProto), |
|
88 | + $this->allowedTypes) |
|
89 | + ) { |
|
90 | + |
|
91 | + // Automatic detection: |
|
92 | + if ($country == 'ZZ') { |
|
93 | + // Validate if the international phone number is valid for its contained country. |
|
94 | + return $phoneUtil->isValidNumber($phoneProto); |
|
95 | + } |
|
96 | + |
|
97 | + // Validate number against the specified country. Return only if success. |
|
98 | + // If failure, continue loop to next specfied country |
|
99 | + $success = $phoneUtil->isValidNumberForRegion($phoneProto, $country); |
|
100 | + |
|
101 | + if ($success) { |
|
102 | + return true; |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + } catch (NumberParseException $e) { |
|
107 | + // Proceed to default validation error. |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + // All specified country validations have failed. |
|
112 | + return false; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Checks if the supplied string is a valid country code using some arbitrary country validation. |
|
117 | + * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'. |
|
118 | + * |
|
119 | + * @param string $country |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public function isPhoneCountry($country) |
|
123 | + { |
|
124 | + return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country) && $country != 'ZZ'); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Checks if the supplied string is a valid phone number type. |
|
129 | + * |
|
130 | + * @param string $type |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function isPhoneType($type) |
|
134 | + { |
|
135 | + // Legacy support. |
|
136 | + $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type); |
|
137 | + |
|
138 | + return defined($this->constructPhoneTypeConstant($type)); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Constructs the corresponding namespaced class constant for a phone number type. |
|
143 | + * |
|
144 | + * @param string $type |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + protected function constructPhoneTypeConstant($type) |
|
148 | + { |
|
149 | + return '\libphonenumber\PhoneNumberType::' . $type; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Sets the countries to validate against. |
|
154 | + * |
|
155 | + * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException |
|
156 | + */ |
|
157 | + protected function determineCountries() |
|
158 | + { |
|
159 | + // Check for the existence of a country field. |
|
160 | + $field = $this->attribute . '_country'; |
|
161 | + if (isset($this->data[$field])) { |
|
162 | + $this->allowedCountries = ($this->isPhoneCountry($this->data[$field])) ? [$this->data[$field]] : []; |
|
163 | + // No exception should be thrown since empty country fields should validate to false. |
|
164 | + } // Or if we need to parse for automatic detection. |
|
165 | + elseif (in_array('AUTO', $this->parameters)) { |
|
166 | + $this->allowedCountries = ['ZZ']; |
|
167 | + } // Else use the supplied parameters. |
|
168 | + else { |
|
169 | + $this->allowedCountries = array_filter($this->parameters, function ($item) { |
|
170 | + return $this->isPhoneCountry($item); |
|
171 | + }); |
|
172 | + |
|
173 | + if (empty($this->allowedCountries)) { |
|
174 | + throw new NoValidCountryFoundException; |
|
175 | + } |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Sets the phone number types to validate against. |
|
181 | + */ |
|
182 | + protected function determineTypes() |
|
183 | + { |
|
184 | + // Get phone types. |
|
185 | + $this->untransformedTypes = array_filter($this->parameters, function ($item) { |
|
186 | + return $this->isPhoneType($item); |
|
187 | + }); |
|
188 | + |
|
189 | + // Transform valid types to their namespaced class constant. |
|
190 | + $this->allowedTypes = array_map(function ($item) { |
|
191 | + return constant($this->constructPhoneTypeConstant($item)); |
|
192 | + }, $this->untransformedTypes); |
|
193 | + |
|
194 | + // Add in the unsure number type if applicable. |
|
195 | + if (array_intersect(['FIXED_LINE', 'MOBILE'], $this->parameters)) { |
|
196 | + $this->allowedTypes[] = PhoneNumberType::FIXED_LINE_OR_MOBILE; |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Checks for parameter leftovers to force developers to write proper code. |
|
202 | + * |
|
203 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException |
|
204 | + */ |
|
205 | + protected function checkLeftoverParameters() |
|
206 | + { |
|
207 | + // Remove the automatic detection option if applicable. |
|
208 | + $leftovers = array_diff($this->parameters, $this->allowedCountries, $this->untransformedTypes, array('AUTO')); |
|
209 | + if (!empty($leftovers)) { |
|
210 | + throw new InvalidParameterException(implode(', ', $leftovers)); |
|
211 | + } |
|
212 | + } |
|
213 | 213 | |
214 | 214 | } |