Browse Source

refactor the validation replacement code.

Taylor Otwell 13 years ago
parent
commit
ad24b08b29
1 changed files with 113 additions and 17 deletions
  1. 113 17
      laravel/validator.php

+ 113 - 17
laravel/validator.php

@@ -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,
-			// 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];
+			$message = $this->$replacer($message, $attribute, $rule, $parameters);
+		}
 
 
-			$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
-		// be replaced with a comma delimited list of the values.
-		elseif (in_array($rule, $this->inclusion_rules))
-		{
-			$message = str_replace(':values', implode(', ', $parameters), $message);
-		}
+	/**
+	 * Replace all place-holders for the size rule.
+	 *
+	 * @param  string  $message
+	 * @param  string  $attribute
+	 * @param  string  $rule
+	 * @param  array   $parameters
+	 * @return string
+	 */
+	protected function replace_size($message, $attribute, $rule, $parameters)
+	{
+		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);
 	}
 	}
 
 
 	/**
 	/**