Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b9ce52

Browse files
bug #11559 [Validator] Convert objects to string in comparison validators (webmozart)
This PR was merged into the 2.3 branch. Discussion ---------- [Validator] Convert objects to string in comparison validators | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - In the [latest merge from 2.3 into 2.4](/symfony/symfony/commit/3bed1b7988e94a897a64c6a2ad3bf70bde9005c1), the changes from 6cf5e08 in 2.4 got lost. This PR brings back these changes and backports them to 2.3. The change is BC, because the former value `true` of the `$prettyDateTime` will be cast to `1`, which corresponds to the `PRETTY_DATE` format constant. Commits ------- 273671e [Validator] Convert objects to string in comparison validators. Reapplies 6cf5e08
2 parents 4f098dc + 273671e commit 6b9ce52

10 files changed

+68
-16
lines changed

src/Symfony/Component/Validator/ConstraintValidator.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
*/
2121
abstract class ConstraintValidator implements ConstraintValidatorInterface
2222
{
23+
/**
24+
* Whether to format {@link \DateTime} objects as RFC-3339 dates
25+
* ("Y-m-d H:i:s").
26+
*
27+
* @var integer
28+
*/
29+
const PRETTY_DATE = 1;
30+
31+
/**
32+
* Whether to cast objects with a "__toString()" method to strings.
33+
*
34+
* @var integer
35+
*/
36+
const OBJECT_TO_STRING = 2;
37+
2338
/**
2439
* @var ExecutionContextInterface
2540
*/
@@ -66,15 +81,15 @@ protected function formatTypeOf($value)
6681
* won't know what an "object", "array" or "resource" is and will be
6782
* confused by the violation message.
6883
*
69-
* @param mixed $value The value to format as string
70-
* @param bool $prettyDateTime Whether to format {@link \DateTime}
71-
* objects as RFC-3339 dates ("Y-m-d H:i:s")
84+
* @param mixed $value The value to format as string
85+
* @param integer $format A bitwise combination of the format
86+
* constants in this class
7287
*
7388
* @return string The string representation of the passed value
7489
*/
75-
protected function formatValue($value, $prettyDateTime = false)
90+
protected function formatValue($value, $format = 0)
7691
{
77-
if ($prettyDateTime && $value instanceof \DateTime) {
92+
if (($format & self::PRETTY_DATE) && $value instanceof \DateTime) {
7893
if (class_exists('IntlDateFormatter')) {
7994
$locale = \Locale::getDefault();
8095
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
@@ -86,6 +101,10 @@ protected function formatValue($value, $prettyDateTime = false)
86101
}
87102

88103
if (is_object($value)) {
104+
if ($format & self::OBJECT_TO_STRING && method_exists($value, '__toString')) {
105+
return $value->__toString();
106+
}
107+
89108
return 'object';
90109
}
91110

@@ -122,18 +141,18 @@ protected function formatValue($value, $prettyDateTime = false)
122141
* Each of the values is converted to a string using
123142
* {@link formatValue()}. The values are then concatenated with commas.
124143
*
125-
* @param array $values A list of values
126-
* @param bool $prettyDateTime Whether to format {@link \DateTime}
127-
* objects as RFC-3339 dates ("Y-m-d H:i:s")
144+
* @param array $values A list of values
145+
* @param integer $format A bitwise combination of the format
146+
* constants in this class
128147
*
129148
* @return string The string representation of the value list
130149
*
131150
* @see formatValue()
132151
*/
133-
protected function formatValues(array $values, $prettyDateTime = false)
152+
protected function formatValues(array $values, $format = 0)
134153
{
135154
foreach ($values as $key => $value) {
136-
$values[$key] = $this->formatValue($value, $prettyDateTime);
155+
$values[$key] = $this->formatValue($value, $format);
137156
}
138157

139158
return implode(', ', $values);

src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function validate($value, Constraint $constraint)
3232

3333
if (!$this->compareValues($value, $constraint->value)) {
3434
$this->context->addViolation($constraint->message, array(
35-
'{{ value }}' => $this->formatValue($value, true),
36-
'{{ compared_value }}' => $this->formatValue($constraint->value, true),
35+
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
36+
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
3737
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
3838
));
3939
}

src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
1717

18+
class ComparisonTest_Class
19+
{
20+
protected $value;
21+
22+
public function __construct($value)
23+
{
24+
$this->value = $value;
25+
}
26+
27+
public function __toString()
28+
{
29+
return (string) $this->value;
30+
}
31+
}
32+
1833
/**
1934
* @author Daniel Holmes <[email protected]>
2035
*/

src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function provideValidComparisons()
3939
array(3, '3'),
4040
array('a', 'a'),
4141
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
42+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5)),
4243
array(null, 1),
4344
);
4445
}
@@ -51,7 +52,8 @@ public function provideInvalidComparisons()
5152
return array(
5253
array(1, '1', 2, '2', 'integer'),
5354
array('22', '"22"', '333', '"333"', 'string'),
54-
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime')
55+
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
56+
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5557
);
5658
}
5759
}

