comparison vendor/symfony/validator/Constraints/RegexValidator.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Validator\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17
18 /**
19 * Validates whether a value match or not given regexp pattern.
20 *
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 * @author Joseph Bielawski <stloyd@gmail.com>
23 */
24 class RegexValidator extends ConstraintValidator
25 {
26 /**
27 * {@inheritdoc}
28 */
29 public function validate($value, Constraint $constraint)
30 {
31 if (!$constraint instanceof Regex) {
32 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Regex');
33 }
34
35 if (null === $value || '' === $value) {
36 return;
37 }
38
39 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
40 throw new UnexpectedTypeException($value, 'string');
41 }
42
43 $value = (string) $value;
44
45 if ($constraint->match xor preg_match($constraint->pattern, $value)) {
46 $this->context->buildViolation($constraint->message)
47 ->setParameter('{{ value }}', $this->formatValue($value))
48 ->setCode(Regex::REGEX_FAILED_ERROR)
49 ->addViolation();
50 }
51 }
52 }