From 5fe3af2794433fa721c6bfdc0fffc7faebc56754 Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Fri, 20 May 2016 12:27:25 +0200 Subject: [PATCH] Defer input field checking (fixes #30) --- src/PhoneValidator.php | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/PhoneValidator.php b/src/PhoneValidator.php index 88a5c44..c4b7a4d 100644 --- a/src/PhoneValidator.php +++ b/src/PhoneValidator.php @@ -111,23 +111,22 @@ protected function assignParameters(array $parameters) $types = array(); foreach ($parameters as $parameter) { - if ($this->isInputField($parameter)) { + // First check if the parameter is some phone type configuration. + if ($this->isPhoneType($parameter)) { + $types[] = strtoupper($parameter); + } elseif ($this->isPhoneCountry($parameter)) { + $this->countries[] = strtoupper($parameter); + } elseif ($parameter == 'AUTO') { + $this->autodetect = true; + } elseif ($parameter == 'LENIENT') { + $this->lenient = true; + } + // Lastly check if it is an input field containing the country. + elseif ($this->isInputField($parameter)) { $this->countryField = $parameter; } else { - $parameter = strtoupper($parameter); - - if ($this->isPhoneCountry($parameter)) { - $this->countries[] = $parameter; - } elseif ($this->isPhoneType($parameter)) { - $types[] = $parameter; - } elseif ($parameter == 'AUTO') { - $this->autodetect = true; - } elseif ($parameter == 'LENIENT') { - $this->lenient = true; - } else { - // Force developers to write proper code. - throw new InvalidParameterException($parameter); - } + // Force developers to write proper code. + throw new InvalidParameterException($parameter); } } @@ -226,7 +225,7 @@ protected function parseTypes(array $types) */ public function isInputField($field) { - return ! is_null(array_get($this->data, $field)); + return !is_null(array_get($this->data, $field)); } /** @@ -237,7 +236,7 @@ public function isInputField($field) */ public function isPhoneCountry($country) { - return ISO3166::isValid($country); + return ISO3166::isValid(strtoupper($country)); } /** @@ -251,7 +250,7 @@ public function isPhoneType($type) // Legacy support. $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type); - return defined('\libphonenumber\PhoneNumberType::' . $type); + return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type)); } }