Skip to content

Commit 6e521cd

Browse files
committed
Merge branch '2.5'
* 2.5: added missing files [TwigBundle] added a test Indicate which file was being parsed if an exception is thrown while running translation:debug [ClassLoader] Cast $useIncludePath property to boolean [HttpFoundation] Minor spelling fix in PHPDocs improve error message for multiple documents Remove aligned '=>' and '=' [Session] remove invalid workaround in session regenerate [Kernel] ensure session is saved before sending response [Routing] serialize the compiled route to speed things up [Form] Fixed usage of "name" variable in form_start block [Validator] Fixed Regex::getHtmlPattern() to work with complex and negated patterns [DependencyInjection] use inheritdoc for loaders [Config] fix filelocator with empty name [Form] fix form handling with unconventional request methods like OPTIONS CSRF warning docs on Request::enableHttpMethodParameterOverride() Conflicts: src/Symfony/Component/Console/Helper/ProgressBar.php
2 parents 93485c1 + 0977f6f commit 6e521cd

File tree

4 files changed

+119
-97
lines changed

4 files changed

+119
-97
lines changed

Constraints/Regex.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,6 @@ public function getRequiredOptions()
4444
return array('pattern');
4545
}
4646

47-
/**
48-
* Returns htmlPattern if exists or pattern is convertible.
49-
*
50-
* @return string|null
51-
*/
52-
public function getHtmlPattern()
53-
{
54-
// If htmlPattern is specified, use it
55-
if (null !== $this->htmlPattern) {
56-
return empty($this->htmlPattern)
57-
? null
58-
: $this->htmlPattern;
59-
}
60-
61-
return $this->getNonDelimitedPattern();
62-
}
63-
6447
/**
6548
* Converts the htmlPattern to a suitable format for HTML5 pattern.
6649
* Example: /^[a-z]+$/ would be converted to [a-z]+
@@ -69,29 +52,47 @@ public function getHtmlPattern()
6952
* Pattern is also ignored if match=false since the pattern should
7053
* then be reversed before application.
7154
*
72-
* @todo reverse pattern in case match=false as per issue #5307
73-
*
7455
* @link http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
7556
*
7657
* @return string|null
7758
*/
78-
private function getNonDelimitedPattern()
59+
public function getHtmlPattern()
7960
{
80-
// If match = false, pattern should not be added to HTML5 validation
81-
if (!$this->match) {
61+
// If htmlPattern is specified, use it
62+
if (null !== $this->htmlPattern) {
63+
return empty($this->htmlPattern)
64+
? null
65+
: $this->htmlPattern;
66+
}
67+
68+
// Quit if delimiters not at very beginning/end (e.g. when options are passed)
69+
if ($this->pattern[0] !== $this->pattern[strlen($this->pattern) - 1]) {
8270
return;
8371
}
8472

85-
if (preg_match('/^(.)(\^?)(.*?)(\$?)\1$/', $this->pattern, $matches)) {
86-
$delimiter = $matches[1];
87-
$start = empty($matches[2]) ? '.*' : '';
88-
$pattern = $matches[3];
89-
$end = empty($matches[4]) ? '.*' : '';
73+
$delimiter = $this->pattern[0];
74+
75+
// Unescape the delimiter
76+
$pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1));
9077

91-
// Unescape the delimiter in pattern
92-
$pattern = str_replace('\\'.$delimiter, $delimiter, $pattern);
78+
// If the pattern is inverted, we can simply wrap it in
79+
// ((?!pattern).)*
80+
if (!$this->match) {
81+
return '((?!'.$pattern.').)*';
82+
}
9383

94-
return $start.$pattern.$end;
84+
// If the pattern contains an or statement, wrap the pattern in
85+
// .*(pattern).* and quit. Otherwise we'd need to parse the pattern
86+
if (false !== strpos($pattern, '|')) {
87+
return '.*('.$pattern.').*';
9588
}
89+
90+
// Trim leading ^, otherwise prepend .*
91+
$pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
92+
93+
// Trim trailing $, otherwise append .*
94+
$pattern = '$' === $pattern[strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
95+
96+
return $pattern;
9697
}
9798
}

