PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Validator/Constraints/RegexValidator.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 58 lines | 23 code | 9 blank | 26 comment | 6 complexity | 648f46e066ece050a9354de32765f281 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. /**
  15. * Validates whether a value match or not given regexp pattern
  16. *
  17. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  18. * @author Joseph Bielawski <stloyd@gmail.com>
  19. *
  20. * @api
  21. */
  22. class RegexValidator extends ConstraintValidator
  23. {
  24. /**
  25. * Checks if the passed value is valid.
  26. *
  27. * @param mixed $value The value that should be validated
  28. * @param Constraint $constraint The constraint for the validation
  29. *
  30. * @return Boolean Whether or not the value is valid
  31. *
  32. * @api
  33. */
  34. public function isValid($value, Constraint $constraint)
  35. {
  36. if (null === $value || '' === $value) {
  37. return true;
  38. }
  39. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  40. throw new UnexpectedTypeException($value, 'string');
  41. }
  42. $value = (string) $value;
  43. if ($constraint->match xor preg_match($constraint->pattern, $value)) {
  44. $this->setMessage($constraint->message, array('{{ value }}' => $value));
  45. return false;
  46. }
  47. return true;
  48. }
  49. }