format_of.php 801 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php namespace System\Validation\Rules;
  2. use System\Validation\Nullable_Rule;
  3. class Format_Of extends Nullable_Rule {
  4. /**
  5. * The regular expression that will be used to validate the attribute.
  6. *
  7. * @var string
  8. */
  9. public $expression;
  10. /**
  11. * Evaluate the validity of an attribute.
  12. *
  13. * @param string $attribute
  14. * @param array $attributes
  15. * @return bool
  16. */
  17. public function check($attribute, $attributes)
  18. {
  19. if ( ! is_null($nullable = parent::check($attribute, $attributes)))
  20. {
  21. return $nullable;
  22. }
  23. return preg_match($this->expression, $attributes[$attribute]);
  24. }
  25. /**
  26. * Set the regular expression.
  27. *
  28. * @param string $expression
  29. * @return Format_Of
  30. */
  31. public function using($expression)
  32. {
  33. $this->expression = $expression;
  34. return $this;
  35. }
  36. }