Tests/Constraints/RegexTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Constraints;
13+
14+
use Symfony\Component\Validator\Constraints\Regex;
15+
16+
/**
17+
* @author Bernhard Schussek <[email protected]>
18+
*/
19+
class RegexTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function testConstraintGetDefaultOption()
22+
{
23+
$constraint = new Regex('/^[0-9]+$/');
24+
25+
$this->assertSame('/^[0-9]+$/', $constraint->pattern);
26+
}
27+
28+
public function provideHtmlPatterns()
29+
{
30+
return array(
31+
// HTML5 wraps the pattern in ^(?:pattern)$
32+
array('/^[0-9]+$/', '[0-9]+'),
33+
array('/[0-9]+$/', '.*[0-9]+'),
34+
array('/^[0-9]+/', '[0-9]+.*'),
35+
array('/[0-9]+/', '.*[0-9]+.*'),
36+
// We need a smart way to allow matching of patterns that contain
37+
// ^ and $ at various sub-clauses of an or-clause
38+
// .*(pattern).* seems to work correctly
39+
array('/[0-9]$|[a-z]+/', '.*([0-9]$|[a-z]+).*'),
40+
array('/[0-9]$|^[a-z]+/', '.*([0-9]$|^[a-z]+).*'),
41+
array('/^[0-9]|[a-z]+$/', '.*(^[0-9]|[a-z]+$).*'),
42+
// Unescape escaped delimiters
43+
array('/^[0-9]+\/$/', '[0-9]+/'),
44+
array('#^[0-9]+\#$#', '[0-9]+#'),
45+
// Cannot be converted
46+
array('/^[0-9]+$/i', null),
47+
48+
// Inverse matches are simple, just wrap in
49+
// ((?!pattern).)*
50+
array('/^[0-9]+$/', '((?!^[0-9]+$).)*', false),
51+
array('/[0-9]+$/', '((?![0-9]+$).)*', false),
52+
array('/^[0-9]+/', '((?!^[0-9]+).)*', false),
53+
array('/[0-9]+/', '((?![0-9]+).)*', false),
54+
array('/[0-9]$|[a-z]+/', '((?![0-9]$|[a-z]+).)*', false),
55+
array('/[0-9]$|^[a-z]+/', '((?![0-9]$|^[a-z]+).)*', false),
56+
array('/^[0-9]|[a-z]+$/', '((?!^[0-9]|[a-z]+$).)*', false),
57+
array('/^[0-9]+\/$/', '((?!^[0-9]+/$).)*', false),
58+
array('#^[0-9]+\#$#', '((?!^[0-9]+#$).)*', false),
59+
array('/^[0-9]+$/i', null, false),
60+
);
61+
}
62+
63+
/**
64+
* @dataProvider provideHtmlPatterns
65+
*/
66+
public function testGetHtmlPattern($pattern, $htmlPattern, $match = true)
67+
{
68+
$constraint = new Regex(array(
69+
'pattern' => $pattern,
70+
'match' => $match,
71+
));
72+
73+
$this->assertSame($pattern, $constraint->pattern);
74+
$this->assertSame($htmlPattern, $constraint->getHtmlPattern());
75+
}
76+
77+
public function testGetCustomHtmlPattern()
78+
{
79+
$constraint = new Regex(array(
80+
'pattern' => '((?![0-9]$|[a-z]+).)*',
81+
'htmlPattern' => 'foobar',
82+
));
83+
84+
$this->assertSame('((?![0-9]$|[a-z]+).)*', $constraint->pattern);
85+
$this->assertSame('foobar', $constraint->getHtmlPattern());
86+
}
87+
}

Tests/Constraints/RegexValidatorTest.php

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -94,70 +94,4 @@ public function getInvalidValues()
9494
array('090foo'),
9595
);
9696
}
97-
98-
public function testConstraintGetDefaultOption()
99-
{
100-
$constraint = new Regex(array(
101-
'pattern' => '/^[0-9]+$/',
102-
));
103-
104-
$this->assertEquals('pattern', $constraint->getDefaultOption());
105-
}
106-
107-
public function testHtmlPatternEscaping()
108-
{
109-
$constraint = new Regex(array(
110-
'pattern' => '/^[0-9]+\/$/',
111-
));
112-
113-
$this->assertEquals('[0-9]+/', $constraint->getHtmlPattern());
114-
115-
$constraint = new Regex(array(
116-
'pattern' => '#^[0-9]+\#$#',
117-
));
118-
119-
$this->assertEquals('[0-9]+#', $constraint->getHtmlPattern());
120-
}
121-
122-
public function testHtmlPattern()
123-
{
124-
// Specified htmlPattern
125-
$constraint = new Regex(array(
126-
'pattern' => '/^[a-z]+$/i',
127-
'htmlPattern' => '[a-zA-Z]+',
128-
));
129-
$this->assertEquals('[a-zA-Z]+', $constraint->getHtmlPattern());
130-
131-
// Disabled htmlPattern
132-
$constraint = new Regex(array(
133-
'pattern' => '/^[a-z]+$/i',
134-
'htmlPattern' => false,
135-
));
136-
$this->assertNull($constraint->getHtmlPattern());
137-
138-
// Cannot be converted
139-
$constraint = new Regex(array(
140-
'pattern' => '/^[a-z]+$/i',
141-
));
142-
$this->assertNull($constraint->getHtmlPattern());
143-
144-
// Automatically converted
145-
$constraint = new Regex(array(
146-
'pattern' => '/^[a-z]+$/',
147-
));
148-
$this->assertEquals('[a-z]+', $constraint->getHtmlPattern());
149-
150-
// Automatically converted, adds .*
151-
$constraint = new Regex(array(
152-
'pattern' => '/[a-z]+/',
153-
));
154-
$this->assertEquals('.*[a-z]+.*', $constraint->getHtmlPattern());
155-
156-
// Dropped because of match=false
157-
$constraint = new Regex(array(
158-
'pattern' => '/[a-z]+/',
159-
'match' => false,
160-
));
161-
$this->assertNull($constraint->getHtmlPattern());
162-
}
16397
}

Tests/ExecutionContextTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function testGetPropertyPathWithNestedCollectionsAndAllMixed()
283283
$constraints = new Collection(array(
284284
'shelves' => new All(array('constraints' => array(
285285
new Collection(array(
286-
'name' => new ConstraintA(),
286+
'name' => new ConstraintA(),
287287
'books' => new All(array('constraints' => array(
288288
new ConstraintA(),
289289
))),

0 commit comments

Comments
 (0)