annotate vendor/symfony/validator/Constraints/Regex.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Validator\Constraints;
Chris@0 13
Chris@0 14 use Symfony\Component\Validator\Constraint;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * @Annotation
Chris@0 18 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
Chris@0 19 *
Chris@0 20 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 21 */
Chris@0 22 class Regex extends Constraint
Chris@0 23 {
Chris@0 24 const REGEX_FAILED_ERROR = 'de1e3db3-5ed4-4941-aae4-59f3667cc3a3';
Chris@0 25
Chris@17 26 protected static $errorNames = [
Chris@0 27 self::REGEX_FAILED_ERROR => 'REGEX_FAILED_ERROR',
Chris@17 28 ];
Chris@0 29
Chris@0 30 public $message = 'This value is not valid.';
Chris@0 31 public $pattern;
Chris@0 32 public $htmlPattern;
Chris@0 33 public $match = true;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 public function getDefaultOption()
Chris@0 39 {
Chris@0 40 return 'pattern';
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 public function getRequiredOptions()
Chris@0 47 {
Chris@17 48 return ['pattern'];
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Converts the htmlPattern to a suitable format for HTML5 pattern.
Chris@0 53 * Example: /^[a-z]+$/ would be converted to [a-z]+
Chris@0 54 * However, if options are specified, it cannot be converted.
Chris@0 55 *
Chris@0 56 * Pattern is also ignored if match=false since the pattern should
Chris@0 57 * then be reversed before application.
Chris@0 58 *
Chris@0 59 * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
Chris@0 60 *
Chris@0 61 * @return string|null
Chris@0 62 */
Chris@0 63 public function getHtmlPattern()
Chris@0 64 {
Chris@0 65 // If htmlPattern is specified, use it
Chris@0 66 if (null !== $this->htmlPattern) {
Chris@0 67 return empty($this->htmlPattern)
Chris@0 68 ? null
Chris@0 69 : $this->htmlPattern;
Chris@0 70 }
Chris@0 71
Chris@0 72 // Quit if delimiters not at very beginning/end (e.g. when options are passed)
Chris@17 73 if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
Chris@0 74 return;
Chris@0 75 }
Chris@0 76
Chris@0 77 $delimiter = $this->pattern[0];
Chris@0 78
Chris@0 79 // Unescape the delimiter
Chris@0 80 $pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1));
Chris@0 81
Chris@18 82 // If the pattern is inverted, we can wrap it in
Chris@0 83 // ((?!pattern).)*
Chris@0 84 if (!$this->match) {
Chris@0 85 return '((?!'.$pattern.').)*';
Chris@0 86 }
Chris@0 87
Chris@0 88 // If the pattern contains an or statement, wrap the pattern in
Chris@0 89 // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
Chris@0 90 if (false !== strpos($pattern, '|')) {
Chris@0 91 return '.*('.$pattern.').*';
Chris@0 92 }
Chris@0 93
Chris@0 94 // Trim leading ^, otherwise prepend .*
Chris@0 95 $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
Chris@0 96
Chris@0 97 // Trim trailing $, otherwise append .*
Chris@17 98 $pattern = '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
Chris@0 99
Chris@0 100 return $pattern;
Chris@0 101 }
Chris@0 102 }