7a43667fef638fb983b19a25897be751ef06d3e3
[yaffs-website] / vendor / symfony / validator / Constraints / Regex.php
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
16 /**
17  * @Annotation
18  * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19  *
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class Regex extends Constraint
23 {
24     const REGEX_FAILED_ERROR = 'de1e3db3-5ed4-4941-aae4-59f3667cc3a3';
25
26     protected static $errorNames = array(
27         self::REGEX_FAILED_ERROR => 'REGEX_FAILED_ERROR',
28     );
29
30     public $message = 'This value is not valid.';
31     public $pattern;
32     public $htmlPattern;
33     public $match = true;
34
35     /**
36      * {@inheritdoc}
37      */
38     public function getDefaultOption()
39     {
40         return 'pattern';
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     public function getRequiredOptions()
47     {
48         return array('pattern');
49     }
50
51     /**
52      * Converts the htmlPattern to a suitable format for HTML5 pattern.
53      * Example: /^[a-z]+$/ would be converted to [a-z]+
54      * However, if options are specified, it cannot be converted.
55      *
56      * Pattern is also ignored if match=false since the pattern should
57      * then be reversed before application.
58      *
59      * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
60      *
61      * @return string|null
62      */
63     public function getHtmlPattern()
64     {
65         // If htmlPattern is specified, use it
66         if (null !== $this->htmlPattern) {
67             return empty($this->htmlPattern)
68                 ? null
69                 : $this->htmlPattern;
70         }
71
72         // Quit if delimiters not at very beginning/end (e.g. when options are passed)
73         if ($this->pattern[0] !== $this->pattern[strlen($this->pattern) - 1]) {
74             return;
75         }
76
77         $delimiter = $this->pattern[0];
78
79         // Unescape the delimiter
80         $pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1));
81
82         // If the pattern is inverted, we can simply wrap it in
83         // ((?!pattern).)*
84         if (!$this->match) {
85             return '((?!'.$pattern.').)*';
86         }
87
88         // If the pattern contains an or statement, wrap the pattern in
89         // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
90         if (false !== strpos($pattern, '|')) {
91             return '.*('.$pattern.').*';
92         }
93
94         // Trim leading ^, otherwise prepend .*
95         $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
96
97         // Trim trailing $, otherwise append .*
98         $pattern = '$' === $pattern[strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
99
100         return $pattern;
101     }
102 }