Commit ad24b08b authored by Taylor Otwell's avatar Taylor Otwell

refactor the validation replacement code.

parent 554d390f
...@@ -714,28 +714,124 @@ class Validator { ...@@ -714,28 +714,124 @@ class Validator {
{ {
$message = str_replace(':attribute', $this->attribute($attribute), $message); $message = str_replace(':attribute', $this->attribute($attribute), $message);
if (in_array($rule, $this->size_rules)) if (method_exists($this, $replacer = 'replace_'.$rule))
{ {
// Even though every size rule will not have a place-holder for min, max, $message = $this->$replacer($message, $attribute, $rule, $parameters);
// and size, we will go ahead and make replacements for all of them just }
// for convenience. Except for "between", every replacement should be
// the first parameter in the array.
$max = ($rule == 'between') ? $parameters[1] : $parameters[0];
$replace = array($parameters[0], $parameters[0], $max); return $message;
}
$message = str_replace(array(':size', ':min', ':max'), $replace, $message); /**
* Replace all place-holders for the between rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_between($message, $attribute, $rule, $parameters)
{
return str_replace(array(':min', ':max'), $parameters, $message);
} }
// The :values place-holder is used for rules that accept a list of /**
// values, such as "in" and "not_in". The place-holder value will * Replace all place-holders for the size rule.
// be replaced with a comma delimited list of the values. *
elseif (in_array($rule, $this->inclusion_rules)) * @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_size($message, $attribute, $rule, $parameters)
{ {
$message = str_replace(':values', implode(', ', $parameters), $message); return str_replace(':size', $parameters[0], $message);
} }
return $message; /**
* Replace all place-holders for the min rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_min($message, $attribute, $rule, $parameters)
{
return str_replace(':min', $parameters[0], $message);
}
/**
* Replace all place-holders for the max rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_max($message, $attribute, $rule, $parameters)
{
return str_replace(':max', $parameters[0], $message);
}
/**
* Replace all place-holders for the in rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_in($message, $attribute, $rule, $parameters)
{
return str_replace(':values', implode(', ', $parameters), $message);
}
/**
* Replace all place-holders for the not_in rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_not_in($message, $attribute, $rule, $parameters)
{
return str_replace(':values', implode(', ', $parameters), $message);
}
/**
* Replace all place-holders for the same rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_same($message, $attribute, $rule, $parameters)
{
return str_replace(':other', $parameters[0], $message);
}
/**
* Replace all place-holders for the different rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replace_different($message, $attribute, $rule, $parameters)
{
return str_replace(':other', $parameters[0], $message);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment