rule.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php namespace System\Validation;
  2. use System\Lang;
  3. abstract class Rule {
  4. /**
  5. * The attributes being validated by the rule.
  6. *
  7. * @var array
  8. */
  9. public $attributes;
  10. /**
  11. * The validation error message.
  12. *
  13. * @var string
  14. */
  15. public $message;
  16. /**
  17. * The error type. This is used for rules that have more than
  18. * one type of error such as Size_Of and Upload_Of.
  19. *
  20. * @var string
  21. */
  22. public $error;
  23. /**
  24. * Create a new validation Rule instance.
  25. *
  26. * @param array $attributes
  27. * @return void
  28. */
  29. public function __construct($attributes)
  30. {
  31. $this->attributes = $attributes;
  32. }
  33. /**
  34. * Run the validation rule.
  35. *
  36. * @param array $attributes
  37. * @param Error_Collector $errors
  38. * @return void
  39. */
  40. public function validate($attributes, $errors)
  41. {
  42. foreach ($this->attributes as $attribute)
  43. {
  44. $this->error = null;
  45. if ( ! $this->check($attribute, $attributes))
  46. {
  47. $errors->add($attribute, Message::get($this, $attribute));
  48. }
  49. }
  50. }
  51. /**
  52. * Set the validation error message.
  53. *
  54. * @param string $message
  55. * @return Rule
  56. */
  57. public function message($message)
  58. {
  59. $this->message = $message;
  60. return $this;
  61. }
  62. }