Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Constraints; Chris@0: Chris@0: use Symfony\Component\Validator\Constraint; Chris@0: Chris@0: /** Chris@0: * @Annotation Chris@0: * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class Regex extends Constraint Chris@0: { Chris@0: const REGEX_FAILED_ERROR = 'de1e3db3-5ed4-4941-aae4-59f3667cc3a3'; Chris@0: Chris@17: protected static $errorNames = [ Chris@0: self::REGEX_FAILED_ERROR => 'REGEX_FAILED_ERROR', Chris@17: ]; Chris@0: Chris@0: public $message = 'This value is not valid.'; Chris@0: public $pattern; Chris@0: public $htmlPattern; Chris@0: public $match = true; Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDefaultOption() Chris@0: { Chris@0: return 'pattern'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getRequiredOptions() Chris@0: { Chris@17: return ['pattern']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts the htmlPattern to a suitable format for HTML5 pattern. Chris@0: * Example: /^[a-z]+$/ would be converted to [a-z]+ Chris@0: * However, if options are specified, it cannot be converted. Chris@0: * Chris@0: * Pattern is also ignored if match=false since the pattern should Chris@0: * then be reversed before application. Chris@0: * Chris@0: * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute Chris@0: * Chris@0: * @return string|null Chris@0: */ Chris@0: public function getHtmlPattern() Chris@0: { Chris@0: // If htmlPattern is specified, use it Chris@0: if (null !== $this->htmlPattern) { Chris@0: return empty($this->htmlPattern) Chris@0: ? null Chris@0: : $this->htmlPattern; Chris@0: } Chris@0: Chris@0: // Quit if delimiters not at very beginning/end (e.g. when options are passed) Chris@17: if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $delimiter = $this->pattern[0]; Chris@0: Chris@0: // Unescape the delimiter Chris@0: $pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1)); Chris@0: Chris@18: // If the pattern is inverted, we can wrap it in Chris@0: // ((?!pattern).)* Chris@0: if (!$this->match) { Chris@0: return '((?!'.$pattern.').)*'; Chris@0: } Chris@0: Chris@0: // If the pattern contains an or statement, wrap the pattern in Chris@0: // .*(pattern).* and quit. Otherwise we'd need to parse the pattern Chris@0: if (false !== strpos($pattern, '|')) { Chris@0: return '.*('.$pattern.').*'; Chris@0: } Chris@0: Chris@0: // Trim leading ^, otherwise prepend .* Chris@0: $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern; Chris@0: Chris@0: // Trim trailing $, otherwise append .* Chris@17: $pattern = '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*'; Chris@0: Chris@0: return $pattern; Chris@0: } Chris@0: }