src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function provideValidComparisons()
3737
return array(
3838
array(2, 1),
3939
array(new \DateTime('2005/01/01'), new \DateTime('2001/01/01')),
40+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(4)),
4041
array('333', '22'),
4142
array(null, 1),
4243
);
@@ -52,6 +53,8 @@ public function provideInvalidComparisons()
5253
array(2, '2', 2, '2', 'integer'),
5354
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2005/01/01'), 'Jan 1, 2005, 12:00 AM', 'DateTime'),
5455
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
56+
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
57+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5558
array('22', '"22"', '333', '"333"', 'string'),
5659
array('22', '"22"', '22', '"22"', 'string')
5760
);

src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ protected function createConstraint(array $options)
3535
public function provideValidComparisons()
3636
{
3737
$date = new \DateTime('2000-01-01');
38+
$object = new ComparisonTest_Class(2);
3839

3940
return array(
4041
array(3, 3),
4142
array('a', 'a'),
4243
array($date, $date),
44+
array($object, $object),
4345
array(null, 1),
4446
);
4547
}
@@ -54,7 +56,8 @@ public function provideInvalidComparisons()
5456
array(2, '2', '2', '"2"', 'string'),
5557
array('22', '"22"', '333', '"333"', 'string'),
5658
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'),
57-
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime')
59+
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime'),
60+
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5861
);
5962
}
6063
}

src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function provideValidComparisons()
3939
array(1, 1),
4040
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
4141
array(new \DateTime('2000-01-01'), new \DateTime('2020-01-01')),
42+
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5)),
43+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5)),
4244
array('a', 'a'),
4345
array('a', 'z'),
4446
array(null, 1),
@@ -53,6 +55,7 @@ public function provideInvalidComparisons()
5355
return array(
5456
array(2, '2', 1, '1', 'integer'),
5557
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
58+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(4), '4', __NAMESPACE__.'\ComparisonTest_Class'),
5659
array('c', '"c"', 'b', '"b"', 'string')
5760
);
5861
}

src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function provideValidComparisons()
3737
return array(
3838
array(1, 2),
3939
array(new \DateTime('2000-01-01'), new \DateTime('2010-01-01')),
40+
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5)),
4041
array('22', '333'),
4142
array(null, 1),
4243
);
@@ -52,6 +53,8 @@ public function provideInvalidComparisons()
5253
array(2, '2', 2, '2', 'integer'),
5354
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
5455
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
56+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
57+
array(new ComparisonTest_Class(6), '6', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5558
array('333', '"333"', '22', '"22"', 'string'),
5659
);
5760
}

src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function provideValidComparisons()
3838
array(1, 2),
3939
array('22', '333'),
4040
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')),
41+
array(new ComparisonTest_Class(6), new ComparisonTest_Class(5)),
4142
array(null, 1),
4243
);
4344
}
@@ -51,7 +52,8 @@ public function provideInvalidComparisons()
5152
array(3, '3', 3, '3', 'integer'),
5253
array('2', '"2"', 2, '2', 'integer'),
5354
array('a', '"a"', 'a', '"a"', 'string'),
54-
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime')
55+
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
56+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5557
);
5658
}
5759
}

src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ public function provideValidComparisons()
5050
public function provideInvalidComparisons()
5151
{
5252
$date = new \DateTime('2000-01-01');
53+
$object = new ComparisonTest_Class(2);
5354

5455
return array(
5556
array(3, '3', 3, '3', 'integer'),
5657
array('a', '"a"', 'a', '"a"', 'string'),
57-
array($date, 'Jan 1, 2000, 12:00 AM', $date, 'Jan 1, 2000, 12:00 AM', 'DateTime')
58+
array($date, 'Jan 1, 2000, 12:00 AM', $date, 'Jan 1, 2000, 12:00 AM', 'DateTime'),
59+
array($object, '2', $object, '2', __NAMESPACE__.'\ComparisonTest_Class'),
5860
);
5961
}
6062
}

0 commit comments

Comments
 (0)