2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2011-05-19 08:34:54 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
|
|
|
* @Annotation
|
|
|
|
*
|
2013-04-17 17:02:12 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*
|
2011-07-20 09:37:57 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2011-05-19 08:34:54 +01:00
|
|
|
class Regex extends Constraint
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-04-13 15:47:58 +01:00
|
|
|
public $message = 'This value is not valid.';
|
2010-06-24 10:24:08 +01:00
|
|
|
public $pattern;
|
2012-06-08 05:32:21 +01:00
|
|
|
public $htmlPattern = null;
|
2010-06-24 10:24:08 +01:00
|
|
|
public $match = true;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2011-03-30 22:18:15 +01:00
|
|
|
public function getDefaultOption()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
return 'pattern';
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
2011-03-30 22:18:15 +01:00
|
|
|
public function getRequiredOptions()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
return array('pattern');
|
|
|
|
}
|
2012-06-08 05:32:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns htmlPattern if exists or pattern is convertible.
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getHtmlPattern()
|
|
|
|
{
|
|
|
|
// If htmlPattern is specified, use it
|
|
|
|
if (null !== $this->htmlPattern) {
|
|
|
|
return empty($this->htmlPattern)
|
|
|
|
? null
|
|
|
|
: $this->htmlPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getNonDelimitedPattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-07 03:21:50 +00:00
|
|
|
* Converts the htmlPattern to a suitable format for HTML5 pattern.
|
2012-06-08 05:32:21 +01:00
|
|
|
* Example: /^[a-z]+$/ would be converted to [a-z]+
|
|
|
|
* However, if options are specified, it cannot be converted
|
2012-12-11 10:40:22 +00:00
|
|
|
*
|
2012-08-30 09:16:04 +01:00
|
|
|
* Pattern is also ignored if match=false since the pattern should
|
|
|
|
* then be reversed before application.
|
|
|
|
*
|
|
|
|
* @todo reverse pattern in case match=false as per issue #5307
|
2012-06-08 05:32:21 +01:00
|
|
|
*
|
|
|
|
* @link http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
private function getNonDelimitedPattern()
|
|
|
|
{
|
2012-08-30 09:16:04 +01:00
|
|
|
// If match = false, pattern should not be added to HTML5 validation
|
|
|
|
if (!$this->match) {
|
2014-04-16 08:15:58 +01:00
|
|
|
return;
|
2012-08-30 09:16:04 +01:00
|
|
|
}
|
2012-12-11 10:40:22 +00:00
|
|
|
|
2012-06-08 05:32:21 +01:00
|
|
|
if (preg_match('/^(.)(\^?)(.*?)(\$?)\1$/', $this->pattern, $matches)) {
|
|
|
|
$delimiter = $matches[1];
|
|
|
|
$start = empty($matches[2]) ? '.*' : '';
|
|
|
|
$pattern = $matches[3];
|
|
|
|
$end = empty($matches[4]) ? '.*' : '';
|
|
|
|
|
|
|
|
// Unescape the delimiter in pattern
|
2013-04-02 10:39:57 +01:00
|
|
|
$pattern = str_replace('\\'.$delimiter, $delimiter, $pattern);
|
2012-07-09 13:50:58 +01:00
|
|
|
|
2013-04-02 10:39:57 +01:00
|
|
|
return $start.$pattern.$end;
|
2012-06-08 05:32:21 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|