Skip to content

Commit

Permalink
Defer input field checking (fixes #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas committed May 20, 2016
1 parent fbb0e08 commit 5fe3af2
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/PhoneValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -237,7 +236,7 @@ public function isInputField($field)
*/
public function isPhoneCountry($country)
{
return ISO3166::isValid($country);
return ISO3166::isValid(strtoupper($country));
}

/**
Expand All @@ -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));
}

}

0 comments on commit 5fe3af2

Please sign in to comment.