[Validator] Convert objects to string in comparison validators. Reapplies 6cf5e0812e
This commit is contained in:
parent
d754467d0e
commit
273671ec59
@ -20,6 +20,21 @@ namespace Symfony\Component\Validator;
|
|||||||
*/
|
*/
|
||||||
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Whether to format {@link \DateTime} objects as RFC-3339 dates
|
||||||
|
* ("Y-m-d H:i:s").
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const PRETTY_DATE = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to cast objects with a "__toString()" method to strings.
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
const OBJECT_TO_STRING = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ExecutionContextInterface
|
* @var ExecutionContextInterface
|
||||||
*/
|
*/
|
||||||
@ -67,14 +82,14 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
* confused by the violation message.
|
* confused by the violation message.
|
||||||
*
|
*
|
||||||
* @param mixed $value The value to format as string
|
* @param mixed $value The value to format as string
|
||||||
* @param bool $prettyDateTime Whether to format {@link \DateTime}
|
* @param integer $format A bitwise combination of the format
|
||||||
* objects as RFC-3339 dates ("Y-m-d H:i:s")
|
* constants in this class
|
||||||
*
|
*
|
||||||
* @return string The string representation of the passed value
|
* @return string The string representation of the passed value
|
||||||
*/
|
*/
|
||||||
protected function formatValue($value, $prettyDateTime = false)
|
protected function formatValue($value, $format = 0)
|
||||||
{
|
{
|
||||||
if ($prettyDateTime && $value instanceof \DateTime) {
|
if (($format & self::PRETTY_DATE) && $value instanceof \DateTime) {
|
||||||
if (class_exists('IntlDateFormatter')) {
|
if (class_exists('IntlDateFormatter')) {
|
||||||
$locale = \Locale::getDefault();
|
$locale = \Locale::getDefault();
|
||||||
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
|
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
|
||||||
@ -86,6 +101,10 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_object($value)) {
|
if (is_object($value)) {
|
||||||
|
if ($format & self::OBJECT_TO_STRING && method_exists($value, '__toString')) {
|
||||||
|
return $value->__toString();
|
||||||
|
}
|
||||||
|
|
||||||
return 'object';
|
return 'object';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,17 +142,17 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|||||||
* {@link formatValue()}. The values are then concatenated with commas.
|
* {@link formatValue()}. The values are then concatenated with commas.
|
||||||
*
|
*
|
||||||
* @param array $values A list of values
|
* @param array $values A list of values
|
||||||
* @param bool $prettyDateTime Whether to format {@link \DateTime}
|
* @param integer $format A bitwise combination of the format
|
||||||
* objects as RFC-3339 dates ("Y-m-d H:i:s")
|
* constants in this class
|
||||||
*
|
*
|
||||||
* @return string The string representation of the value list
|
* @return string The string representation of the value list
|
||||||
*
|
*
|
||||||
* @see formatValue()
|
* @see formatValue()
|
||||||
*/
|
*/
|
||||||
protected function formatValues(array $values, $prettyDateTime = false)
|
protected function formatValues(array $values, $format = 0)
|
||||||
{
|
{
|
||||||
foreach ($values as $key => $value) {
|
foreach ($values as $key => $value) {
|
||||||
$values[$key] = $this->formatValue($value, $prettyDateTime);
|
$values[$key] = $this->formatValue($value, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(', ', $values);
|
return implode(', ', $values);
|
||||||
|
@ -32,8 +32,8 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
|
|||||||
|
|
||||||
if (!$this->compareValues($value, $constraint->value)) {
|
if (!$this->compareValues($value, $constraint->value)) {
|
||||||
$this->context->addViolation($constraint->message, array(
|
$this->context->addViolation($constraint->message, array(
|
||||||
'{{ value }}' => $this->formatValue($value, true),
|
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
|
||||||
'{{ compared_value }}' => $this->formatValue($constraint->value, true),
|
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
|
||||||
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
|
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,21 @@ use Symfony\Component\Intl\Util\IntlTestHelper;
|
|||||||
use Symfony\Component\Validator\Constraint;
|
use Symfony\Component\Validator\Constraint;
|
||||||
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
|
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
|
||||||
|
|
||||||
|
class ComparisonTest_Class
|
||||||
|
{
|
||||||
|
protected $value;
|
||||||
|
|
||||||
|
public function __construct($value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return (string) $this->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Daniel Holmes <daniel@danielholmes.org>
|
* @author Daniel Holmes <daniel@danielholmes.org>
|
||||||
*/
|
*/
|
||||||
|
@ -39,6 +39,7 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(3, '3'),
|
array(3, '3'),
|
||||||
array('a', 'a'),
|
array('a', 'a'),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
||||||
|
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5)),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -51,7 +52,8 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(1, '1', 2, '2', 'integer'),
|
array(1, '1', 2, '2', 'integer'),
|
||||||
array('22', '"22"', '333', '"333"', 'string'),
|
array('22', '"22"', '333', '"333"', 'string'),
|
||||||
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime')
|
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(2, 1),
|
array(2, 1),
|
||||||
array(new \DateTime('2005/01/01'), new \DateTime('2001/01/01')),
|
array(new \DateTime('2005/01/01'), new \DateTime('2001/01/01')),
|
||||||
|
array(new ComparisonTest_Class(5), new ComparisonTest_Class(4)),
|
||||||
array('333', '22'),
|
array('333', '22'),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
@ -52,6 +53,8 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(2, '2', 2, '2', 'integer'),
|
array(2, '2', 2, '2', 'integer'),
|
||||||
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2005/01/01'), 'Jan 1, 2005, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2005/01/01'), 'Jan 1, 2005, 12:00 AM', 'DateTime'),
|
||||||
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
array('22', '"22"', '333', '"333"', 'string'),
|
array('22', '"22"', '333', '"333"', 'string'),
|
||||||
array('22', '"22"', '22', '"22"', 'string')
|
array('22', '"22"', '22', '"22"', 'string')
|
||||||
);
|
);
|
||||||
|
@ -35,11 +35,13 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
public function provideValidComparisons()
|
public function provideValidComparisons()
|
||||||
{
|
{
|
||||||
$date = new \DateTime('2000-01-01');
|
$date = new \DateTime('2000-01-01');
|
||||||
|
$object = new ComparisonTest_Class(2);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array(3, 3),
|
array(3, 3),
|
||||||
array('a', 'a'),
|
array('a', 'a'),
|
||||||
array($date, $date),
|
array($date, $date),
|
||||||
|
array($object, $object),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -54,7 +56,8 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(2, '2', '2', '"2"', 'string'),
|
array(2, '2', '2', '"2"', 'string'),
|
||||||
array('22', '"22"', '333', '"333"', 'string'),
|
array('22', '"22"', '333', '"333"', 'string'),
|
||||||
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'),
|
||||||
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime')
|
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,8 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(1, 1),
|
array(1, 1),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2020-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2020-01-01')),
|
||||||
|
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5)),
|
||||||
|
array(new ComparisonTest_Class(5), new ComparisonTest_Class(5)),
|
||||||
array('a', 'a'),
|
array('a', 'a'),
|
||||||
array('a', 'z'),
|
array('a', 'z'),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
@ -53,6 +55,7 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(2, '2', 1, '1', 'integer'),
|
array(2, '2', 1, '1', 'integer'),
|
||||||
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(4), '4', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
array('c', '"c"', 'b', '"b"', 'string')
|
array('c', '"c"', 'b', '"b"', 'string')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(1, 2),
|
array(1, 2),
|
||||||
array(new \DateTime('2000-01-01'), new \DateTime('2010-01-01')),
|
array(new \DateTime('2000-01-01'), new \DateTime('2010-01-01')),
|
||||||
|
array(new ComparisonTest_Class(4), new ComparisonTest_Class(5)),
|
||||||
array('22', '333'),
|
array('22', '333'),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
@ -52,6 +53,8 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(2, '2', 2, '2', 'integer'),
|
array(2, '2', 2, '2', 'integer'),
|
||||||
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
|
array(new ComparisonTest_Class(6), '6', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
array('333', '"333"', '22', '"22"', 'string'),
|
array('333', '"333"', '22', '"22"', 'string'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(1, 2),
|
array(1, 2),
|
||||||
array('22', '333'),
|
array('22', '333'),
|
||||||
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')),
|
array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')),
|
||||||
|
array(new ComparisonTest_Class(6), new ComparisonTest_Class(5)),
|
||||||
array(null, 1),
|
array(null, 1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -51,7 +52,8 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
array(3, '3', 3, '3', 'integer'),
|
array(3, '3', 3, '3', 'integer'),
|
||||||
array('2', '"2"', 2, '2', 'integer'),
|
array('2', '"2"', 2, '2', 'integer'),
|
||||||
array('a', '"a"', 'a', '"a"', 'string'),
|
array('a', '"a"', 'a', '"a"', 'string'),
|
||||||
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime')
|
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,11 +50,13 @@ class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
|
|||||||
public function provideInvalidComparisons()
|
public function provideInvalidComparisons()
|
||||||
{
|
{
|
||||||
$date = new \DateTime('2000-01-01');
|
$date = new \DateTime('2000-01-01');
|
||||||
|
$object = new ComparisonTest_Class(2);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array(3, '3', 3, '3', 'integer'),
|
array(3, '3', 3, '3', 'integer'),
|
||||||
array('a', '"a"', 'a', '"a"', 'string'),
|
array('a', '"a"', 'a', '"a"', 'string'),
|
||||||
array($date, 'Jan 1, 2000, 12:00 AM', $date, 'Jan 1, 2000, 12:00 AM', 'DateTime')
|
array($date, 'Jan 1, 2000, 12:00 AM', $date, 'Jan 1, 2000, 12:00 AM', 'DateTime'),
|
||||||
|
array($object, '2', $object, '2', __NAMESPACE__.'\ComparisonTest_Class'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user