8000 merged branch bes89/validator_object_to_string (PR #9036) · symfony/symfony@d3ca145 · GitHub
[go: up one dir, main page]

Skip to content

Commit d3ca145

Browse files
committed
merged branch bes89/validator_object_to_string (PR #9036)
This PR was submitted for the 2.3 branch but it was merged into the master branch instead (closes #9036). Discussion ---------- [Validator] convert object to string if possible instead of using var_export directly | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 2149146 [Validator] convert object to string if possible instead of using var_export directly
2 parents f4f6b06 + 6cf5e08 commit d3ca145

9 files changed

+41
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ private function valueToType($value)
6060
*/
6161
private function valueToString($value)
6262
{
63+
if (is_object($value) && method_exists($value, '__toString')) {
64+
return (string) $value;
65+
}
66+
6367
if ($value instanceof \DateTime) {
6468
return $value->format('Y-m-d H:i:s');
6569
}

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

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

17+
class ComparisonTest_Class
18+
{
1 10000 9+
protected $value;
20+
21+
public function __construct($value)
22+
{
23+
$this->value = $value;
24+
}
25+
26+
public function __toString()
27+
{
28+
return (string) $this->value;
29+
}
30+
}
31+
1732
/**
1833
* @author Daniel Holmes <daniel@danielholmes.org>
1934
*/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function provideValidComparisons()
4040
array(3, '3'),
4141
array('a', 'a'),
4242
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
43+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5)),
4344
array(null, 1),
4445
);
4546
}
@@ -52,7 +53,8 @@ public function provideInvalidComparisons()
5253
return array(
5354
array(1, 2, '2', 'integer'),
5455
array('22', '333', "'333'", 'string'),
55-
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime')
56+
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'),
57+
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5658
);
5759
}
5860
}

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', 'integer'),
5354
array(new \DateTime('2000/01/01'), new \DateTime('2005/01/01'), '2005-01-01 00:00:00', 'DateTime'),
5455
array(new \DateTime('2000/01/01'), new \DateTime('2000/01/01'), '2000-01-01 00:00:00', 'DateTime'),
56+
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
57+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5558
array('22', '333', "'333'", 'string'),
5659
array('22', '22', "'22'", 'string')
5760
);

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

< 9E7A span aria-hidden="true" class="f6 text-bold fgColor-danger">-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ protected function createConstraint(array $options)
3636
public function provideValidComparisons()
3737
{
3838
$date = new \DateTime('2000-01-01');
39+
$object = new ComparisonTest_Class(2);
3940

4041
return array(
4142
array(3, 3),
4243
array('a', 'a'),
4344
array($date, $date),
45+
array($object, $object),
4446
array(null, 1),
4547
);
4648
}
@@ -55,7 +57,8 @@ public function provideInvalidComparisons()
5557
array(2, '2', "'2'", 'string'),
5658
array('22', '333', "'333'", 'string'),
5759
array(new \DateTime('2001-01-01'), new \DateTime('2001-01-01'), '2001-01-01 00:00:00', 'DateTime'),
58-
array(new \DateTime('2001-01-01'), new \DateTime('1999-01-01'), '1999-01-01 00:00:00', 'DateTime')
60+
array(new \DateTime('2001-01-01'), new \DateTime('1999-01-01'), '1999-01-01 00:00:00', 'DateTime'),
61+
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5962
);
6063
}
6164
}

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, 1, '1', 'integer'),
5557
array(new \DateTime('2010-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'),
58+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(4), '4', __NAMESPACE__.'\ComparisonTest_Class'),
5659
array('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', 'integer'),
5354
array(new \DateTime('2010-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'),
5455
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'),
56+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
57+
array(new ComparisonTest_Class(6), new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5558
array('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
@@ -39,6 +39,7 @@ public function provideValidComparisons()
3939
array(1, 2),
4040
array('22', '333'),
4141
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')),
42+
array(new ComparisonTest_Class(6), new ComparisonTest_Class(5)),
4243
array(null, 1),
4344
);
4445
}
@@ -52,7 +53,8 @@ public function provideInvalidComparisons()
5253
array(3, 3, '3', 'integer'),
5354
array('2', 2, '2', 'integer'),
5455
array('a', 'a', "'a'", 'string'),
55-
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime')
56+
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'),
57+
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
5658
);
5759
}
5860
}

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

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

5556
return array(
5657
array(3, 3, '3', 'integer'),
5758
array('a', 'a', "'a'", 'string'),
58-
array($date, $date, '2000-01-01 00:00:00', 'DateTime')
59+
array($date, $date, '2000-01-01 00:00:00', 'DateTime'),
60+
array($object, $object, '2', __NAMESPACE__.'\ComparisonTest_Class'),
5961
);
6062
}
6163
}

0 commit comments

Comments
 (0)
0