Skip to content

Commit 04cd7a3

Browse files
committed
Merge branch '2.2' into 2.3
* 2.2: [Form] add support for Length and Range constraint in order to replace MaxLength, MinLength, Max and Min constraints in next release (2.3) [Form] check the required output timezone against the actual timezone of the input datetime object, rather than the expected timezone supplied
2 parents 509feab + 78a22d7 commit 04cd7a3

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function reverseTransform($rfc3339)
5858
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
5959
}
6060

61-
if ($this->outputTimezone !== $this->inputTimezone) {
61+
if ($this->outputTimezone !== $dateTime->getTimezone()->getName()) {
6262
try {
6363
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
6464
} catch (\Exception $e) {

Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@ public function guessTypeForConstraint(Constraint $constraint)
147147

148148
case 'Symfony\Component\Validator\Constraints\MaxLength':
149149
case 'Symfony\Component\Validator\Constraints\MinLength':
150+
case 'Symfony\Component\Validator\Constraints\Length':
150151
case 'Symfony\Component\Validator\Constraints\Regex':
151152
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
152153

153154
case 'Symfony\Component\Validator\Constraints\Min':
154155
case 'Symfony\Component\Validator\Constraints\Max':
156+
case 'Symfony\Component\Validator\Constraints\Range':
155157
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
156158

157159
case 'Symfony\Component\Validator\Constraints\MinCount':
@@ -198,6 +200,12 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
198200
case 'Symfony\Component\Validator\Constraints\MaxLength':
199201
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
200202

203+
case 'Symfony\Component\Validator\Constraints\Length':
204+
if (is_numeric($constraint->max)) {
205+
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
206+
}
207+
break;
208+
201209
case 'Symfony\Component\Validator\Constraints\Type':
202210
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
203211
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
@@ -206,6 +214,12 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
206214

207215
case 'Symfony\Component\Validator\Constraints\Max':
208216
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
217+
218+
case 'Symfony\Component\Validator\Constraints\Range':
219+
if (is_numeric($constraint->max)) {
220+
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
221+
}
222+
break;
209223
}
210224

211225
return null;
@@ -224,6 +238,12 @@ public function guessPatternForConstraint(Constraint $constraint)
224238
case 'Symfony\Component\Validator\Constraints\MinLength':
225239
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);
226240

241+
case 'Symfony\Component\Validator\Constraints\Length':
242+
if (is_numeric($constraint->min)) {
243+
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
244+
}
245+
break;
246+
227247
case 'Symfony\Component\Validator\Constraints\Regex':
228248
$htmlPattern = $constraint->getHtmlPattern();
229249

@@ -235,6 +255,12 @@ public function guessPatternForConstraint(Constraint $constraint)
235255
case 'Symfony\Component\Validator\Constraints\Min':
236256
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
237257

258+
case 'Symfony\Component\Validator\Constraints\Range':
259+
if (is_numeric($constraint->min)) {
260+
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->min)), Guess::LOW_CONFIDENCE);
261+
}
262+
break;
263+
238264
case 'Symfony\Component\Validator\Constraints\Type':
239265
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
240266
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);

Tests/Extension/Core/DataTransformer/DateTimeToRfc3339TransformerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function reverseTransformProvider()
6565
// format without seconds, as appears in some browsers
6666
array('UTC', 'UTC', '2010-02-03 04:05:00 UTC', '2010-02-03T04:05Z'),
6767
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:00 America/New_York', '2010-02-03T17:05+08:00'),
68+
array('Europe/Amsterdam', 'Europe/Amsterdam', '2013-08-21 10:30:00 Europe/Amsterdam', '2013-08-21T08:30:00Z')
6869
));
6970
}
7071

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 Symfony\Component\Form\Tests\Extension\Validator;
13+
14+
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
15+
use Symfony\Component\Form\Guess\Guess;
16+
use Symfony\Component\Validator\Constraints\Length;
17+
use Symfony\Component\Validator\Constraints\Type;
18+
19+
/**
20+
* @author franek <[email protected]>
21+
*/
22+
class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
23+
{
24+
private $typeGuesser;
25+
26+
public function setUp()
27+
{
28+
if (!class_exists('Symfony\Component\Validator\Constraint')) {
29+
$this->markTestSkipped('The "Validator" component is not available');
30+
}
31+
32+
$metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
33+
34+
$this->typeGuesser = new ValidatorTypeGuesser($metadataFactory);
35+
}
36+
37+
public function testGuessMaxLengthForConstraintWithMaxValue()
38+
{
39+
$constraint = new Length(array('max' => '2'));
40+
41+
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
42+
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
43+
$this->assertEquals(2, $result->getValue());
44+
$this->assertEquals(Guess::HIGH_CONFIDENCE, $result->getConfidence());
45+
}
46+
47+
public function testGuessMaxLengthForConstraintWithMinValue()
48+
{
49+
$constraint = new Length(array('min' => '2'));
50+
51+
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
52+
$this->assertNull($result);
53+
}
54+
55+
/**
56+
* @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType
57+
*/
58+
public function testGuessMaxLengthForConstraintWithType($type)
59+
{
60+
$constraint = new Type($type);
61+
62+
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
63+
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
64+
$this->assertEquals(null, $result->getValue());
65+
$this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence());
66+
}
67+
68+
public static function dataProviderTestGuessMaxLengthForConstraintWithType()
69+
{
70+
return array (
71+
array('double'),
72+
array('float'),
73+
array('numeric'),
74+
array('real')
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)