This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/Constraints/Regex.php
Fabien Potencier 72c169e8a1 Merge branch '2.5' into 2.6
* 2.5:
  [2.3] CS And DocBlock Fixes
  [2.3] CS Fixes
  [FrameworkBundle] Fixed Translation loader and update translation command.
  [Console] remove « use » statement for PHP built-in exception classes.
  [SecurityBundle] adds unit tests suite for SecurityDataCollector class.

Conflicts:
	src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php
	src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php
	src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
	src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
	src/Symfony/Component/Form/Form.php
	src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
	src/Symfony/Component/OptionsResolver/Options.php
	src/Symfony/Component/OptionsResolver/OptionsResolver.php
	src/Symfony/Component/Process/ProcessPipes.php
	src/Symfony/Component/Stopwatch/Stopwatch.php
	src/Symfony/Component/Translation/Loader/XliffFileLoader.php
	src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php
2014-12-22 17:44:09 +01:00

99 lines
2.6 KiB
PHP

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