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.

40 lines
985 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. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class RegexValidator extends Validator
  12. {
  13. const NOT_MATCH = 'regex_not_match';
  14. protected $vars = array('regex');
  15. protected $templates = array(self::NOT_MATCH => '"%value%" does not match against regex "%regex%"');
  16. protected $regex;
  17. public function __construct($regex)
  18. {
  19. $this->regex = $regex;
  20. }
  21. public function isValid($value, $context = null)
  22. {
  23. $this->setValue($value);
  24. $status = preg_match($this->regex, $value);
  25. if ($status === false) {
  26. throw new GeneralException('Internal error matching regex "' . $this->regex . ' against value "' . $value . '"');
  27. }
  28. if (!$status) {
  29. $this->error();
  30. return false;
  31. }
  32. return true;
  33. }
  34. }