rule.php 1.17 KB
Newer Older
1 2
<?php namespace System\Validation;

3 4
use System\Lang;

5 6 7
abstract class Rule {

	/**
8
	 * The attributes being validated by the rule.
9 10 11 12 13 14 15 16 17 18 19 20 21
	 *
	 * @var array
	 */
	public $attributes;

	/**
	 * The validation error message.
	 *
	 * @var string
	 */
	public $message;

	/**
22 23 24 25 26
	 * The error type. This is used for rules that have more than
	 * one type of error such as Size_Of and Upload_Of.
	 *
	 * @var string
	 */
27
	public $error;
28 29

	/**
30 31 32 33 34 35 36 37 38 39 40 41 42
	 * Create a new validation Rule instance.
	 *
	 * @param  array      $attributes
	 * @return void
	 */
	public function __construct($attributes)
	{
		$this->attributes = $attributes;
	}

	/**
	 * Run the validation rule.
	 *
43 44
	 * @param  array            $attributes
	 * @param  Error_Collector  $errors
45 46
	 * @return void
	 */
47
	public function validate($attributes, $errors)
48 49 50
	{
		foreach ($this->attributes as $attribute)
		{
51 52
			$this->error = null;

53 54
			if ( ! $this->check($attribute, $attributes))
			{
55
				$errors->add($attribute, Message::get($this, $attribute));
56
			}
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		}
	}

	/**
	 * Set the validation error message.
	 *
	 * @param  string  $message
	 * @return Rule
	 */
	public function message($message)
	{
		$this->message = $message;
		return $this;
	}

}