You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
952 B

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage validator
  7. * @since 2010-04-26
  8. */
  9. class RegexValidator extends \Majestic\Validator
  10. {
  11. const NOT_MATCH = 'regex_not_match';
  12. protected $vars = array('regex');
  13. protected $templates = array(self::NOT_MATCH => '"%value%" does not match against regex "%regex%"');
  14. protected $regex;
  15. public function __construct($regex)
  16. {
  17. $this->regex = $regex;
  18. }
  19. public function isValid($value, $context = null)
  20. {
  21. $this->setValue($value);
  22. $status = preg_match($this->regex, $value);
  23. if ($status === false) {
  24. throw new GeneralException('Internal error matching regex "' . $this->regex . ' against value "' . $value . '"');
  25. }
  26. if (!$status) {
  27. $this->error();
  28. return false;
  29. }
  30. return true;
  31. }
  32. }