From 75e881556638a3e928c8a0260da2a87df7886bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Nohales?= Date: Wed, 9 Apr 2014 19:09:58 -0300 Subject: [PATCH 01/24] [Validator] Fix constraint violation message parameterization --- .../Validator/Constraints/AbstractComparisonValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php index 6d80c45027..a561ddc759 100644 --- a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php @@ -32,7 +32,7 @@ abstract class AbstractComparisonValidator extends ConstraintValidator if (!$this->compareValues($value, $constraint->value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($constraint->value), + '{{ value }}' => $this->valueToString($value), '{{ compared_value }}' => $this->valueToString($constraint->value), '{{ compared_value_type }}' => $this->valueToType($constraint->value) )); From 97243bcd024bbfa458d66a4263a50ee7f16bbe74 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 10 Apr 2014 17:39:39 +0200 Subject: [PATCH 02/24] [Validator] Fixed value-to-string conversion in constraint violations --- .../Validator/ConstraintValidator.php | 63 +++++++++++++++++++ .../Validator/ConstraintViolation.php | 4 +- .../AbstractComparisonValidator.php | 28 --------- .../Validator/Constraints/BlankValidator.php | 4 +- .../Constraints/CardSchemeValidator.php | 8 ++- .../Validator/Constraints/ChoiceValidator.php | 16 +++-- .../Constraints/CountryValidator.php | 4 +- .../Constraints/CurrencyValidator.php | 4 +- .../Validator/Constraints/DateValidator.php | 4 +- .../Validator/Constraints/EmailValidator.php | 4 +- .../Validator/Constraints/FileValidator.php | 14 ++--- .../Validator/Constraints/IpValidator.php | 4 +- .../Constraints/LanguageValidator.php | 4 +- .../Validator/Constraints/LocaleValidator.php | 4 +- .../Validator/Constraints/NullValidator.php | 4 +- .../Validator/Constraints/RangeValidator.php | 2 +- .../Validator/Constraints/RegexValidator.php | 4 +- .../Validator/Constraints/TimeValidator.php | 4 +- .../Validator/Constraints/TypeValidator.php | 2 +- .../Validator/Constraints/UrlValidator.php | 4 +- .../AbstractComparisonValidatorTestCase.php | 6 +- .../Tests/Constraints/BlankValidatorTest.php | 12 ++-- .../Tests/Constraints/ChoiceValidatorTest.php | 8 +-- .../Constraints/EqualToValidatorTest.php | 6 +- .../GreaterThanOrEqualValidatorTest.php | 6 +- .../Constraints/GreaterThanValidatorTest.php | 12 ++-- .../Constraints/IdenticalToValidatorTest.php | 10 +-- .../LessThanOrEqualValidatorTest.php | 6 +- .../Constraints/LessThanValidatorTest.php | 10 +-- .../Constraints/NotEqualToValidatorTest.php | 8 +-- .../NotIdenticalToValidatorTest.php | 6 +- .../Tests/Constraints/TypeValidatorTest.php | 55 ++++++++-------- 32 files changed, 202 insertions(+), 128 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index 201db0f230..fe3758f65b 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -32,4 +32,67 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface { $this->context = $context; } + + /** + * Returns a string representation of the type of the value. + * + * @param mixed $value + * + * @return string + */ + protected function valueToType($value) + { + return is_object($value) ? get_class($value) : gettype($value); + } + + /** + * Returns a string representation of the value. + * + * @param mixed $value + * + * @return string + */ + protected function valueToString($value) + { + if ($value instanceof \DateTime) { + if (class_exists('IntlDateFormatter')) { + $locale = \Locale::getDefault(); + $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); + + return $formatter->format($value); + } + + return $value->format('Y-m-d H:i:s'); + } + + if (is_object($value)) { + return 'Object('.get_class($value).')'; + } + + if (is_array($value)) { + return 'Array'; + } + + if (is_string($value)) { + return '"'.$value.'"'; + } + + if (is_resource($value)) { + return sprintf('Resource(%s#%d)', get_resource_type($value), $value); + } + + if (null === $value) { + return 'null'; + } + + if (false === $value) { + return 'false'; + } + + if (true === $value) { + return 'true'; + } + + return (string) $value; + } } diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index ddd14dbdc0..02b77be0bf 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -96,9 +96,9 @@ class ConstraintViolation implements ConstraintViolationInterface public function __toString() { if (is_object($this->root)) { - $class = get_class($this->root); + $class = 'Object('.get_class($this->root).')'; } elseif (is_array($this->root)) { - $class = "Array"; + $class = 'Array'; } else { $class = (string) $this->root; } diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php index a561ddc759..351ce73491 100644 --- a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php @@ -39,34 +39,6 @@ abstract class AbstractComparisonValidator extends ConstraintValidator } } - /** - * Returns a string representation of the type of the value. - * - * @param mixed $value - * - * @return string - */ - private function valueToType($value) - { - return is_object($value) ? get_class($value) : gettype($value); - } - - /** - * Returns a string representation of the value. - * - * @param mixed $value - * - * @return string - */ - private function valueToString($value) - { - if ($value instanceof \DateTime) { - return $value->format('Y-m-d H:i:s'); - } - - return var_export($value, true); - } - /** * Compares the two given values to find if their relationship is valid * diff --git a/src/Symfony/Component/Validator/Constraints/BlankValidator.php b/src/Symfony/Component/Validator/Constraints/BlankValidator.php index 06064620f1..7bb57ddd0e 100644 --- a/src/Symfony/Component/Validator/Constraints/BlankValidator.php +++ b/src/Symfony/Component/Validator/Constraints/BlankValidator.php @@ -27,7 +27,9 @@ class BlankValidator extends ConstraintValidator public function validate($value, Constraint $constraint) { if ('' !== $value && null !== $value) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->valueToString($value) + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php b/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php index 1ece3fdd65..eae71a92d4 100644 --- a/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php @@ -108,7 +108,9 @@ class CardSchemeValidator extends ConstraintValidator } if (!is_numeric($value)) { - $this->context->addViolation($constraint->message); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->valueToString($value), + )); return; } @@ -124,6 +126,8 @@ class CardSchemeValidator extends ConstraintValidator } } - $this->context->addViolation($constraint->message); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index ef9ae6e3ad..c4f3b56c8b 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -59,25 +59,33 @@ class ChoiceValidator extends ConstraintValidator if ($constraint->multiple) { foreach ($value as $_value) { if (!in_array($_value, $choices, $constraint->strict)) { - $this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value)); + $this->context->addViolation($constraint->multipleMessage, array( + '{{ value }}' => $this->valueToString($_value), + )); } } $count = count($value); if ($constraint->min !== null && $count < $constraint->min) { - $this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min); + $this->context->addViolation($constraint->minMessage, array( + '{{ limit }}' => $constraint->min + ), null, (int) $constraint->min); return; } if ($constraint->max !== null && $count > $constraint->max) { - $this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max); + $this->context->addViolation($constraint->maxMessage, array( + '{{ limit }}' => $constraint->max + ), null, (int) $constraint->max); return; } } elseif (!in_array($value, $choices, $constraint->strict)) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->valueToString($value) + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/CountryValidator.php b/src/Symfony/Component/Validator/Constraints/CountryValidator.php index 6a8d1d6446..c912a650a6 100644 --- a/src/Symfony/Component/Validator/Constraints/CountryValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountryValidator.php @@ -42,7 +42,9 @@ class CountryValidator extends ConstraintValidator $countries = Intl::getRegionBundle()->getCountryNames(); if (!isset($countries[$value])) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php index 4451047cdd..d12952fc81 100644 --- a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php @@ -42,7 +42,9 @@ class CurrencyValidator extends ConstraintValidator $currencies = Intl::getCurrencyBundle()->getCurrencyNames(); if (!isset($currencies[$value])) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/DateValidator.php b/src/Symfony/Component/Validator/Constraints/DateValidator.php index 733a3b7f5e..1170314a70 100644 --- a/src/Symfony/Component/Validator/Constraints/DateValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateValidator.php @@ -40,7 +40,9 @@ class DateValidator extends ConstraintValidator $value = (string) $value; if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/EmailValidator.php b/src/Symfony/Component/Validator/Constraints/EmailValidator.php index e14546ce82..5b433a768f 100644 --- a/src/Symfony/Component/Validator/Constraints/EmailValidator.php +++ b/src/Symfony/Component/Validator/Constraints/EmailValidator.php @@ -50,7 +50,9 @@ class EmailValidator extends ConstraintValidator } if (!$valid) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index 5f499810e6..dce0480a0e 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -126,10 +126,10 @@ class FileValidator extends ConstraintValidator if ($size > $limit) { $this->context->addViolation($constraint->maxSizeMessage, array( - '{{ size }}' => $size, - '{{ limit }}' => $limit, - '{{ suffix }}' => $suffix, - '{{ file }}' => $path, + '{{ size }}' => $size, + '{{ limit }}' => $limit, + '{{ suffix }}' => $suffix, + '{{ file }}' => $path, )); return; @@ -161,9 +161,9 @@ class FileValidator extends ConstraintValidator if (false === $valid) { $this->context->addViolation($constraint->mimeTypesMessage, array( - '{{ type }}' => '"'.$mime.'"', - '{{ types }}' => '"'.implode('", "', $mimeTypes) .'"', - '{{ file }}' => $path, + '{{ type }}' => '"'.$mime.'"', + '{{ types }}' => '"'.implode('", "', $mimeTypes) .'"', + '{{ file }}' => $path, )); } } diff --git a/src/Symfony/Component/Validator/Constraints/IpValidator.php b/src/Symfony/Component/Validator/Constraints/IpValidator.php index 69afe61b71..ad5d9e2466 100644 --- a/src/Symfony/Component/Validator/Constraints/IpValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IpValidator.php @@ -91,7 +91,9 @@ class IpValidator extends ConstraintValidator } if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php index 87019705db..02edb963f1 100644 --- a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php @@ -42,7 +42,9 @@ class LanguageValidator extends ConstraintValidator $languages = Intl::getLanguageBundle()->getLanguageNames(); if (!isset($languages[$value])) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php index daf594800b..cfd2105e90 100644 --- a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php @@ -42,7 +42,9 @@ class LocaleValidator extends ConstraintValidator $locales = Intl::getLocaleBundle()->getLocaleNames(); if (!isset($locales[$value])) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/NullValidator.php b/src/Symfony/Component/Validator/Constraints/NullValidator.php index f0f09c492f..bc9bf4f38c 100644 --- a/src/Symfony/Component/Validator/Constraints/NullValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NullValidator.php @@ -33,7 +33,9 @@ class NullValidator extends ConstraintValidator $value = 'Array'; } - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php index 6f0ad51dba..fe79f54222 100644 --- a/src/Symfony/Component/Validator/Constraints/RangeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php @@ -30,7 +30,7 @@ class RangeValidator extends ConstraintValidator if (!is_numeric($value)) { $this->context->addViolation($constraint->invalidMessage, array( - '{{ value }}' => $value, + '{{ value }}' => $this->valueToString($value), )); return; diff --git a/src/Symfony/Component/Validator/Constraints/RegexValidator.php b/src/Symfony/Component/Validator/Constraints/RegexValidator.php index 1f2febbbca..f59d6a4e25 100644 --- a/src/Symfony/Component/Validator/Constraints/RegexValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RegexValidator.php @@ -41,7 +41,9 @@ class RegexValidator extends ConstraintValidator $value = (string) $value; if ($constraint->match xor preg_match($constraint->pattern, $value)) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/TimeValidator.php b/src/Symfony/Component/Validator/Constraints/TimeValidator.php index cbaa816d1a..36260e67d1 100644 --- a/src/Symfony/Component/Validator/Constraints/TimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimeValidator.php @@ -40,7 +40,9 @@ class TimeValidator extends ConstraintValidator $value = (string) $value; if (!preg_match(static::PATTERN, $value)) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/TypeValidator.php b/src/Symfony/Component/Validator/Constraints/TypeValidator.php index f75bb0fd8b..ff713ffeef 100644 --- a/src/Symfony/Component/Validator/Constraints/TypeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TypeValidator.php @@ -44,7 +44,7 @@ class TypeValidator extends ConstraintValidator } $this->context->addViolation($constraint->message, array( - '{{ value }}' => is_object($value) ? get_class($value) : (is_array($value) ? 'Array' : (string) $value), + '{{ value }}' => $this->valueToString($value), '{{ type }}' => $constraint->type, )); } diff --git a/src/Symfony/Component/Validator/Constraints/UrlValidator.php b/src/Symfony/Component/Validator/Constraints/UrlValidator.php index d743c3807f..4aeab117af 100644 --- a/src/Symfony/Component/Validator/Constraints/UrlValidator.php +++ b/src/Symfony/Component/Validator/Constraints/UrlValidator.php @@ -55,7 +55,9 @@ class UrlValidator extends ConstraintValidator $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols)); if (!preg_match($pattern, $value)) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php index d72eaf23fa..c0ab0f7a53 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php @@ -29,6 +29,8 @@ abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_Te ->disableOriginalConstructor() ->getMock(); $this->validator->initialize($this->context); + + \Locale::setDefault('en'); } /** @@ -75,7 +77,7 @@ abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_Te * @param mixed $comparedValueString * @param string $comparedValueType */ - public function testInvalidComparisonToValue($dirtyValue, $comparedValue, $comparedValueString, $comparedValueType) + public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType) { $constraint = $this->createConstraint(array('value' => $comparedValue)); $constraint->message = 'Constraint Message'; @@ -87,7 +89,7 @@ abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_Te $this->context->expects($this->once()) ->method('addViolation') ->with('Constraint Message', array( - '{{ value }}' => $comparedValueString, + '{{ value }}' => $dirtyValueAsString, '{{ compared_value }}' => $comparedValueString, '{{ compared_value_type }}' => $comparedValueType )); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php index 0fbe5e6fbb..fe4a30b638 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php @@ -51,7 +51,7 @@ class BlankValidatorTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getInvalidValues */ - public function testInvalidValues($value) + public function testInvalidValues($value, $valueAsString) { $constraint = new Blank(array( 'message' => 'myMessage' @@ -60,7 +60,7 @@ class BlankValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $value, + '{{ value }}' => $valueAsString, )); $this->validator->validate($value, $constraint); @@ -69,10 +69,10 @@ class BlankValidatorTest extends \PHPUnit_Framework_TestCase public function getInvalidValues() { return array( - array('foobar'), - array(0), - array(false), - array(1234), + array('foobar', '"foobar"'), + array(0, '0'), + array(false, 'false'), + array(1234, '1234'), ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 9267fdb872..ad524b87ea 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -158,7 +158,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => 'baz', + '{{ value }}' => '"baz"', ), null, null); $this->validator->validate('baz', $constraint); @@ -175,7 +175,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => 'baz', + '{{ value }}' => '"baz"', )); $this->validator->validate(array('foo', 'baz'), $constraint); @@ -255,7 +255,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => '2', + '{{ value }}' => '"2"', )); $this->validator->validate('2', $constraint); @@ -287,7 +287,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => '3', + '{{ value }}' => '"3"', )); $this->validator->validate(array(2, '3'), $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php index 45c9bd1dee..2cc624e7f3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php @@ -49,9 +49,9 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase public function provideInvalidComparisons() { return array( - array(1, 2, '2', 'integer'), - array('22', '333', "'333'", 'string'), - array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime') + array(1, '1', 2, '2', 'integer'), + 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') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php index 9393bec619..8dd27f7f4b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php @@ -51,9 +51,9 @@ class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCas public function provideInvalidComparisons() { return array( - array(1, 2, '2', 'integer'), - array(new \DateTime('2000/01/01'), new \DateTime('2005/01/01'), '2005-01-01 00:00:00', 'DateTime'), - array('b', 'c', "'c'", 'string') + array(1, '1', 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('b', '"b"', 'c', '"c"', 'string') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php index 49c31727ff..e261c42c8b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php @@ -48,12 +48,12 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase public function provideInvalidComparisons() { return array( - array(1, 2, '2', 'integer'), - array(2, 2, '2', 'integer'), - array(new \DateTime('2000/01/01'), new \DateTime('2005/01/01'), '2005-01-01 00:00:00', 'DateTime'), - array(new \DateTime('2000/01/01'), new \DateTime('2000/01/01'), '2000-01-01 00:00:00', 'DateTime'), - array('22', '333', "'333'", 'string'), - array('22', '22', "'22'", 'string') + array(1, '1', 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('2000/01/01'), 'Jan 1, 2000 12:00 AM', 'DateTime'), + array('22', '"22"', '333', '"333"', 'string'), + array('22', '"22"', '22', '"22"', 'string') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php index ede2e43e7e..02f166c7cb 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php @@ -50,11 +50,11 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase public function provideInvalidComparisons() { return array( - array(1, 2, '2', 'integer'), - array(2, '2', "'2'", 'string'), - array('22', '333', "'333'", 'string'), - array(new \DateTime('2001-01-01'), new \DateTime('2001-01-01'), '2001-01-01 00:00:00', 'DateTime'), - array(new \DateTime('2001-01-01'), new \DateTime('1999-01-01'), '1999-01-01 00:00:00', 'DateTime') + array(1, '1', 2, '2', 'integer'), + array(2, '2', '2', '"2"', '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('1999-01-01'), 'Jan 1, 1999 12:00 AM', 'DateTime') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php index 9eca353e48..e2ce1bcbc1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php @@ -51,9 +51,9 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase public function provideInvalidComparisons() { return array( - array(2, 1, '1', 'integer'), - array(new \DateTime('2010-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'), - array('c', 'b', "'b'", 'string') + 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('c', '"c"', 'b', '"b"', 'string') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php index daee667254..9e88ac1e2b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php @@ -48,11 +48,11 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase public function provideInvalidComparisons() { return array( - array(3, 2, '2', 'integer'), - array(2, 2, '2', 'integer'), - array(new \DateTime('2010-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'), - array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'), - array('333', '22', "'22'", 'string'), + array(3, '3', 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('2000-01-01'), 'Jan 1, 2000 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000 12:00 AM', 'DateTime'), + array('333', '"333"', '22', '"22"', 'string'), ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php index ecf89d79de..334aeb219a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php @@ -48,10 +48,10 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase public function provideInvalidComparisons() { return array( - array(3, 3, '3', 'integer'), - array('2', 2, '2', 'integer'), - array('a', 'a', "'a'", 'string'), - array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime') + array(3, '3', 3, '3', 'integer'), + array('2', '"2"', 2, '2', 'integer'), + 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') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php index 6b518c92a9..406a10163f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php @@ -52,9 +52,9 @@ class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase $date = new \DateTime('2000-01-01'); return array( - array(3, 3, '3', 'integer'), - array('a', 'a', "'a'", 'string'), - array($date, $date, '2000-01-01 00:00:00', 'DateTime') + array(3, '3', 3, '3', 'integer'), + array('a', '"a"', 'a', '"a"', 'string'), + array($date, 'Jan 1, 2000 12:00 AM', $date, 'Jan 1, 2000 12:00 AM', 'DateTime') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php index e5e6d0bf02..57d7f5c4c4 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php @@ -132,36 +132,37 @@ class TypeValidatorTest extends \PHPUnit_Framework_TestCase { $object = new \stdClass(); $file = $this->createFile(); + $fileAsString = 'Resource(stream#'.intval($file).')'; return array( - array('foobar', 'numeric', 'foobar'), - array('foobar', 'boolean', 'foobar'), - array('0', 'integer', '0'), - array('1.5', 'float', '1.5'), + array('foobar', 'numeric', '"foobar"'), + array('foobar', 'boolean', '"foobar"'), + array('0', 'integer', '"0"'), + array('1.5', 'float', '"1.5"'), array(12345, 'string', '12345'), - array($object, 'boolean', 'stdClass'), - array($object, 'numeric', 'stdClass'), - array($object, 'integer', 'stdClass'), - array($object, 'float', 'stdClass'), - array($object, 'string', 'stdClass'), - array($object, 'resource', 'stdClass'), - array($file, 'boolean', (string) $file), - array($file, 'numeric', (string) $file), - array($file, 'integer', (string) $file), - array($file, 'float', (string) $file), - array($file, 'string', (string) $file), - array($file, 'object', (string) $file), - array('12a34', 'digit', '12a34'), - array('1a#23', 'alnum', '1a#23'), - array('abcd1', 'alpha', 'abcd1'), - array("\nabc", 'cntrl', "\nabc"), - array("abc\n", 'graph', "abc\n"), - array('abCDE', 'lower', 'abCDE'), - array('ABcde', 'upper', 'ABcde'), - array("\nabc", 'print', "\nabc"), - array('abc&$!', 'punct', 'abc&$!'), - array("\nabc", 'space', "\nabc"), - array('AR1012', 'xdigit', 'AR1012'), + array($object, 'boolean', 'Object(stdClass)'), + array($object, 'numeric', 'Object(stdClass)'), + array($object, 'integer', 'Object(stdClass)'), + array($object, 'float', 'Object(stdClass)'), + array($object, 'string', 'Object(stdClass)'), + array($object, 'resource', 'Object(stdClass)'), + array($file, 'boolean', $fileAsString), + array($file, 'numeric', $fileAsString), + array($file, 'integer', $fileAsString), + array($file, 'float', $fileAsString), + array($file, 'string', $fileAsString), + array($file, 'object', $fileAsString), + array('12a34', 'digit', '"12a34"'), + array('1a#23', 'alnum', '"1a#23"'), + array('abcd1', 'alpha', '"abcd1"'), + array("\nabc", 'cntrl', "\"\nabc\""), + array("abc\n", 'graph', "\"abc\n\""), + array('abCDE', 'lower', '"abCDE"'), + array('ABcde', 'upper', '"ABcde"'), + array("\nabc", 'print', "\"\nabc\""), + array('abc&$!', 'punct', '"abc&$!"'), + array("\nabc", 'space', "\"\nabc\""), + array('AR1012', 'xdigit', '"AR1012"'), ); } From fd58870ac03aa2f0ca258164a876c43af4a43f61 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 10 Apr 2014 18:24:43 +0200 Subject: [PATCH 03/24] [Validator] Simplified IBAN validation algorithm --- .../Validator/Constraints/IbanValidator.php | 101 ++++++++++++++---- .../Tests/Constraints/IbanValidatorTest.php | 2 + 2 files changed, 82 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/IbanValidator.php b/src/Symfony/Component/Validator/Constraints/IbanValidator.php index 640da3c702..615518883b 100644 --- a/src/Symfony/Component/Validator/Constraints/IbanValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IbanValidator.php @@ -13,10 +13,12 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Manuel Reinhard * @author Michael Schummel + * @author Bernhard Schussek * @link http://www.michael-schummel.de/2007/10/05/iban-prufung-mit-php/ */ class IbanValidator extends ConstraintValidator @@ -30,41 +32,98 @@ class IbanValidator extends ConstraintValidator return; } - // An IBAN without a country code is not an IBAN. - if (0 === preg_match('/[A-Z]/', $value)) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { + throw new UnexpectedTypeException($value, 'string'); + } + + // Remove spaces + $canonicalized = str_replace(' ', '', $value); + + if (strlen($canonicalized) < 4) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); return; } - $teststring = preg_replace('/\s+/', '', $value); - - if (strlen($teststring) < 4) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + // The IBAN must have at least 4 characters, start with a country + // code and contain only digits and (uppercase) characters + if (strlen($canonicalized) < 4 || !ctype_upper($canonicalized{0}) + || !ctype_upper($canonicalized{1}) || !ctype_alnum($canonicalized)) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); return; } - $teststring = substr($teststring, 4) - .strval(ord($teststring{0}) - 55) - .strval(ord($teststring{1}) - 55) - .substr($teststring, 2, 2); + // Move the first four characters to the end + // e.g. CH93 0076 2011 6238 5295 7 + // -> 0076 2011 6238 5295 7 CH93 + $canonicalized = substr($canonicalized, 4).substr($canonicalized, 0, 4); - $teststring = preg_replace_callback('/[A-Z]/', function ($letter) { - return intval(ord(strtolower($letter[0])) - 87); - }, $teststring); + // Convert all remaining letters to their ordinals + // The result is an integer, which is too large for PHP's int + // data type, so we store it in a string instead. + // e.g. 0076 2011 6238 5295 7 CH93 + // -> 0076 2011 6238 5295 7 121893 + $checkSum = $this->toBigInt($canonicalized); - $rest = 0; - $strlen = strlen($teststring); - for ($pos = 0; $pos < $strlen; $pos += 7) { - $part = strval($rest).substr($teststring, $pos, 7); - $rest = intval($part) % 97; + if (false === $checkSum) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); + + return; } - if ($rest != 1) { - $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); + // Do a modulo-97 operation on the large integer + // We cannot use PHP's modulo operator, so we calculate the + // modulo step-wisely instead + if (1 !== $this->bigModulo97($checkSum)) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); return; } } + + private function toBigInt($string) + { + $chars = str_split($string); + $bigInt = ''; + + foreach ($chars as $char) { + // Convert uppercase characters to ordinals, starting with 10 for "A" + if (ctype_upper($char)) { + $bigInt .= (ord($char) - 55); + + continue; + } + + // Disallow lowercase characters + if (ctype_lower($char)) { + return false; + } + + // Simply append digits + $bigInt .= $char; + } + + return $bigInt; + } + + private function bigModulo97($bigInt) + { + $parts = str_split($bigInt, 7); + $rest = 0; + + foreach ($parts as $part) { + $rest = ($rest.$part) % 97; + } + + return $rest; + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php index 60ba94c32c..cf0802fed2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php @@ -54,6 +54,7 @@ class IbanValidatorTest extends \PHPUnit_Framework_TestCase { return array( array('CH9300762011623852957'), // Switzerland without spaces + array('CH93 0076 2011 6238 5295 7'), // Switzerland with multiple spaces //Country list //http://www.rbs.co.uk/corporate/international/g0/guide-to-international-business/regulatory-information/iban/iban-example.ashx @@ -182,6 +183,7 @@ class IbanValidatorTest extends \PHPUnit_Framework_TestCase array('foo'), array('123'), array('0750447346'), + array('CH930076201162385295]'), //Ibans with lower case values are invalid array('Ae260211000000230064016'), From 224e70f709a449138b1f99e23a3f699cff950052 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 10 Apr 2014 18:57:38 +0200 Subject: [PATCH 04/24] [Validator] Fixed and simplified IsbnValidator --- .../Component/Validator/Constraints/Isbn.php | 3 + .../Validator/Constraints/IsbnValidator.php | 105 +++++++++++------- .../Tests/Constraints/IsbnValidatorTest.php | 8 ++ 3 files changed, 73 insertions(+), 43 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Isbn.php b/src/Symfony/Component/Validator/Constraints/Isbn.php index 6c978cf3b8..bd61f4b539 100644 --- a/src/Symfony/Component/Validator/Constraints/Isbn.php +++ b/src/Symfony/Component/Validator/Constraints/Isbn.php @@ -42,5 +42,8 @@ class Isbn extends Constraint if (null === $this->isbn10 && null === $this->isbn13) { throw new MissingOptionsException(sprintf('Either option "isbn10" or "isbn13" must be given for constraint "%s".', __CLASS__), array('isbn10', 'isbn13')); } + + $this->isbn10 = (bool) $this->isbn10; + $this->isbn13 = (bool) $this->isbn13; } } diff --git a/src/Symfony/Component/Validator/Constraints/IsbnValidator.php b/src/Symfony/Component/Validator/Constraints/IsbnValidator.php index a000e382ed..0e79c3289a 100644 --- a/src/Symfony/Component/Validator/Constraints/IsbnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IsbnValidator.php @@ -19,6 +19,7 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; * Validates whether the value is a valid ISBN-10 or ISBN-13. * * @author The Whole Life To Learn + * @author Bernhard Schussek * * @see https://en.wikipedia.org/wiki/Isbn */ @@ -37,53 +38,71 @@ class IsbnValidator extends ConstraintValidator throw new UnexpectedTypeException($value, 'string'); } - if (!is_numeric($value)) { - $value = str_replace('-', '', $value); + $value = (string) $value; + $canonical = strtoupper(str_replace('-', '', $value)); + + if ($constraint->isbn10 && $this->isValidIsbn10($canonical)) { + return; } - $validation = 0; - $value = strtoupper($value); - $valueLength = strlen($value); + if ($constraint->isbn13 && $this->isValidIsbn13($canonical)) { + return; + } - if (10 === $valueLength && null !== $constraint->isbn10) { - for ($i = 0; $i < 10; $i++) { - if ($value[$i] == 'X') { - $validation += 10 * intval(10 - $i); - } else { - $validation += intval($value[$i]) * intval(10 - $i); - } - } - - if ($validation % 11 != 0) { - if (null !== $constraint->isbn13) { - $this->context->addViolation($constraint->bothIsbnMessage); - } else { - $this->context->addViolation($constraint->isbn10Message); - } - } - } elseif (13 === $valueLength && null !== $constraint->isbn13) { - for ($i = 0; $i < 13; $i += 2) { - $validation += intval($value[$i]); - } - for ($i = 1; $i < 12; $i += 2) { - $validation += intval($value[$i]) * 3; - } - - if ($validation % 10 != 0) { - if (null !== $constraint->isbn10) { - $this->context->addViolation($constraint->bothIsbnMessage); - } else { - $this->context->addViolation($constraint->isbn13Message); - } - } + if ($constraint->isbn10 && $constraint->isbn13) { + $this->context->addViolation($constraint->bothIsbnMessage, array( + '{{ value }}' => $value, + )); + } elseif ($constraint->isbn10) { + $this->context->addViolation($constraint->isbn10Message, array( + '{{ value }}' => $value, + )); } else { - if (null !== $constraint->isbn10 && null !== $constraint->isbn13) { - $this->context->addViolation($constraint->bothIsbnMessage); - } elseif (null !== $constraint->isbn10) { - $this->context->addViolation($constraint->isbn10Message); - } else { - $this->context->addViolation($constraint->isbn13Message); - } + $this->context->addViolation($constraint->isbn13Message, array( + '{{ value }}' => $value, + )); } } + + private function isValidIsbn10($isbn) + { + if (10 !== strlen($isbn)) { + return false; + } + + $checkSum = 0; + + for ($i = 0; $i < 10; ++$i) { + if ('X' === $isbn{$i}) { + $digit = 10; + } elseif (ctype_digit($isbn{$i})) { + $digit = $isbn{$i}; + } else { + return false; + } + + $checkSum += $digit * intval(10 - $i); + } + + return 0 === $checkSum % 11; + } + + private function isValidIsbn13($isbn) + { + if (13 !== strlen($isbn) || !ctype_digit($isbn)) { + return false; + } + + $checkSum = 0; + + for ($i = 0; $i < 13; $i += 2) { + $checkSum += $isbn{$i}; + } + + for ($i = 1; $i < 12; $i += 2) { + $checkSum += $isbn{$i} * 3; + } + + return 0 === $checkSum % 10; + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php index 7f0859b999..049a9ac3ee 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php @@ -58,6 +58,10 @@ class IsbnValidatorTest extends \PHPUnit_Framework_TestCase array('0-4X19-92611'), array('0_45122_5244'), array('2870#971#648'), + array('1A34567890'), + // chr(1) evaluates to 0 + // 2070546810 is valid + array('2'.chr(1).'70546810'), ); } @@ -92,6 +96,10 @@ class IsbnValidatorTest extends \PHPUnit_Framework_TestCase array('980-0474292319'), array('978_0451225245'), array('978#0471292319'), + array('978-272C442282'), + // chr(1) evaluates to 0 + // 978-2070546817 is valid + array('978-2'.chr(1).'70546817'), ); } From bff09f210b65f1916a6402ac280db2f5d373c84f Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 10 Apr 2014 19:05:39 +0200 Subject: [PATCH 05/24] [Validator] Simplified IssnValidator --- .../Validator/Constraints/IssnValidator.php | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/IssnValidator.php b/src/Symfony/Component/Validator/Constraints/IssnValidator.php index 0e580ea96c..2ae70ec2f3 100644 --- a/src/Symfony/Component/Validator/Constraints/IssnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IssnValidator.php @@ -37,26 +37,35 @@ class IssnValidator extends ConstraintValidator throw new UnexpectedTypeException($value, 'string'); } + $value = (string) $value; + // Compose regex pattern $digitsPattern = $constraint->requireHyphen ? '\d{4}-\d{3}' : '\d{4}-?\d{3}'; - $checksumPattern = $constraint->caseSensitive ? '[\d|X]' : '[\d|X|x]'; - $pattern = "/^".$digitsPattern.$checksumPattern."$/"; + $checkSumPattern = $constraint->caseSensitive ? '[\d|X]' : '[\d|X|x]'; + $pattern = "/^".$digitsPattern.$checkSumPattern."$/"; if (!preg_match($pattern, $value)) { - $this->context->addViolation($constraint->message); - } else { - $digits = str_split(strtoupper(str_replace('-', '', $value))); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); - $sum = 0; - for ($i = 8; $i > 1; $i--) { - $sum += $i * (int) array_shift($digits); - } + return; + } - $checksum = 'X' == reset($digits) ? 10 : (int) reset($digits); + $canonical = strtoupper(str_replace('-', '', $value)); - if (0 != ($sum + $checksum) % 11) { - $this->context->addViolation($constraint->message); - } + // Calculate a checksum. "X" equals 10. + $checkSum = 'X' === $canonical{7} ? 10 : $canonical{7}; + + for ($i = 0; $i < 7; ++$i) { + // Multiply the first digit by 8, the second by 7, etc. + $checkSum += (8-$i) * $canonical{$i}; + } + + if (0 !== $checkSum % 11) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } From f3295522efcd5d0bd43eff48e16592051bf2c915 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 10 Apr 2014 19:30:28 +0200 Subject: [PATCH 06/24] [Validator] Simplified and explained the LuhnValidator --- .../Validator/Constraints/LuhnValidator.php | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php index d3802fe99a..444696994f 100644 --- a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php @@ -39,28 +39,48 @@ class LuhnValidator extends ConstraintValidator return; } - /** - * need to work with strings only because long numbers are treated as floats and don't work with strlen - */ - if (!is_string($value)) { + // Work with strings only, because long numbers are represented as floats + // internally and don't work with strlen() + if (!is_string($value) && !(is_object($value) && method_exists($value, '__toString'))) { throw new UnexpectedTypeException($value, 'string'); } - if (!is_numeric($value)) { - $this->context->addViolation($constraint->message); + $value = (string) $value; + + if (!ctype_digit($value)) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); return; } + $checkSum = 0; $length = strlen($value); - $oddLength = $length % 2; - for ($sum = 0, $i = $length - 1; $i >= 0; $i--) { - $digit = (int) $value[$i]; - $sum += (($i % 2) === $oddLength) ? array_sum(str_split($digit * 2)) : $digit; + + // Starting with the last digit and walking left, add every second + // digit to the check sum + // e.g. 7 9 9 2 7 3 9 8 7 1 3 + // ^ ^ ^ ^ ^ ^ + // = 7 + 9 + 7 + 9 + 7 + 3 + for ($i = $length - 1; $i >= 0; $i -= 2) { + $checkSum += $value{$i}; } - if ($sum === 0 || ($sum % 10) !== 0) { - $this->context->addViolation($constraint->message); + // Starting with the second last digit and walking left, double every + // second digit and add it to the check sum + // For doubles greater than 9, sum the individual digits + // e.g. 7 9 9 2 7 3 9 8 7 1 3 + // ^ ^ ^ ^ ^ + // = 1+8 + 4 + 6 + 1+6 + 2 + for ($i = $length - 2; $i >= 0; $i -= 2) { + $checkSum += array_sum(str_split($value{$i} * 2)); + } + + if (0 === $checkSum || 0 !== $checkSum % 10) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $value, + )); } } } From 5aa7e6dbe0d27c9cfcfb4cb8d44da1b061c2280d Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 10 Apr 2014 19:37:34 +0200 Subject: [PATCH 07/24] [Validator] Added "{{ value }}" parameters where they were missing --- .../Component/Validator/ConstraintValidator.php | 7 ++++--- .../Constraints/AbstractComparisonValidator.php | 4 ++-- .../Validator/Constraints/FalseValidator.php | 4 +++- .../Validator/Constraints/LuhnValidator.php | 3 ++- .../Validator/Constraints/NotBlankValidator.php | 4 +++- .../Validator/Constraints/NullValidator.php | 8 +------- .../Validator/Constraints/TrueValidator.php | 4 +++- .../Tests/Constraints/FalseValidatorTest.php | 2 +- .../Tests/Constraints/NullValidatorTest.php | 17 +++++++++-------- .../Tests/Constraints/TrueValidatorTest.php | 1 + 10 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index fe3758f65b..3aaf806cc2 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -48,13 +48,14 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface /** * Returns a string representation of the value. * - * @param mixed $value + * @param mixed $value + * @param Boolean $formatDates * * @return string */ - protected function valueToString($value) + protected function valueToString($value, $formatDates = false) { - if ($value instanceof \DateTime) { + if ($formatDates && $value instanceof \DateTime) { if (class_exists('IntlDateFormatter')) { $locale = \Locale::getDefault(); $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php index 351ce73491..7beebf703c 100644 --- a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php @@ -32,8 +32,8 @@ abstract class AbstractComparisonValidator extends ConstraintValidator if (!$this->compareValues($value, $constraint->value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value), - '{{ compared_value }}' => $this->valueToString($constraint->value), + '{{ value }}' => $this->valueToString($value, true), + '{{ compared_value }}' => $this->valueToString($constraint->value, true), '{{ compared_value_type }}' => $this->valueToType($constraint->value) )); } diff --git a/src/Symfony/Component/Validator/Constraints/FalseValidator.php b/src/Symfony/Component/Validator/Constraints/FalseValidator.php index 4e8c4df269..76e370f348 100644 --- a/src/Symfony/Component/Validator/Constraints/FalseValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FalseValidator.php @@ -30,6 +30,8 @@ class FalseValidator extends ConstraintValidator return; } - $this->context->addViolation($constraint->message); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->valueToString($value), + )); } } diff --git a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php index 444696994f..4a59e9559d 100644 --- a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php @@ -24,11 +24,12 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; * @see http://en.wikipedia.org/wiki/Luhn_algorithm * @author Tim Nagel * @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/ + * @author Bernhard Schussek */ class LuhnValidator extends ConstraintValidator { /** - * Validates a creditcard number with the Luhn algorithm. + * Validates a credit card number with the Luhn algorithm. * * @param mixed $value * @param Constraint $constraint diff --git a/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php b/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php index 98c2730fe8..20675a8da9 100644 --- a/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php @@ -27,7 +27,9 @@ class NotBlankValidator extends ConstraintValidator public function validate($value, Constraint $constraint) { if (false === $value || (empty($value) && '0' != $value)) { - $this->context->addViolation($constraint->message); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->valueToString($value), + )); } } } diff --git a/src/Symfony/Component/Validator/Constraints/NullValidator.php b/src/Symfony/Component/Validator/Constraints/NullValidator.php index bc9bf4f38c..8698585ffd 100644 --- a/src/Symfony/Component/Validator/Constraints/NullValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NullValidator.php @@ -27,14 +27,8 @@ class NullValidator extends ConstraintValidator public function validate($value, Constraint $constraint) { if (null !== $value) { - if (is_object($value)) { - $value = get_class($value); - } elseif (is_array($value)) { - $value = 'Array'; - } - $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->valueToString($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/TrueValidator.php b/src/Symfony/Component/Validator/Constraints/TrueValidator.php index 5ef74fd274..69495dd6a3 100644 --- a/src/Symfony/Component/Validator/Constraints/TrueValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TrueValidator.php @@ -31,7 +31,9 @@ class TrueValidator extends ConstraintValidator } if (true !== $value && 1 !== $value && '1' !== $value) { - $this->context->addViolation($constraint->message); + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->valueToString($value), + )); } } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php index 1bc16c20bd..22ab63a9e2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php @@ -56,7 +56,7 @@ class FalseValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') - ->with('myMessage', array()); + ->with('myMessage', array('{{ value }}' => 'true')); $this->validator->validate(true, $constraint); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php index d343c869fd..cbf8da1f38 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php @@ -43,7 +43,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getInvalidValues */ - public function testInvalidValues($value, $readableValue) + public function testInvalidValues($value, $valueAsString) { $constraint = new Null(array( 'message' => 'myMessage' @@ -52,7 +52,7 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $readableValue, + '{{ value }}' => $valueAsString, )); $this->validator->validate($value, $constraint); @@ -61,12 +61,13 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase public function getInvalidValues() { return array( - array(0, 0), - array(false, false), - array(true, true), - array('', ''), - array('foo bar', 'foo bar'), - array(new \DateTime(), 'DateTime'), + array(0, '0'), + array(false, 'false'), + array(true, 'true'), + array('', '""'), + array('foo bar', '"foo bar"'), + array(new \DateTime(), 'Object(DateTime)'), + array(new \stdClass(), 'Object(stdClass)'), array(array(), 'Array'), ); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php index 25901793a0..5a42912edf 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php @@ -57,6 +57,7 @@ class TrueValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( + '{{ value }}' => 'false', )); $this->validator->validate(false, $constraint); From cea4155d3982c4f7842b4207230fbeae47b4843d Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 22 May 2014 10:30:24 +0200 Subject: [PATCH 08/24] [Validator] Fixed date-to-string conversion tests to match ICU 51 --- .../Component/Validator/ConstraintValidator.php | 17 +++++++++++++++++ .../Validator/Constraints/FileValidator.php | 4 ++-- .../AbstractComparisonValidatorTestCase.php | 8 ++++++++ .../Tests/Constraints/EqualToValidatorTest.php | 2 +- .../GreaterThanOrEqualValidatorTest.php | 2 +- .../Constraints/GreaterThanValidatorTest.php | 4 ++-- .../Constraints/IdenticalToValidatorTest.php | 4 ++-- .../LessThanOrEqualValidatorTest.php | 2 +- .../Tests/Constraints/LessThanValidatorTest.php | 4 ++-- .../Constraints/NotEqualToValidatorTest.php | 2 +- .../Constraints/NotIdenticalToValidatorTest.php | 2 +- 11 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index 3aaf806cc2..ac6a64dc37 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -96,4 +96,21 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface return (string) $value; } + + /** + * Returns a string representation of a list of values. + * + * @param array $values + * @param Boolean $formatDates + * + * @return string + */ + protected function valuesToString(array $values, $formatDates = false) + { + foreach ($values as $key => $value) { + $values[$key] = $this->valueToString($value, $formatDates); + } + + return implode(', ', $values); + } } diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index dce0480a0e..24e64f9ee9 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -161,8 +161,8 @@ class FileValidator extends ConstraintValidator if (false === $valid) { $this->context->addViolation($constraint->mimeTypesMessage, array( - '{{ type }}' => '"'.$mime.'"', - '{{ types }}' => '"'.implode('", "', $mimeTypes) .'"', + '{{ type }}' => $this->valueToString($mime), + '{{ types }}' => $this->valuesToString($mimeTypes), '{{ file }}' => $path, )); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php index c0ab0f7a53..80690d29e0 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Component\Intl\Util\IntlTestHelper; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\AbstractComparisonValidator; @@ -73,12 +74,19 @@ abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_Te /** * @dataProvider provideInvalidComparisons * @param mixed $dirtyValue + * @param mixed $dirtyValueAsString * @param mixed $comparedValue * @param mixed $comparedValueString * @param string $comparedValueType */ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType) { + // Conversion of dates to string differs between ICU versions + // Make sure we have the correct version loaded + if ($dirtyValue instanceof \DateTime) { + IntlTestHelper::requireIntl($this); + } + $constraint = $this->createConstraint(array('value' => $comparedValue)); $constraint->message = 'Constraint Message'; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php index 2cc624e7f3..783e5508d7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php @@ -51,7 +51,7 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase return array( array(1, '1', 2, '2', 'integer'), 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') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php index 8dd27f7f4b..bb90f7c283 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php @@ -52,7 +52,7 @@ class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCas { return array( array(1, '1', 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('b', '"b"', 'c', '"c"', 'string') ); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php index e261c42c8b..14a7a6d16f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php @@ -50,8 +50,8 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase return array( array(1, '1', 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('2000/01/01'), 'Jan 1, 2000 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('22', '"22"', '333', '"333"', 'string'), array('22', '"22"', '22', '"22"', 'string') ); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php index 02f166c7cb..9c08523386 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php @@ -53,8 +53,8 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase array(1, '1', 2, '2', 'integer'), array(2, '2', '2', '"2"', '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('1999-01-01'), 'Jan 1, 1999 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') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php index e2ce1bcbc1..fc41811a76 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php @@ -52,7 +52,7 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase { return array( 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('c', '"c"', 'b', '"b"', 'string') ); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php index 9e88ac1e2b..5cb5673c81 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php @@ -50,8 +50,8 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase return array( array(3, '3', 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('2000-01-01'), 'Jan 1, 2000 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('333', '"333"', '22', '"22"', 'string'), ); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php index 334aeb219a..e0db97a0e7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php @@ -51,7 +51,7 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase array(3, '3', 3, '3', 'integer'), array('2', '"2"', 2, '2', 'integer'), 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') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php index 406a10163f..ad2865c457 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php @@ -54,7 +54,7 @@ class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase return array( array(3, '3', 3, '3', 'integer'), 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') ); } } From 71897d7e3591d4664204da17f50c8587ccd65582 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 22 May 2014 11:22:43 +0200 Subject: [PATCH 09/24] [Validator] Fixed CS --- src/Symfony/Component/Validator/ConstraintValidator.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index ac6a64dc37..0cf50916df 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -48,8 +48,8 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface /** * Returns a string representation of the value. * - * @param mixed $value - * @param Boolean $formatDates + * @param mixed $value + * @param bool $formatDates * * @return string */ @@ -100,8 +100,8 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface /** * Returns a string representation of a list of values. * - * @param array $values - * @param Boolean $formatDates + * @param array $values + * @param bool $formatDates * * @return string */ From d6a783f989e76d844747ff4e024066b1fc423cbe Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 24 Jul 2014 14:14:19 +0200 Subject: [PATCH 10/24] [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls --- .../Validator/ConstraintValidator.php | 14 +++++------ .../AbstractComparisonValidator.php | 6 ++--- .../Validator/Constraints/BlankValidator.php | 2 +- .../Constraints/CardSchemeValidator.php | 4 ++-- .../Validator/Constraints/ChoiceValidator.php | 4 ++-- .../Constraints/CollectionValidator.php | 4 ++-- .../Constraints/CountryValidator.php | 2 +- .../Constraints/CurrencyValidator.php | 2 +- .../Validator/Constraints/DateValidator.php | 2 +- .../Validator/Constraints/EmailValidator.php | 2 +- .../Validator/Constraints/FalseValidator.php | 2 +- .../Validator/Constraints/FileValidator.php | 16 ++++++++----- .../Validator/Constraints/IbanValidator.php | 10 ++++---- .../Validator/Constraints/IpValidator.php | 2 +- .../Validator/Constraints/IsbnValidator.php | 6 ++--- .../Validator/Constraints/IssnValidator.php | 4 ++-- .../Constraints/LanguageValidator.php | 2 +- .../Validator/Constraints/LengthValidator.php | 6 ++--- .../Validator/Constraints/LocaleValidator.php | 2 +- .../Validator/Constraints/LuhnValidator.php | 4 ++-- .../Constraints/NotBlankValidator.php | 2 +- .../Validator/Constraints/NullValidator.php | 2 +- .../Validator/Constraints/RangeValidator.php | 2 +- .../Validator/Constraints/RegexValidator.php | 2 +- .../Validator/Constraints/TimeValidator.php | 2 +- .../Validator/Constraints/TrueValidator.php | 2 +- .../Validator/Constraints/TypeValidator.php | 2 +- .../Validator/Constraints/UrlValidator.php | 3 +-- .../Constraints/CollectionValidatorTest.php | 6 ++--- .../Constraints/DateTimeValidatorTest.php | 2 +- .../Tests/Constraints/DateValidatorTest.php | 2 +- .../Tests/Constraints/EmailValidatorTest.php | 2 +- .../Constraints/FileValidatorPathTest.php | 2 +- .../Tests/Constraints/FileValidatorTest.php | 10 ++++---- .../Tests/Constraints/IbanValidatorTest.php | 2 +- .../Tests/Constraints/IpValidatorTest.php | 24 +++++++++---------- .../Tests/Constraints/LengthValidatorTest.php | 6 ++--- .../Tests/Constraints/RangeValidatorTest.php | 15 ++++++++++++ .../Tests/Constraints/RegexValidatorTest.php | 2 +- .../Tests/Constraints/TimeValidatorTest.php | 2 +- .../Tests/Constraints/UrlValidatorTest.php | 2 +- 41 files changed, 105 insertions(+), 85 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index 0cf50916df..bb3378cd93 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -40,7 +40,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface * * @return string */ - protected function valueToType($value) + protected function formatTypeOf($value) { return is_object($value) ? get_class($value) : gettype($value); } @@ -49,13 +49,13 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface * Returns a string representation of the value. * * @param mixed $value - * @param bool $formatDates + * @param bool $prettyDateTime * * @return string */ - protected function valueToString($value, $formatDates = false) + protected function formatValue($value, $prettyDateTime = false) { - if ($formatDates && $value instanceof \DateTime) { + if ($prettyDateTime && $value instanceof \DateTime) { if (class_exists('IntlDateFormatter')) { $locale = \Locale::getDefault(); $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); @@ -101,14 +101,14 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface * Returns a string representation of a list of values. * * @param array $values - * @param bool $formatDates + * @param bool $prettyDateTime * * @return string */ - protected function valuesToString(array $values, $formatDates = false) + protected function formatValues(array $values, $prettyDateTime = false) { foreach ($values as $key => $value) { - $values[$key] = $this->valueToString($value, $formatDates); + $values[$key] = $this->formatValue($value, $prettyDateTime); } return implode(', ', $values); diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php index 7beebf703c..d2b15f2162 100644 --- a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php @@ -32,9 +32,9 @@ abstract class AbstractComparisonValidator extends ConstraintValidator if (!$this->compareValues($value, $constraint->value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value, true), - '{{ compared_value }}' => $this->valueToString($constraint->value, true), - '{{ compared_value_type }}' => $this->valueToType($constraint->value) + '{{ value }}' => $this->formatValue($value, true), + '{{ compared_value }}' => $this->formatValue($constraint->value, true), + '{{ compared_value_type }}' => $this->formatTypeOf($constraint->value) )); } } diff --git a/src/Symfony/Component/Validator/Constraints/BlankValidator.php b/src/Symfony/Component/Validator/Constraints/BlankValidator.php index 7bb57ddd0e..e8406a238e 100644 --- a/src/Symfony/Component/Validator/Constraints/BlankValidator.php +++ b/src/Symfony/Component/Validator/Constraints/BlankValidator.php @@ -28,7 +28,7 @@ class BlankValidator extends ConstraintValidator { if ('' !== $value && null !== $value) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value) + '{{ value }}' => $this->formatValue($value) )); } } diff --git a/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php b/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php index eae71a92d4..73f602adce 100644 --- a/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php @@ -109,7 +109,7 @@ class CardSchemeValidator extends ConstraintValidator if (!is_numeric($value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value), + '{{ value }}' => $this->formatValue($value), )); return; @@ -127,7 +127,7 @@ class CardSchemeValidator extends ConstraintValidator } $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index c4f3b56c8b..2e0658cfa6 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -60,7 +60,7 @@ class ChoiceValidator extends ConstraintValidator foreach ($value as $_value) { if (!in_array($_value, $choices, $constraint->strict)) { $this->context->addViolation($constraint->multipleMessage, array( - '{{ value }}' => $this->valueToString($_value), + '{{ value }}' => $this->formatValue($_value), )); } } @@ -84,7 +84,7 @@ class ChoiceValidator extends ConstraintValidator } } elseif (!in_array($value, $choices, $constraint->strict)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value) + '{{ value }}' => $this->formatValue($value) )); } } diff --git a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php index 538ff7e346..a322ef49ad 100644 --- a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php @@ -48,7 +48,7 @@ class CollectionValidator extends ConstraintValidator } } elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) { $this->context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array( - '{{ field }}' => $field + '{{ field }}' => $this->formatValue($field) ), null); } } @@ -57,7 +57,7 @@ class CollectionValidator extends ConstraintValidator foreach ($value as $field => $fieldValue) { if (!isset($constraint->fields[$field])) { $this->context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array( - '{{ field }}' => $field + '{{ field }}' => $this->formatValue($field) ), $fieldValue); } } diff --git a/src/Symfony/Component/Validator/Constraints/CountryValidator.php b/src/Symfony/Component/Validator/Constraints/CountryValidator.php index c912a650a6..a094c10481 100644 --- a/src/Symfony/Component/Validator/Constraints/CountryValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountryValidator.php @@ -43,7 +43,7 @@ class CountryValidator extends ConstraintValidator if (!isset($countries[$value])) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php index d12952fc81..d8c999af4d 100644 --- a/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php @@ -43,7 +43,7 @@ class CurrencyValidator extends ConstraintValidator if (!isset($currencies[$value])) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/DateValidator.php b/src/Symfony/Component/Validator/Constraints/DateValidator.php index 1170314a70..5e5250852f 100644 --- a/src/Symfony/Component/Validator/Constraints/DateValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateValidator.php @@ -41,7 +41,7 @@ class DateValidator extends ConstraintValidator if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/EmailValidator.php b/src/Symfony/Component/Validator/Constraints/EmailValidator.php index 5b433a768f..b3789a130d 100644 --- a/src/Symfony/Component/Validator/Constraints/EmailValidator.php +++ b/src/Symfony/Component/Validator/Constraints/EmailValidator.php @@ -51,7 +51,7 @@ class EmailValidator extends ConstraintValidator if (!$valid) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/FalseValidator.php b/src/Symfony/Component/Validator/Constraints/FalseValidator.php index 76e370f348..280deba3c9 100644 --- a/src/Symfony/Component/Validator/Constraints/FalseValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FalseValidator.php @@ -31,7 +31,7 @@ class FalseValidator extends ConstraintValidator } $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value), + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index 24e64f9ee9..006064594b 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -96,13 +96,17 @@ class FileValidator extends ConstraintValidator $path = $value instanceof FileObject ? $value->getPathname() : (string) $value; if (!is_file($path)) { - $this->context->addViolation($constraint->notFoundMessage, array('{{ file }}' => $path)); + $this->context->addViolation($constraint->notFoundMessage, array( + '{{ file }}' => $this->formatValue($path) + )); return; } if (!is_readable($path)) { - $this->context->addViolation($constraint->notReadableMessage, array('{{ file }}' => $path)); + $this->context->addViolation($constraint->notReadableMessage, array( + '{{ file }}' => $this->formatValue($path) + )); return; } @@ -129,7 +133,7 @@ class FileValidator extends ConstraintValidator '{{ size }}' => $size, '{{ limit }}' => $limit, '{{ suffix }}' => $suffix, - '{{ file }}' => $path, + '{{ file }}' => $this->formatValue($path), )); return; @@ -161,9 +165,9 @@ class FileValidator extends ConstraintValidator if (false === $valid) { $this->context->addViolation($constraint->mimeTypesMessage, array( - '{{ type }}' => $this->valueToString($mime), - '{{ types }}' => $this->valuesToString($mimeTypes), - '{{ file }}' => $path, + '{{ type }}' => $this->formatValue($mime), + '{{ types }}' => $this->formatValues($mimeTypes), + '{{ file }}' => $this->formatValue($path), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/IbanValidator.php b/src/Symfony/Component/Validator/Constraints/IbanValidator.php index 615518883b..699db7ea7a 100644 --- a/src/Symfony/Component/Validator/Constraints/IbanValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IbanValidator.php @@ -36,12 +36,14 @@ class IbanValidator extends ConstraintValidator throw new UnexpectedTypeException($value, 'string'); } + $value = (string) $value; + // Remove spaces $canonicalized = str_replace(' ', '', $value); if (strlen($canonicalized) < 4) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); return; @@ -52,7 +54,7 @@ class IbanValidator extends ConstraintValidator if (strlen($canonicalized) < 4 || !ctype_upper($canonicalized{0}) || !ctype_upper($canonicalized{1}) || !ctype_alnum($canonicalized)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); return; @@ -72,7 +74,7 @@ class IbanValidator extends ConstraintValidator if (false === $checkSum) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); return; @@ -83,7 +85,7 @@ class IbanValidator extends ConstraintValidator // modulo step-wisely instead if (1 !== $this->bigModulo97($checkSum)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); return; diff --git a/src/Symfony/Component/Validator/Constraints/IpValidator.php b/src/Symfony/Component/Validator/Constraints/IpValidator.php index ad5d9e2466..5ebda4e9e2 100644 --- a/src/Symfony/Component/Validator/Constraints/IpValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IpValidator.php @@ -92,7 +92,7 @@ class IpValidator extends ConstraintValidator if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/IsbnValidator.php b/src/Symfony/Component/Validator/Constraints/IsbnValidator.php index 0e79c3289a..5fee10e718 100644 --- a/src/Symfony/Component/Validator/Constraints/IsbnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IsbnValidator.php @@ -51,15 +51,15 @@ class IsbnValidator extends ConstraintValidator if ($constraint->isbn10 && $constraint->isbn13) { $this->context->addViolation($constraint->bothIsbnMessage, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } elseif ($constraint->isbn10) { $this->context->addViolation($constraint->isbn10Message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } else { $this->context->addViolation($constraint->isbn13Message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/IssnValidator.php b/src/Symfony/Component/Validator/Constraints/IssnValidator.php index 2ae70ec2f3..301b40d2d9 100644 --- a/src/Symfony/Component/Validator/Constraints/IssnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/IssnValidator.php @@ -46,7 +46,7 @@ class IssnValidator extends ConstraintValidator if (!preg_match($pattern, $value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); return; @@ -64,7 +64,7 @@ class IssnValidator extends ConstraintValidator if (0 !== $checkSum % 11) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php index 02edb963f1..8b048b8a48 100644 --- a/src/Symfony/Component/Validator/Constraints/LanguageValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LanguageValidator.php @@ -43,7 +43,7 @@ class LanguageValidator extends ConstraintValidator if (!isset($languages[$value])) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/LengthValidator.php b/src/Symfony/Component/Validator/Constraints/LengthValidator.php index 8dc68483c8..ceccde5a61 100644 --- a/src/Symfony/Component/Validator/Constraints/LengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LengthValidator.php @@ -45,7 +45,7 @@ class LengthValidator extends ConstraintValidator if ($constraint->min == $constraint->max && $length != $constraint->min) { $this->context->addViolation($constraint->exactMessage, array( - '{{ value }}' => $stringValue, + '{{ value }}' => $this->formatValue($stringValue), '{{ limit }}' => $constraint->min, ), $value, (int) $constraint->min); @@ -54,7 +54,7 @@ class LengthValidator extends ConstraintValidator if (null !== $constraint->max && $length > $constraint->max) { $this->context->addViolation($constraint->maxMessage, array( - '{{ value }}' => $stringValue, + '{{ value }}' => $this->formatValue($stringValue), '{{ limit }}' => $constraint->max, ), $value, (int) $constraint->max); @@ -63,7 +63,7 @@ class LengthValidator extends ConstraintValidator if (null !== $constraint->min && $length < $constraint->min) { $this->context->addViolation($constraint->minMessage, array( - '{{ value }}' => $stringValue, + '{{ value }}' => $this->formatValue($stringValue), '{{ limit }}' => $constraint->min, ), $value, (int) $constraint->min); } diff --git a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php index cfd2105e90..1d887d58f0 100644 --- a/src/Symfony/Component/Validator/Constraints/LocaleValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LocaleValidator.php @@ -43,7 +43,7 @@ class LocaleValidator extends ConstraintValidator if (!isset($locales[$value])) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php index 4a59e9559d..1cc7a7b22f 100644 --- a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php @@ -50,7 +50,7 @@ class LuhnValidator extends ConstraintValidator if (!ctype_digit($value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); return; @@ -80,7 +80,7 @@ class LuhnValidator extends ConstraintValidator if (0 === $checkSum || 0 !== $checkSum % 10) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php b/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php index 20675a8da9..945803348b 100644 --- a/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php @@ -28,7 +28,7 @@ class NotBlankValidator extends ConstraintValidator { if (false === $value || (empty($value) && '0' != $value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value), + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/NullValidator.php b/src/Symfony/Component/Validator/Constraints/NullValidator.php index 8698585ffd..9c5deb36e7 100644 --- a/src/Symfony/Component/Validator/Constraints/NullValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NullValidator.php @@ -28,7 +28,7 @@ class NullValidator extends ConstraintValidator { if (null !== $value) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value), + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php index fe79f54222..c2df195b81 100644 --- a/src/Symfony/Component/Validator/Constraints/RangeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php @@ -30,7 +30,7 @@ class RangeValidator extends ConstraintValidator if (!is_numeric($value)) { $this->context->addViolation($constraint->invalidMessage, array( - '{{ value }}' => $this->valueToString($value), + '{{ value }}' => $this->formatValue($value), )); return; diff --git a/src/Symfony/Component/Validator/Constraints/RegexValidator.php b/src/Symfony/Component/Validator/Constraints/RegexValidator.php index f59d6a4e25..88ea8ae05b 100644 --- a/src/Symfony/Component/Validator/Constraints/RegexValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RegexValidator.php @@ -42,7 +42,7 @@ class RegexValidator extends ConstraintValidator if ($constraint->match xor preg_match($constraint->pattern, $value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/TimeValidator.php b/src/Symfony/Component/Validator/Constraints/TimeValidator.php index 36260e67d1..a7c2b27ad0 100644 --- a/src/Symfony/Component/Validator/Constraints/TimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimeValidator.php @@ -41,7 +41,7 @@ class TimeValidator extends ConstraintValidator if (!preg_match(static::PATTERN, $value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/TrueValidator.php b/src/Symfony/Component/Validator/Constraints/TrueValidator.php index 69495dd6a3..480230a909 100644 --- a/src/Symfony/Component/Validator/Constraints/TrueValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TrueValidator.php @@ -32,7 +32,7 @@ class TrueValidator extends ConstraintValidator if (true !== $value && 1 !== $value && '1' !== $value) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value), + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Constraints/TypeValidator.php b/src/Symfony/Component/Validator/Constraints/TypeValidator.php index ff713ffeef..237546a5b9 100644 --- a/src/Symfony/Component/Validator/Constraints/TypeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TypeValidator.php @@ -44,7 +44,7 @@ class TypeValidator extends ConstraintValidator } $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->valueToString($value), + '{{ value }}' => $this->formatValue($value), '{{ type }}' => $constraint->type, )); } diff --git a/src/Symfony/Component/Validator/Constraints/UrlValidator.php b/src/Symfony/Component/Validator/Constraints/UrlValidator.php index 4aeab117af..748f824894 100644 --- a/src/Symfony/Component/Validator/Constraints/UrlValidator.php +++ b/src/Symfony/Component/Validator/Constraints/UrlValidator.php @@ -51,12 +51,11 @@ class UrlValidator extends ConstraintValidator } $value = (string) $value; - $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols)); if (!preg_match($pattern, $value)) { $this->context->addViolation($constraint->message, array( - '{{ value }}' => $value, + '{{ value }}' => $this->formatValue($value), )); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php index 4a13234b69..e966aadae7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php @@ -147,7 +147,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolationAt') ->with('[baz]', 'myMessage', array( - '{{ field }}' => 'baz' + '{{ field }}' => '"baz"' )); $this->validator->validate($data, new Collection(array( @@ -211,7 +211,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolationAt') ->with('[foo]', 'myMessage', array( - '{{ field }}' => 'foo', + '{{ field }}' => '"foo"', )); $this->validator->validate($data, $constraint); @@ -331,7 +331,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolationAt') ->with('[foo]', 'myMessage', array( - '{{ field }}' => 'foo', + '{{ field }}' => '"foo"', )); $this->validator->validate($data, new Collection(array( diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index 0776848fdb..46079a8155 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -96,7 +96,7 @@ class DateTimeValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $dateTime, + '{{ value }}' => '"'.$dateTime.'"', )); $this->validator->validate($dateTime, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php index b9cf8816b7..a62426ec8a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php @@ -96,7 +96,7 @@ class DateValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $date, + '{{ value }}' => '"'.$date.'"', )); $this->validator->validate($date, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php index 701ab1f556..4b7a8bd6c3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php @@ -88,7 +88,7 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $email, + '{{ value }}' => '"'.$email.'"', )); $this->validator->validate($email, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php index c4b93cdcd3..7701d7faf4 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php @@ -29,7 +29,7 @@ class FileValidatorPathTest extends FileValidatorTest $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ file }}' => 'foobar', + '{{ file }}' => '"foobar"', )); $this->validator->validate('foobar', $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index fcbbb04b83..47952f061c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -101,7 +101,7 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase '{{ limit }}' => '10', '{{ size }}' => '11', '{{ suffix }}' => 'bytes', - '{{ file }}' => $this->path, + '{{ file }}' => '"'.$this->path.'"', )); $this->validator->validate($this->getFile($this->path), $constraint); @@ -122,7 +122,7 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase '{{ limit }}' => '1', '{{ size }}' => '1.4', '{{ suffix }}' => 'kB', - '{{ file }}' => $this->path, + '{{ file }}' => '"'.$this->path.'"', )); $this->validator->validate($this->getFile($this->path), $constraint); @@ -143,7 +143,7 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase '{{ limit }}' => '1', '{{ size }}' => '1.4', '{{ suffix }}' => 'MB', - '{{ file }}' => $this->path, + '{{ file }}' => '"'.$this->path.'"', )); $this->validator->validate($this->getFile($this->path), $constraint); @@ -245,7 +245,7 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase ->with('myMessage', array( '{{ type }}' => '"application/pdf"', '{{ types }}' => '"image/png", "image/jpg"', - '{{ file }}' => $this->path, + '{{ file }}' => '"'.$this->path.'"', )); $this->validator->validate($file, $constraint); @@ -279,7 +279,7 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase ->with('myMessage', array( '{{ type }}' => '"application/pdf"', '{{ types }}' => '"image/*", "image/jpg"', - '{{ file }}' => $this->path, + '{{ file }}' => '"'.$this->path.'"', )); $this->validator->validate($file, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php index cf0802fed2..49b3943017 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php @@ -164,7 +164,7 @@ class IbanValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $iban, + '{{ value }}' => '"'.$iban.'"', )); $this->validator->validate($iban, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php index bdf6192880..25cd5d44f8 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php @@ -162,7 +162,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -196,7 +196,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -224,7 +224,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -252,7 +252,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -276,7 +276,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -314,7 +314,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -342,7 +342,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -369,7 +369,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -393,7 +393,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -417,7 +417,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -441,7 +441,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); @@ -465,7 +465,7 @@ class IpValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $ip, + '{{ value }}' => '"'.$ip.'"', )); $this->validator->validate($ip, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php index db6339449d..267f05b1b6 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -167,7 +167,7 @@ class LengthValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', $this->identicalTo(array( - '{{ value }}' => (string) $value, + '{{ value }}' => '"'.$value.'"', '{{ limit }}' => 4, )), $this->identicalTo($value), 4); @@ -191,7 +191,7 @@ class LengthValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', $this->identicalTo(array( - '{{ value }}' => (string) $value, + '{{ value }}' => '"'.$value.'"', '{{ limit }}' => 4, )), $this->identicalTo($value), 4); @@ -216,7 +216,7 @@ class LengthValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', $this->identicalTo(array( - '{{ value }}' => (string) $value, + '{{ value }}' => '"'.$value.'"', '{{ limit }}' => 4, )), $this->identicalTo($value), 4); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php index c44b0ea6ce..f7601de797 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -234,4 +234,19 @@ class RangeValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate(21, $constraint); } + + public function testNonNumeric() + { + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', array( + '{{ value }}' => '"abcd"', + )); + + $this->validator->validate('abcd', new Range(array( + 'min' => 10, + 'max' => 20, + 'invalidMessage' => 'myMessage', + ))); + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php index 1ea79fb8e2..25b5006c33 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php @@ -91,7 +91,7 @@ class RegexValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $value, + '{{ value }}' => '"'.$value.'"', )); $this->validator->validate($value, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php index ba398ab373..cfa24b80c7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php @@ -96,7 +96,7 @@ class TimeValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $time, + '{{ value }}' => '"'.$time.'"', )); $this->validator->validate($time, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php index 1cf4bb4d22..ac2cf9e360 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php @@ -131,7 +131,7 @@ class UrlValidatorTest extends \PHPUnit_Framework_TestCase $this->context->expects($this->once()) ->method('addViolation') ->with('myMessage', array( - '{{ value }}' => $url, + '{{ value }}' => '"'.$url.'"', )); $this->validator->validate($url, $constraint); From 123fc62652ec712fcf15216e5fca78fbf398d787 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 20 Jul 2014 16:43:00 +0200 Subject: [PATCH 11/24] properly handle null data when denormalizing If null is passed to denormalize(), no property values can be set on the denormalized object. Additionally, this fixes passing values to the denormalized object's constructor if the incoming data is an object. --- .../Normalizer/GetSetMethodNormalizer.php | 20 +++++++++--- .../Normalizer/GetSetMethodNormalizerTest.php | 32 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index e4e3d82291..122ab0e5dc 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -114,6 +114,18 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal */ public function denormalize($data, $class, $format = null, array $context = array()) { + if (is_array($data) || is_object($data) && $data instanceof \ArrayAccess) { + $normalizedData = $data; + } elseif (is_object($data)) { + $normalizedData = array(); + + foreach ($data as $attribute => $value) { + $normalizedData[$attribute] = $value; + } + } else { + $normalizedData = array(); + } + $reflectionClass = new \ReflectionClass($class); $constructor = $reflectionClass->getConstructor(); @@ -124,10 +136,10 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal foreach ($constructorParameters as $constructorParameter) { $paramName = lcfirst($this->formatAttribute($constructorParameter->name)); - if (isset($data[$paramName])) { - $params[] = $data[$paramName]; + if (isset($normalizedData[$paramName])) { + $params[] = $normalizedData[$paramName]; // don't run set for a parameter passed to the constructor - unset($data[$paramName]); + unset($normalizedData[$paramName]); } elseif ($constructorParameter->isOptional()) { $params[] = $constructorParameter->getDefaultValue(); } else { @@ -144,7 +156,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal $object = new $class; } - foreach ($data as $attribute => $value) { + foreach ($normalizedData as $attribute => $value) { $setter = 'set'.$this->formatAttribute($attribute); if (method_exists($object, $setter)) { diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index e4f1b5c656..1cbceece04 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -15,6 +15,11 @@ use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase { + /** + * @var GetSetMethodNormalizer + */ + private $normalizer; + protected function setUp() { $this->normalizer = new GetSetMethodNormalizer(); @@ -44,6 +49,17 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('bar', $obj->getBar()); } + public function testDenormalizeWithObject() + { + $data = new \stdClass(); + $data->foo = 'foo'; + $data->bar = 'bar'; + $data->fooBar = 'foobar'; + $obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetSetDummy', 'any'); + $this->assertEquals('foo', $obj->getFoo()); + $this->assertEquals('bar', $obj->getBar()); + } + public function testDenormalizeOnCamelCaseFormat() { $this->normalizer->setCamelizedAttributes(array('camel_case')); @@ -54,6 +70,11 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('camelCase', $obj->getCamelCase()); } + public function testDenormalizeNull() + { + $this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy')); + } + /** * @dataProvider attributeProvider */ @@ -96,6 +117,17 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(1, 2, 3), $obj->getBaz()); } + public function testConstructorWithObjectDenormalize() + { + $data = new \stdClass(); + $data->foo = 'foo'; + $data->bar = 'bar'; + $data->fooBar = 'foobar'; + $obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetConstructorDummy', 'any'); + $this->assertEquals('foo', $obj->getFoo()); + $this->assertEquals('bar', $obj->getBar()); + } + /** * @dataProvider provideCallbacks */ From e9022adaef40330a92bdf0916bf0b995f942c7fa Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 29 Jul 2014 20:09:11 +0200 Subject: [PATCH 12/24] fixed CS --- .../Console/Tests/Command/ListCommandTest.php | 2 +- .../Console/Tests/Fixtures/application_1.xml | 22 +++++++++---------- .../Console/Tests/Fixtures/application_2.xml | 22 +++++++++---------- .../Tests/Dumper/PhpDumperTest.php | 2 +- .../Tests/Fixtures/php/services9_compiled.php | 2 +- .../Tests/Fixtures/yaml/services1.yml | 1 - .../Tests/Fixtures/yaml/services8.yml | 1 - .../HttpFoundation/Tests/ResponseTest.php | 4 ++-- .../Tests/HttpCache/HttpCacheTest.php | 2 +- .../Process/Tests/ExecutableFinderTest.php | 8 +++---- .../Normalizer/GetSetMethodNormalizer.php | 2 +- .../Tests/Encoder/XmlEncoderTest.php | 4 ++-- 12 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php b/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php index 1df06f5d94..ca50874912 100644 --- a/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php @@ -50,7 +50,7 @@ EOF; public function testExecuteListsCommandsWithNamespaceArgument() { - require_once(realpath(__DIR__.'/../Fixtures/FooCommand.php')); + require_once realpath(__DIR__.'/../Fixtures/FooCommand.php'); $application = new Application(); $application->add(new \FooCommand()); $commandTester = new CommandTester($command = $application->get('list')); diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml index d5c57be401..1989971cc1 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml @@ -5,13 +5,13 @@ help [--xml] [--format="..."] [--raw] [command_name] Displays help for a command The <info>help</info> command displays help for a given command: - + <info>php app/console help list</info> - + You can also output the help in other formats by using the <comment>--format</comment> option: - + <info>php app/console help --format=xml list</info> - + To display the list of available commands, please use the <info>list</info> command. @@ -60,19 +60,19 @@ list [--xml] [--raw] [--format="..."] [namespace] Lists commands The <info>list</info> command lists all commands: - + <info>php app/console list</info> - + You can also display the commands for a specific namespace: - + <info>php app/console list test</info> - + You can also output the information in other formats by using the <comment>--format</comment> option: - + <info>php app/console list --format=xml</info> - + It's also possible to get raw list of commands (useful for embedding command runner): - + <info>php app/console list --raw</info> diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml b/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml index 9aa77bbeec..c4affaf1a0 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml @@ -5,13 +5,13 @@ help [--xml] [--format="..."] [--raw] [command_name] Displays help for a command The <info>help</info> command displays help for a given command: - + <info>php app/console help list</info> - + You can also output the help in other formats by using the <comment>--format</comment> option: - + <info>php app/console help --format=xml list</info> - + To display the list of available commands, please use the <info>list</info> command. @@ -60,19 +60,19 @@ list [--xml] [--raw] [--format="..."] [namespace] Lists commands The <info>list</info> command lists all commands: - + <info>php app/console list</info> - + You can also display the commands for a specific namespace: - + <info>php app/console list test</info> - + You can also output the information in other formats by using the <comment>--format</comment> option: - + <info>php app/console list --format=xml</info> - + It's also possible to get raw list of commands (useful for embedding command runner): - + <info>php app/console list --raw</info> diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 420671145f..1a64508413 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -140,7 +140,7 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases'))); $container = new \Symfony_DI_PhpDumper_Test_Aliases(); - $container->set('foo', $foo = new \stdClass); + $container->set('foo', $foo = new \stdClass()); $this->assertSame($foo, $container->get('foo')); $this->assertSame($foo, $container->get('alias_for_foo')); $this->assertSame($foo, $container->get('alias_for_alias')); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 629499a608..b53bb55006 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -201,7 +201,7 @@ class ProjectServiceContainer extends Container $this->services['method_call1'] = $instance = new \FooClass(); $instance->setBar($this->get('foo')); - $instance->setBar(NULL); + $instance->setBar(null); return $instance; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml index 8b13789179..e69de29bb2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml @@ -1 +0,0 @@ - diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml index a1fb590358..8850818f86 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml @@ -4,4 +4,3 @@ parameters: bar: 'foo is %%foo bar' escape: '@@escapeme' values: [true, false, null, 0, 1000.3, 'true', 'false', 'null'] - diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index f1e37fff7b..c94ace9af9 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -680,7 +680,7 @@ class ResponseTest extends ResponseTestCase public function validContentProvider() { return array( - 'obj' => array(new StringableObject), + 'obj' => array(new StringableObject()), 'string' => array('Foo'), 'int' => array(2), ); @@ -689,7 +689,7 @@ class ResponseTest extends ResponseTestCase public function invalidContentProvider() { return array( - 'obj' => array(new \stdClass), + 'obj' => array(new \stdClass()), 'array' => array(array()), 'bool' => array(true, '1'), ); diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index e08f198281..71cdd37aa7 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -1190,7 +1190,7 @@ class HttpCacheTest extends HttpCacheTestCase public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses() { - $time = new \DateTime; + $time = new \DateTime(); $responses = array( array( diff --git a/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php b/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php index 3b5bff26e9..5fbe1e0aa7 100644 --- a/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php +++ b/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php @@ -46,7 +46,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase $this->setPath(dirname(PHP_BINARY)); - $finder = new ExecutableFinder; + $finder = new ExecutableFinder(); $result = $finder->find($this->getPhpBinaryName()); $this->assertSamePath(PHP_BINARY, $result); @@ -62,7 +62,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase $this->setPath(''); - $finder = new ExecutableFinder; + $finder = new ExecutableFinder(); $result = $finder->find('foo', $expected); $this->assertEquals($expected, $result); @@ -82,7 +82,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase $extraDirs = array(dirname(PHP_BINARY)); - $finder = new ExecutableFinder; + $finder = new ExecutableFinder(); $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs); $this->assertSamePath(PHP_BINARY, $result); @@ -104,7 +104,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase ini_set('open_basedir', dirname(PHP_BINARY).PATH_SEPARATOR.'/'); - $finder = new ExecutableFinder; + $finder = new ExecutableFinder(); $result = $finder->find($this->getPhpBinaryName()); $this->assertSamePath(PHP_BINARY, $result); diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index e4e3d82291..24b5732f38 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -141,7 +141,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal $object = $reflectionClass->newInstanceArgs($params); } else { - $object = new $class; + $object = new $class(); } foreach ($data as $attribute => $value) { diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index 73dee7d021..b07b18e24d 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -189,7 +189,7 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase public function testEncodeSerializerXmlRootNodeNameOption() { $options = array('xml_root_node_name' => 'test'); - $this->encoder = new XmlEncoder; + $this->encoder = new XmlEncoder(); $serializer = new Serializer(array(), array('xml' => new XmlEncoder())); $this->encoder->setSerializer($serializer); @@ -272,7 +272,7 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase public function testDecodeWithoutItemHash() { - $obj = new ScalarDummy; + $obj = new ScalarDummy(); $obj->xmlFoo = array( 'foo-bar' => array( '@key' => "value", From 71edf38d599f80ccfc462cdb1970cdddfd8efbba Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 29 Jul 2014 20:13:16 +0200 Subject: [PATCH 13/24] partially reverted previous commit --- .../Tests/Fixtures/php/services9_compiled.php | 2 +- .../DependencyInjection/Tests/Fixtures/yaml/services1.yml | 1 + .../DependencyInjection/Tests/Fixtures/yaml/services8.yml | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index b53bb55006..629499a608 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -201,7 +201,7 @@ class ProjectServiceContainer extends Container $this->services['method_call1'] = $instance = new \FooClass(); $instance->setBar($this->get('foo')); - $instance->setBar(null); + $instance->setBar(NULL); return $instance; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml index e69de29bb2..8b13789179 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml @@ -0,0 +1 @@ + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml index 8850818f86..a1fb590358 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml @@ -4,3 +4,4 @@ parameters: bar: 'foo is %%foo bar' escape: '@@escapeme' values: [true, false, null, 0, 1000.3, 'true', 'false', 'null'] + From 08ea6d362138ffd593cc9b6951c055b93464ce0e Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 28 Jul 2014 15:51:04 +0200 Subject: [PATCH 14/24] [Validator] Removed information from the violation output if the value is an array, object or resource This was decided in the discussion of #10687. --- .../Validator/ConstraintValidator.php | 6 ++--- .../Tests/Constraints/NullValidatorTest.php | 6 ++--- .../Tests/Constraints/TypeValidatorTest.php | 25 +++++++++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index bb3378cd93..28609a6936 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -67,11 +67,11 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface } if (is_object($value)) { - return 'Object('.get_class($value).')'; + return 'object'; } if (is_array($value)) { - return 'Array'; + return 'array'; } if (is_string($value)) { @@ -79,7 +79,7 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface } if (is_resource($value)) { - return sprintf('Resource(%s#%d)', get_resource_type($value), $value); + return 'resource'; } if (null === $value) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php index cbf8da1f38..4ca50c1087 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php @@ -66,9 +66,9 @@ class NullValidatorTest extends \PHPUnit_Framework_TestCase array(true, 'true'), array('', '""'), array('foo bar', '"foo bar"'), - array(new \DateTime(), 'Object(DateTime)'), - array(new \stdClass(), 'Object(stdClass)'), - array(array(), 'Array'), + array(new \DateTime(), 'object'), + array(new \stdClass(), 'object'), + array(array(), 'array'), ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php index 57d7f5c4c4..db008ce3ce 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php @@ -132,7 +132,6 @@ class TypeValidatorTest extends \PHPUnit_Framework_TestCase { $object = new \stdClass(); $file = $this->createFile(); - $fileAsString = 'Resource(stream#'.intval($file).')'; return array( array('foobar', 'numeric', '"foobar"'), @@ -140,18 +139,18 @@ class TypeValidatorTest extends \PHPUnit_Framework_TestCase array('0', 'integer', '"0"'), array('1.5', 'float', '"1.5"'), array(12345, 'string', '12345'), - array($object, 'boolean', 'Object(stdClass)'), - array($object, 'numeric', 'Object(stdClass)'), - array($object, 'integer', 'Object(stdClass)'), - array($object, 'float', 'Object(stdClass)'), - array($object, 'string', 'Object(stdClass)'), - array($object, 'resource', 'Object(stdClass)'), - array($file, 'boolean', $fileAsString), - array($file, 'numeric', $fileAsString), - array($file, 'integer', $fileAsString), - array($file, 'float', $fileAsString), - array($file, 'string', $fileAsString), - array($file, 'object', $fileAsString), + array($object, 'boolean', 'object'), + array($object, 'numeric', 'object'), + array($object, 'integer', 'object'), + array($object, 'float', 'object'), + array($object, 'string', 'object'), + array($object, 'resource', 'object'), + array($file, 'boolean', 'resource'), + array($file, 'numeric', 'resource'), + array($file, 'integer', 'resource'), + array($file, 'float', 'resource'), + array($file, 'string', 'resource'), + array($file, 'object', 'resource'), array('12a34', 'digit', '"12a34"'), array('1a#23', 'alnum', '"1a#23"'), array('abcd1', 'alpha', '"abcd1"'), From 32ae95bdda30df61f64db2519fa209efa387570d Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 30 Jul 2014 10:05:53 +0200 Subject: [PATCH 15/24] [Validator] Added more detailed inline documentation --- .../Validator/ConstraintValidator.php | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index 28609a6936..ac50f7e0e2 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -36,9 +36,14 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface /** * Returns a string representation of the type of the value. * - * @param mixed $value + * This method should be used if you pass the type of a value as + * message parameter to a constraint violation. Note that such + * parameters should usually not be included in messages aimed at + * non-technical people. * - * @return string + * @param mixed $value The value to return the type of + * + * @return string The type of the value */ protected function formatTypeOf($value) { @@ -48,10 +53,24 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface /** * Returns a string representation of the value. * - * @param mixed $value - * @param bool $prettyDateTime + * This method returns the equivalent PHP tokens for most scalar types + * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped + * in double quotes ("). Objects, arrays and resources are formatted as + * "object", "array" and "resource". If the parameter $prettyDateTime + * is set to true, {@link \DateTime} objects will be formatted as + * RFC-3339 dates ("Y-m-d H:i:s"). * - * @return string + * Be careful when passing message parameters to a constraint violation + * that (may) contain objects, arrays or resources. These parameters + * should only be displayed for technical users. Non-technical users + * won't know what an "object", "array" or "resource" is and will be + * confused by the violation message. + * + * @param mixed $value The value to format as string + * @param bool $prettyDateTime Whether to format {@link \DateTime} + * objects as RFC-3339 dates ("Y-m-d H:i:s") + * + * @return string The string representation of the passed value */ protected function formatValue($value, $prettyDateTime = false) { @@ -100,10 +119,16 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface /** * Returns a string representation of a list of values. * - * @param array $values - * @param bool $prettyDateTime + * Each of the values is converted to a string using + * {@link formatValue()}. The values are then concatenated with commas. * - * @return string + * @param array $values A list of values + * @param bool $prettyDateTime Whether to format {@link \DateTime} + * objects as RFC-3339 dates ("Y-m-d H:i:s") + * + * @return string The string representation of the value list + * + * @see formatValue() */ protected function formatValues(array $values, $prettyDateTime = false) { From 67a6b75a470282afd5fa32241ff8f65cea7cdd26 Mon Sep 17 00:00:00 2001 From: mmoreram Date: Thu, 31 Jul 2014 18:46:41 +0200 Subject: [PATCH 16/24] Issue #11489 Added some CA and ES translations * Added some translations in CA * Fixed some translations in ES --- .../Resources/translations/validators.ca.xlf | 20 +++++++++++++++++++ .../Resources/translations/validators.es.xlf | 18 ++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.ca.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.ca.xlf index 4b42b819b9..08df82c64f 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.ca.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.ca.xlf @@ -278,6 +278,26 @@ This value should not be identical to {{ compared_value_type }} {{ compared_value }}. Aquest valor no hauria de idèntic a {{ compared_value_type }} {{ compared_value }}. + + The image ratio is too big ({{ ratio }}). Allowed maximum ratio is {{ max_ratio }}. + La proporció de l'imatge és massa gran ({{ ratio }}). La màxima proporció permesa és {{ max_ratio }}. + + + The image ratio is too small ({{ ratio }}). Minimum ratio expected is {{ min_ratio }}. + La proporció de l'imatge és massa petita ({{ ratio }}). La mínima proporció permesa és {{ max_ratio }}. + + + The image is square ({{ width }}x{{ height }}px). Square images are not allowed. + L'imatge és quadrada({{ width }}x{{ height }}px). Les imatges quadrades no estan permeses. + + + The image is landscape oriented ({{ width }}x{{ height }}px). Landscape oriented images are not allowed. + L'imatge està orientada horitzontalment ({{ width }}x{{ height }}px). Les imatges orientades horitzontalment no estan permeses. + + + The image is portrait oriented ({{ width }}x{{ height }}px). Portrait oriented images are not allowed. + L'imatge està orientada verticalment ({{ width }}x{{ height }}px). Les imatges orientades verticalment no estan permeses. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf index 2765fa2ae4..2f8c99322a 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf @@ -160,19 +160,19 @@ The image width is too big ({{ width }}px). Allowed maximum width is {{ max_width }}px. - La anchura de la imagen es demasiado grande ({{ width }}px). La anchura máxima permitida son {{ max_width }}px. + El ancho de la imagen es demasiado grande ({{ width }}px). El ancho máximo permitido es de {{ max_width }}px. The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px. - La anchura de la imagen es demasiado pequeña ({{ width }}px). La anchura mínima requerida son {{ min_width }}px. + El ancho de la imagen es demasiado pequeño ({{ width }}px). El ancho mínimo requerido es {{ min_width }}px. The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px. - La altura de la imagen es demasiado grande ({{ height }}px). La altura máxima permitida son {{ max_height }}px. + La altura de la imagen es demasiado grande ({{ height }}px). La altura máxima permitida es de {{ max_height }}px. The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px. - La altura de la imagen es demasiado pequeña ({{ height }}px). La altura mínima requerida son {{ min_height }}px. + La altura de la imagen es demasiado pequeña ({{ height }}px). La altura mínima requerida es de {{ min_height }}px. This value should be the user current password. @@ -280,23 +280,23 @@ The image ratio is too big ({{ ratio }}). Allowed maximum ratio is {{ max_ratio }}. - La proporción de la imagen es demasiado grande ({{ ratio }}). Maxima proporción permitida es {{ max_ratio }}. + La proporción de la imagen es demasiado grande ({{ ratio }}). La máxima proporción permitida es {{ max_ratio }}. The image ratio is too small ({{ ratio }}). Minimum ratio expected is {{ min_ratio }}. - La proporción de la imagen es demasiado pequeña ({{ ratio }}). Mínima proporción permitida es {{ min_ratio }}. + La proporción de la imagen es demasiado pequeña ({{ ratio }}). La mínima proporción permitida es {{ min_ratio }}. The image is square ({{ width }}x{{ height }}px). Square images are not allowed. - La imagen es cuadrada ({{ width }}x{{ height }}px). Imágenes cuadradas no son permitidas. + La imagen es cuadrada ({{ width }}x{{ height }}px). Las imágenes cuadradas no están permitidas. The image is landscape oriented ({{ width }}x{{ height }}px). Landscape oriented images are not allowed. - La imagen está orientada horizontal ({{ width }}x{{ height }}px). Imágenes orientada horizontal no está permitido. + La imagen está orientada horizontalmente ({{ width }}x{{ height }}px). Las imágenes orientadas horizontalmente no están permitidas. The image is portrait oriented ({{ width }}x{{ height }}px). Portrait oriented images are not allowed. - La imagen está orientada vertical ({{ width }}x{{ height }}px). Imágenes orientada vertical no está permitido. + La imagen está orientada verticalmente ({{ width }}x{{ height }}px). Las imágenes orientadas verticalmente no están permitidas. From f89811d8d1fb3f1a44dae551a07fbbe9d30ab813 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Thu, 31 Jul 2014 21:49:10 +0100 Subject: [PATCH 17/24] [Console] Roll back changes made to fixture files. Original change was made in e9022adaef40330a92bdf0916bf0b995f942c7fa. --- .../Console/Tests/Fixtures/application_1.xml | 22 +++++++++---------- .../Console/Tests/Fixtures/application_2.xml | 22 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml index 1989971cc1..d5c57be401 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml @@ -5,13 +5,13 @@ help [--xml] [--format="..."] [--raw] [command_name] Displays help for a command The <info>help</info> command displays help for a given command: - + <info>php app/console help list</info> - + You can also output the help in other formats by using the <comment>--format</comment> option: - + <info>php app/console help --format=xml list</info> - + To display the list of available commands, please use the <info>list</info> command. @@ -60,19 +60,19 @@ list [--xml] [--raw] [--format="..."] [namespace] Lists commands The <info>list</info> command lists all commands: - + <info>php app/console list</info> - + You can also display the commands for a specific namespace: - + <info>php app/console list test</info> - + You can also output the information in other formats by using the <comment>--format</comment> option: - + <info>php app/console list --format=xml</info> - + It's also possible to get raw list of commands (useful for embedding command runner): - + <info>php app/console list --raw</info> diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml b/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml index c4affaf1a0..9aa77bbeec 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml @@ -5,13 +5,13 @@ help [--xml] [--format="..."] [--raw] [command_name] Displays help for a command The <info>help</info> command displays help for a given command: - + <info>php app/console help list</info> - + You can also output the help in other formats by using the <comment>--format</comment> option: - + <info>php app/console help --format=xml list</info> - + To display the list of available commands, please use the <info>list</info> command. @@ -60,19 +60,19 @@ list [--xml] [--raw] [--format="..."] [namespace] Lists commands The <info>list</info> command lists all commands: - + <info>php app/console list</info> - + You can also display the commands for a specific namespace: - + <info>php app/console list test</info> - + You can also output the information in other formats by using the <comment>--format</comment> option: - + <info>php app/console list --format=xml</info> - + It's also possible to get raw list of commands (useful for embedding command runner): - + <info>php app/console list --raw</info> From 88b4e7008d71da9b7d1d87020c491bd674c55351 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Thu, 31 Jul 2014 22:03:37 +0100 Subject: [PATCH 18/24] [DependencyInjection] Roll back changes made to generated files. Original change was made in e9022adaef40330a92bdf0916bf0b995f942c7fa. --- .../Tests/Fixtures/php/services9_compiled.php | 2 +- .../DependencyInjection/Tests/Fixtures/yaml/services1.yml | 1 + .../DependencyInjection/Tests/Fixtures/yaml/services8.yml | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index b53bb55006..629499a608 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -201,7 +201,7 @@ class ProjectServiceContainer extends Container $this->services['method_call1'] = $instance = new \FooClass(); $instance->setBar($this->get('foo')); - $instance->setBar(null); + $instance->setBar(NULL); return $instance; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml index e69de29bb2..8b13789179 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services1.yml @@ -0,0 +1 @@ + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml index 8850818f86..a1fb590358 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services8.yml @@ -4,3 +4,4 @@ parameters: bar: 'foo is %%foo bar' escape: '@@escapeme' values: [true, false, null, 0, 1000.3, 'true', 'false', 'null'] + From e40f24f0a9c6f73f89d318c2d5b4fd8d854d0972 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Thu, 31 Jul 2014 22:38:34 +0100 Subject: [PATCH 19/24] [Process] Fix tests when pcntl is not available. --- src/Symfony/Component/Process/Tests/SimpleProcessTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Process/Tests/SimpleProcessTest.php b/src/Symfony/Component/Process/Tests/SimpleProcessTest.php index ccaa78df24..ca4522acf1 100644 --- a/src/Symfony/Component/Process/Tests/SimpleProcessTest.php +++ b/src/Symfony/Component/Process/Tests/SimpleProcessTest.php @@ -167,7 +167,7 @@ class SimpleProcessTest extends AbstractProcessTest $process = $this->getProcess('php -r "echo \'foo\'; sleep(1); echo \'bar\';"'); $process->run(function () use ($process) { if ($process->isRunning()) { - $process->signal(SIGKILL); + $process->signal(defined('SIGKILL') ? SIGKILL : 9); } }); } catch (RuntimeException $e) { @@ -183,7 +183,7 @@ class SimpleProcessTest extends AbstractProcessTest $process = $this->getProcess('php -r "echo \'foo\'; sleep(1); echo \'bar\';"'); $process->run(function () use ($process) { if ($process->isRunning()) { - $process->signal(SIGTERM); + $process->signal(defined('SIGTERM') ? SIGTERM : 15); } }); } catch (RuntimeException $e) { From 440322effc9e482241bc552b2cc6a682c6063d27 Mon Sep 17 00:00:00 2001 From: sun Date: Sat, 19 Jul 2014 20:07:52 +0200 Subject: [PATCH 20/24] Fixed self-reference in 'service_container' service breaks garbage collection (and clone). --- .../DependencyInjection/Container.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 63b8d79a85..d8d08cd52e 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -93,8 +93,6 @@ class Container implements IntrospectableContainerInterface $this->scopeChildren = array(); $this->scopedServices = array(); $this->scopeStacks = array(); - - $this->set('service_container', $this); } /** @@ -204,6 +202,12 @@ class Container implements IntrospectableContainerInterface $id = strtolower($id); + if ('service_container' === $id) { + // BC: 'service_container' is no longer a self-reference but always + // $this, so ignore this call. + // @todo Throw InvalidArgumentException in next major release. + return; + } if (self::SCOPE_CONTAINER !== $scope) { if (!isset($this->scopedServices[$scope])) { throw new RuntimeException(sprintf('You cannot set service "%s" of inactive scope.', $id)); @@ -240,6 +244,10 @@ class Container implements IntrospectableContainerInterface { $id = strtolower($id); + if ('service_container' === $id) { + return true; + } + return isset($this->services[$id]) || array_key_exists($id, $this->services) || isset($this->aliases[$id]) @@ -276,6 +284,9 @@ class Container implements IntrospectableContainerInterface if ($strtolower) { $id = strtolower($id); } + if ('service_container' === $id) { + return $this; + } if (isset($this->aliases[$id])) { $id = $this->aliases[$id]; } @@ -347,6 +358,12 @@ class Container implements IntrospectableContainerInterface { $id = strtolower($id); + if ('service_container' === $id) { + // BC: 'service_container' was a synthetic service previously. + // @todo Change to false in next major release. + return true; + } + return isset($this->services[$id]) || array_key_exists($id, $this->services); } @@ -364,6 +381,7 @@ class Container implements IntrospectableContainerInterface $ids[] = self::underscore($match[1]); } } + $ids[] = 'service_container'; return array_unique(array_merge($ids, array_keys($this->services))); } From 1775da59255010724f2864751c8d1256729727e7 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 26 Jul 2014 14:09:47 +0200 Subject: [PATCH 21/24] fix some docblocks --- .../CompilerPass/RegisterMappingsPass.php | 2 +- .../Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php | 3 +++ src/Symfony/Component/Config/Definition/BaseNode.php | 1 + src/Symfony/Component/Console/Application.php | 2 ++ src/Symfony/Component/Console/Helper/DescriptorHelper.php | 6 ++++-- src/Symfony/Component/Console/Helper/DialogHelper.php | 6 +++--- src/Symfony/Component/Console/Helper/TableHelper.php | 2 ++ src/Symfony/Component/DependencyInjection/Container.php | 5 +++-- .../Component/DependencyInjection/Dumper/PhpDumper.php | 5 +++++ .../Component/DependencyInjection/Loader/YamlFileLoader.php | 2 ++ src/Symfony/Component/Form/ButtonBuilder.php | 2 ++ src/Symfony/Component/HttpFoundation/BinaryFileResponse.php | 2 ++ src/Symfony/Component/HttpFoundation/File/UploadedFile.php | 2 +- .../Component/HttpKernel/Debug/TraceableEventDispatcher.php | 5 +++-- .../Component/HttpKernel/Fragment/FragmentHandler.php | 1 + .../Component/Intl/NumberFormatter/NumberFormatter.php | 2 +- .../Component/Security/Acl/Dbal/MutableAclProvider.php | 6 +++--- .../Security/Core/Encoder/BCryptPasswordEncoder.php | 2 ++ .../Component/Security/Core/Encoder/BasePasswordEncoder.php | 2 ++ .../Component/Validator/Constraints/LuhnValidator.php | 2 ++ src/Symfony/Component/Yaml/Inline.php | 2 ++ 21 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterMappingsPass.php b/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterMappingsPass.php index ac3ce490aa..e56baa1e48 100644 --- a/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterMappingsPass.php +++ b/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterMappingsPass.php @@ -74,7 +74,7 @@ abstract class RegisterMappingsPass implements CompilerPassInterface * @param string[] $managerParameters list of container parameters * that could hold the manager name * @param string $driverPattern pattern to get the metadata driver service names - * @param string $enabledParameter service container parameter that must be + * @param string|false $enabledParameter service container parameter that must be * present to enable the mapping. Set to false * to not do any check, optional. */ diff --git a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php index 2b04a69623..e39cc71d9e 100644 --- a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php +++ b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php @@ -80,6 +80,9 @@ class ModelChoiceList extends ObjectChoiceList * Either an array if $choices is given, * or a ModelCriteria to be merged with the $queryObject. * @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths. + * + * @throws MissingOptionsException when no model class is given + * @throws InvalidOptionsException when the model class cannot be found */ public function __construct($class, $labelPath = null, $choices = null, $queryObject = null, $groupPath = null, $preferred = array(), PropertyAccessorInterface $propertyAccessor = null) { diff --git a/src/Symfony/Component/Config/Definition/BaseNode.php b/src/Symfony/Component/Config/Definition/BaseNode.php index 39e6d16aff..9e250c1347 100644 --- a/src/Symfony/Component/Config/Definition/BaseNode.php +++ b/src/Symfony/Component/Config/Definition/BaseNode.php @@ -287,6 +287,7 @@ abstract class BaseNode implements NodeInterface * * @return mixed The finalized value * + * @throws Exception * @throws InvalidConfigurationException */ final public function finalize($value) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 88162a210e..029dc29e00 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -884,6 +884,8 @@ class Application * @param OutputInterface $output An Output instance * * @return int 0 if everything went fine, or an error code + * + * @throws \Exception when the command being run threw an exception */ protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { diff --git a/src/Symfony/Component/Console/Helper/DescriptorHelper.php b/src/Symfony/Component/Console/Helper/DescriptorHelper.php index 9d1a7d6144..9072efe987 100644 --- a/src/Symfony/Component/Console/Helper/DescriptorHelper.php +++ b/src/Symfony/Component/Console/Helper/DescriptorHelper.php @@ -48,9 +48,11 @@ class DescriptorHelper extends Helper * * @param OutputInterface $output * @param object $object - * @param string $format + * @param string|null $format * @param bool $raw - * @param string $namespace + * @param string|null $namespace + * + * @throws \InvalidArgumentException when the given format is not supported */ public function describe(OutputInterface $output, $object, $format = null, $raw = false, $namespace = null) { diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 57f6a6f6b4..6605e8939d 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -321,7 +321,7 @@ class DialogHelper extends Helper * @param OutputInterface $output An Output instance * @param string|array $question The question to ask * @param callable $validator A PHP callback - * @param int $attempts Max number of times to ask before giving up (false by default, which means infinite) + * @param int|false $attempts Max number of times to ask before giving up (false by default, which means infinite) * @param string $default The default answer if none is given by the user * @param array $autocomplete List of values to autocomplete * @@ -350,7 +350,7 @@ class DialogHelper extends Helper * @param OutputInterface $output An Output instance * @param string|array $question The question to ask * @param callable $validator A PHP callback - * @param int $attempts Max number of times to ask before giving up (false by default, which means infinite) + * @param int|false $attempts Max number of times to ask before giving up (false by default, which means infinite) * @param bool $fallback In case the response can not be hidden, whether to fallback on non-hidden question or not * * @return string The response @@ -444,7 +444,7 @@ class DialogHelper extends Helper * @param callable $interviewer A callable that will ask for a question and return the result * @param OutputInterface $output An Output instance * @param callable $validator A PHP callback - * @param int $attempts Max number of times to ask before giving up ; false will ask infinitely + * @param int|false $attempts Max number of times to ask before giving up ; false will ask infinitely * * @return string The validated response * diff --git a/src/Symfony/Component/Console/Helper/TableHelper.php b/src/Symfony/Component/Console/Helper/TableHelper.php index 8e4012b769..285162d4e2 100644 --- a/src/Symfony/Component/Console/Helper/TableHelper.php +++ b/src/Symfony/Component/Console/Helper/TableHelper.php @@ -78,6 +78,8 @@ class TableHelper extends Helper * @param int $layout self::LAYOUT_* * * @return TableHelper + * + * @throws InvalidArgumentException when the table layout is not known */ public function setLayout($layout) { diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 63b8d79a85..266aeee131 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -258,9 +258,10 @@ class Container implements IntrospectableContainerInterface * * @return object The associated service * - * @throws InvalidArgumentException if the service is not defined + * @throws InvalidArgumentException if the service is not defined * @throws ServiceCircularReferenceException When a circular reference is detected - * @throws ServiceNotFoundException When the service is not defined + * @throws ServiceNotFoundException When the service is not defined + * @throws \Exception if an exception has been thrown when the service has been resolved * * @see Reference * diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 03f5f79cc1..ddcfe2a8f8 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -423,7 +423,10 @@ class PhpDumper extends Dumper * * @param string $id * @param Definition $definition + * * @return string + * + * @throws ServiceCircularReferenceException when the container contains a circular reference */ private function addServiceInlinedDefinitionsSetup($id, $definition) { @@ -627,6 +630,8 @@ EOF; * * @param string $id A service identifier * @param Definition $definition A Definition instance + * + * @return string|null */ private function addServiceSynchronizer($id, Definition $definition) { diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index 6f2d4c19a1..cf4cfc75bd 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -242,6 +242,8 @@ class YamlFileLoader extends FileLoader * @param string $file * * @return array The file content + * + * @throws InvalidArgumentException when the given file is not a local file or when it does not exist */ protected function loadFile($file) { diff --git a/src/Symfony/Component/Form/ButtonBuilder.php b/src/Symfony/Component/Form/ButtonBuilder.php index 9302af4190..b02f8f5fe1 100644 --- a/src/Symfony/Component/Form/ButtonBuilder.php +++ b/src/Symfony/Component/Form/ButtonBuilder.php @@ -503,6 +503,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface * * @param bool $initialize * + * @return ButtonBuilder + * * @throws BadMethodCallException */ public function setAutoInitialize($initialize) diff --git a/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php b/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php index 55c2ac5dd6..88e2abefad 100644 --- a/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php +++ b/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php @@ -61,6 +61,8 @@ class BinaryFileResponse extends Response * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename * @param bool $autoEtag Whether the ETag header should be automatically set * @param bool $autoLastModified Whether the Last-Modified header should be automatically set + * + * @return BinaryResponse The created response */ public static function create($file = null, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true) { diff --git a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php index e9f6395201..4133d2eabc 100644 --- a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php +++ b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php @@ -286,7 +286,7 @@ class UploadedFile extends File /** * Returns an informative upload error message. * - * @param int $code The error code returned by an upload attempt + * @param int $errorCode The error code returned by an upload attempt * * @return string The error message regarding the specified error code */ diff --git a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php index c89978e9c4..8af3ad7c4d 100644 --- a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php @@ -261,8 +261,9 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve /** * Returns information about the listener * - * @param object $listener The listener - * @param string $eventName The event name + * @param object $listener The listener + * @param int|null $eventId The event id + * @param string $eventName The event name * * @return array Information about the listener */ diff --git a/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php b/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php index b61b0805d3..6a1654adf8 100644 --- a/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php +++ b/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php @@ -81,6 +81,7 @@ class FragmentHandler * @return string|null The Response content or null when the Response is streamed * * @throws \InvalidArgumentException when the renderer does not exist + * @throws \LogicException when no master request is being handled * @throws \RuntimeException when the Response is not successful */ public function render($uri, $renderer = 'inline', array $options = array()) diff --git a/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php b/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php index 563f086024..52868bb408 100644 --- a/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php +++ b/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php @@ -505,7 +505,7 @@ class NumberFormatter * Parse a number * * @param string $value The value to parse - * @param string $type Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default + * @param int $type Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default * @param int $position Offset to begin the parsing on return this value will hold the offset at which the parsing ended * * @return bool|string The parsed value of false on error diff --git a/src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php b/src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php index 42803acc07..b8f5fb8e20 100644 --- a/src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php +++ b/src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php @@ -801,12 +801,12 @@ QUERY; } /** - * This process old entries changes on an ACE related property (classFieldAces, or objectFieldAces). + * This processes old entries changes on an ACE related property (classFieldAces, or objectFieldAces). * * @param string $name - * @param array $changes + * @param array $changes */ - private function updateOldFieldAceProperty($ane, array $changes) + private function updateOldFieldAceProperty($name, array $changes) { $currentIds = array(); foreach ($changes[1] as $field => $new) { diff --git a/src/Symfony/Component/Security/Core/Encoder/BCryptPasswordEncoder.php b/src/Symfony/Component/Security/Core/Encoder/BCryptPasswordEncoder.php index 1dcf3a6969..27a7334569 100644 --- a/src/Symfony/Component/Security/Core/Encoder/BCryptPasswordEncoder.php +++ b/src/Symfony/Component/Security/Core/Encoder/BCryptPasswordEncoder.php @@ -61,6 +61,8 @@ class BCryptPasswordEncoder extends BasePasswordEncoder * * @return string The encoded password * + * @throws BadCredentialsException when the given password is too long + * * @link http://lxr.php.net/xref/PHP_5_5/ext/standard/password.c#111 */ public function encodePassword($raw, $salt) diff --git a/src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php b/src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php index b27c2b0b95..5bf223bdbd 100644 --- a/src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php +++ b/src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php @@ -89,6 +89,8 @@ abstract class BasePasswordEncoder implements PasswordEncoderInterface /** * Checks if the password is too long. * + * @param string $password The password to check + * * @return bool true if the password is too long, false otherwise */ protected function isPasswordTooLong($password) diff --git a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php index d3802fe99a..77e52f5040 100644 --- a/src/Symfony/Component/Validator/Constraints/LuhnValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LuhnValidator.php @@ -32,6 +32,8 @@ class LuhnValidator extends ConstraintValidator * * @param mixed $value * @param Constraint $constraint + * + * @throws UnexpectedTypeException when the given credit card number is no string */ public function validate($value, Constraint $constraint) { diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index b0d6a031f8..0db774fcd1 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -384,6 +384,8 @@ class Inline * @param string $scalar * * @return string A YAML string + * + * @throws ParseException when object parsing support was disabled and the parser detected a PHP object */ private static function evaluateScalar($scalar) { From 8dc322be34f89a325dda276cb698d9f5c48cdcae Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 2 Aug 2014 10:47:58 +0200 Subject: [PATCH 22/24] fix axes handling in Crawler::filterXPath() Due to some limitations in the relativize() method, it was not possible to use XPath axes other than descendant or descendant-or-self in the filterXPath() method of the Crawler class. This commit adds support for the ancestor, ancestor-or-self, attribute, child, following, following-sibling, parent, preceding, preceding-sibling and self axes. --- src/Symfony/Component/DomCrawler/Crawler.php | 2 +- .../DomCrawler/Tests/CrawlerTest.php | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 947345c2b9..5dd4dba3cd 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -854,7 +854,7 @@ class Crawler extends \SplObjectStorage $expression = $nonMatchingExpression; } elseif (0 === strpos($expression, 'descendant::')) { $expression = 'descendant-or-self::' . substr($expression, strlen('descendant::')); - } elseif (0 !== strpos($expression, 'descendant-or-self::')) { + } elseif (!preg_match('/^(ancestor|ancestor-or-self|attribute|child|descendant-or-self|following|following-sibling|parent|preceding|preceding-sibling|self)::/', $expression)) { $expression = 'self::' .$expression; } $expressions[] = $parenthesis.$expression; diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 2c52465670..4c9a4bcecf 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -411,6 +411,74 @@ EOF $this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained'); } + public function testFilterXPathWithAncestorAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(2, $crawler->filterXPath('ancestor::*')); + } + + public function testFilterXPathWithAncestorOrSelfAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(3, $crawler->filterXPath('ancestor-or-self::*')); + } + + public function testFilterXPathWithAttributeAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(2, $crawler->filterXPath('attribute::*')); + } + + public function testFilterXPathWithChildAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//body'); + + $this->assertCount(2, $crawler->filterXPath('child::input')); + } + + public function testFilterXPathWithFollowingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//a'); + + $this->assertCount(3, $crawler->filterXPath('following::div')); + } + + public function testFilterXPathWithFollowingSiblingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//a'); + + $this->assertCount(2, $crawler->filterXPath('following-sibling::div')); + } + + public function testFilterXPathWithParentAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//button'); + + $this->assertEquals('foo', $crawler->filterXPath('parent::*')->attr('action')); + } + + public function testFilterXPathWithPrecedingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(13, $crawler->filterXPath('preceding::*')); + } + + public function testFilterXPathWithPrecedingSiblingAxis() + { + $crawler = $this->createTestCrawler()->filterXPath('//form'); + + $this->assertCount(9, $crawler->filterXPath('preceding-sibling::*')); + } + + public function testFilterXPathWithSelfAxes() + { + $this->assertCount(1, $this->createTestCrawler()->filterXPath('self::*')); + } + /** * @covers Symfony\Component\DomCrawler\Crawler::filter */ From 0c6f750c1e2a55a474326fbca953d831040624a4 Mon Sep 17 00:00:00 2001 From: moldcraft Date: Mon, 4 Aug 2014 11:40:18 +0300 Subject: [PATCH 23/24] Fix incorrect romanian plural translations --- .../Resources/translations/validators.ro.xlf | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.ro.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.ro.xlf index e109eb3c4b..58f3d535d1 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.ro.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.ro.xlf @@ -24,11 +24,11 @@ You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices. - Trebuie să selectați cel puțin {{ limit }} opțiuni. + Trebuie să selectați cel puțin {{ limit }} opțiune.|Trebuie să selectați cel puțin {{ limit }} opțiuni.|Trebuie să selectați cel puțin {{ limit }} de opțiuni You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices. - Trebuie să selectați cel mult {{ limit }} opțiuni. + Trebuie să selectați cel mult {{ limit }} opțiune.|Trebuie să selectați cel mult {{ limit }} opțiuni.|Trebuie să selectați cel mult {{ limit }} de opțiuni. One or more of the given values is invalid. @@ -76,7 +76,7 @@ This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less. - Această valoare este prea lungă. Ar trebui să aibă maxim {{ limit }} caracter.|Această valoare este prea lungă. Ar trebui să aibă maxim {{ limit }} caractere. + Această valoare este prea lungă. Ar trebui să aibă maxim {{ limit }} caracter.|Această valoare este prea lungă. Ar trebui să aibă maxim {{ limit }} caractere.|Această valoare este prea lungă. Ar trebui să aibă maxim {{ limit }} de caractere. This value should be {{ limit }} or more. @@ -84,7 +84,7 @@ This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more. - Această valoare este prea scurtă. Ar trebui să aibă minim {{ limit }} caracter.|Această valoare este prea scurtă. Ar trebui să aibă minim {{ limit }} caractere. + Această valoare este prea scurtă. Ar trebui să aibă minim {{ limit }} caracter.|Această valoare este prea scurtă. Ar trebui să aibă minim {{ limit }} caractere.|Această valoare este prea scurtă. Ar trebui să aibă minim {{ limit }} de caractere. This value should not be blank. @@ -180,7 +180,7 @@ This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters. - Această valoare trebuie să conțină exact {{ limit }} caracter.|Această valoare trebuie să conțină exact {{ limit }} caractere. + Această valoare trebuie să conțină exact {{ limit }} caracter.|Această valoare trebuie să conțină exact {{ limit }} caractere.|Această valoare trebuie să conțină exact {{ limit }} de caractere. The file was only partially uploaded. @@ -204,15 +204,15 @@ This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more. - Această colecție trebuie să conțină cel puțin {{ limit }} elemente. + Această colecție trebuie să conțină cel puțin {{ limit }} element.|Această colecție trebuie să conțină cel puțin {{ limit }} elemente.|Această colecție trebuie să conțină cel puțin {{ limit }} de elemente. This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less. - Această colecție trebuie să conțină cel mult {{ limit }} elemente. + Această colecție trebuie să conțină cel mult {{ limit }} element.|Această colecție trebuie să conțină cel mult {{ limit }} elemente.|Această colecție trebuie să conțină cel mult {{ limit }} de elemente. This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements. - Această colecție trebuie să conțină {{ limit }} elemente. + Această colecție trebuie să conțină {{ limit }} element.|Această colecție trebuie să conțină {{ limit }} elemente.|Această colecție trebuie să conțină {{ limit }} de elemente. Invalid card number. From a361f10b1d49bf94625898247377bce2769c5f09 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 4 Aug 2014 15:49:20 +0200 Subject: [PATCH 24/24] [Validator] Added Swedish translations --- .../Resources/translations/validators.sv.xlf | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sv.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sv.xlf index c9d7b37b9a..b70848b8e7 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sv.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sv.xlf @@ -278,6 +278,26 @@ This value should not be identical to {{ compared_value_type }} {{ compared_value }}. Värdet ska inte vara identiskt med {{ compared_value_type }} {{ compared_value }}. + + The image ratio is too big ({{ ratio }}). Allowed maximum ratio is {{ max_ratio }}. + Förhållandet mellan bildens bredd och höjd är för stort ({{ ratio }}). Högsta tillåtna förhållande är {{ max_ratio }}. + + + The image ratio is too small ({{ ratio }}). Minimum ratio expected is {{ min_ratio }}. + Förhållandet mellan bildens bredd och höjd är för litet ({{ ratio }}). Minsta tillåtna förhållande är {{ min_ratio }}. + + + The image is square ({{ width }}x{{ height }}px). Square images are not allowed. + Bilden är kvadratisk ({{ width }}x{{ height }}px). Kvadratiska bilder tillåts inte. + + + The image is landscape oriented ({{ width }}x{{ height }}px). Landscape oriented images are not allowed. + Bilden är landskapsorienterad ({{ width }}x{{ height }}px). Landskapsorienterade bilder tillåts inte. + + + The image is portrait oriented ({{ width }}x{{ height }}px). Portrait oriented images are not allowed. + Bilden är porträttsorienterad ({{ width }}x{{ height }}px). Porträttsorienterade bilder tillåts inte. +