From 44edbdf9c0d7400d757ccbef2d76fbad1f688752 Mon Sep 17 00:00:00 2001 From: "stefan.r" Date: Mon, 6 Apr 2015 01:30:32 +0200 Subject: [PATCH] Fixed compatibility with PHP7 and up by introducing new constraints (IsNull, IsTrue, IsFalse) and related validators (IsNullValidator, IsTrueValidator, IsFalseValidator) --- .../Validator/ValidatorTypeGuesser.php | 3 ++ .../Validator/ValidatorTypeGuesserTest.php | 16 +++++++- src/Symfony/Component/Form/composer.json | 2 +- src/Symfony/Component/Validator/CHANGELOG.md | 5 +++ .../Component/Validator/Constraints/False.php | 7 +--- .../Validator/Constraints/FalseValidator.php | 20 +--------- .../Validator/Constraints/IsFalse.php | 27 +++++++++++++ .../Constraints/IsFalseValidator.php | 37 ++++++++++++++++++ .../Validator/Constraints/IsNull.php | 27 +++++++++++++ .../Validator/Constraints/IsNullValidator.php | 35 +++++++++++++++++ .../Validator/Constraints/IsTrue.php | 27 +++++++++++++ .../Validator/Constraints/IsTrueValidator.php | 39 +++++++++++++++++++ .../Component/Validator/Constraints/Null.php | 7 +--- .../Validator/Constraints/NullValidator.php | 18 +-------- .../Component/Validator/Constraints/True.php | 7 +--- .../Validator/Constraints/TrueValidator.php | 22 +---------- ...datorTest.php => IsFalseValidatorTest.php} | 14 +++---- ...idatorTest.php => IsNullValidatorTest.php} | 12 +++--- ...idatorTest.php => IsTrueValidatorTest.php} | 14 +++---- 19 files changed, 241 insertions(+), 98 deletions(-) create mode 100644 src/Symfony/Component/Validator/Constraints/IsFalse.php create mode 100644 src/Symfony/Component/Validator/Constraints/IsFalseValidator.php create mode 100644 src/Symfony/Component/Validator/Constraints/IsNull.php create mode 100644 src/Symfony/Component/Validator/Constraints/IsNullValidator.php create mode 100644 src/Symfony/Component/Validator/Constraints/IsTrue.php create mode 100644 src/Symfony/Component/Validator/Constraints/IsTrueValidator.php rename src/Symfony/Component/Validator/Tests/Constraints/{FalseValidatorTest.php => IsFalseValidatorTest.php} (68%) rename src/Symfony/Component/Validator/Tests/Constraints/{NullValidatorTest.php => IsNullValidatorTest.php} (79%) rename src/Symfony/Component/Validator/Tests/Constraints/{TrueValidatorTest.php => IsTrueValidatorTest.php} (68%) diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php index 56f763b7ce..63dacfaacc 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php @@ -157,6 +157,8 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface case 'Symfony\Component\Validator\Constraints\True': case 'Symfony\Component\Validator\Constraints\False': + case 'Symfony\Component\Validator\Constraints\IsTrue': + case 'Symfony\Component\Validator\Constraints\IsFalse': return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE); } } @@ -174,6 +176,7 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface case 'Symfony\Component\Validator\Constraints\NotNull': case 'Symfony\Component\Validator\Constraints\NotBlank': case 'Symfony\Component\Validator\Constraints\True': + case 'Symfony\Component\Validator\Constraints\IsTrue': return new ValueGuess(true, Guess::HIGH_CONFIDENCE); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php index 61b2a917ec..654f6d5d2f 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php @@ -19,7 +19,7 @@ use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Constraints\Range; -use Symfony\Component\Validator\Constraints\True; +use Symfony\Component\Validator\Constraints\IsTrue; use Symfony\Component\Validator\Constraints\Type; use Symfony\Component\Validator\Mapping\ClassMetadata; @@ -64,7 +64,7 @@ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase return array( array(new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)), array(new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)), - array(new True(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)), + array(new IsTrue(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)), array(new Length(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)), array(new Range(array('min' => 1, 'max' => 20)), new ValueGuess(false, Guess::LOW_CONFIDENCE)), ); @@ -84,6 +84,18 @@ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase $this->assertEquals($guess, $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY)); } + /** + * @group legacy + */ + public function testLegacyGuessRequired() + { + if (PHP_VERSION_ID >= 70000) { + $this->markTestSkipped('Cannot use a class called True on PHP 7 or higher.'); + } + $true = 'Symfony\Component\Validator\Constraints\True'; + $this->testGuessRequired(new $true(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)); + } + public function testGuessRequiredReturnsFalseForUnmappedProperties() { $this->assertEquals(new ValueGuess(false, Guess::LOW_CONFIDENCE), $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY)); diff --git a/src/Symfony/Component/Form/composer.json b/src/Symfony/Component/Form/composer.json index d9fcb2c0cf..48d217ed6e 100644 --- a/src/Symfony/Component/Form/composer.json +++ b/src/Symfony/Component/Form/composer.json @@ -25,7 +25,7 @@ "require-dev": { "symfony/phpunit-bridge": "~2.7", "doctrine/collections": "~1.0", - "symfony/validator": "~2.3.0,>=2.3.20", + "symfony/validator": "~2.3.29", "symfony/translation": "~2.0,>=2.0.5", "symfony/http-foundation": "~2.2" }, diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 7ab8051c1a..66df4a2d7c 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +2.3.29 +------ + + * fixed compatibility with PHP7 and up by introducing new constraints (IsNull, IsTrue, IsFalse) and related validators (IsNullValidator, IsTrueValidator, IsFalseValidator) + 2.3.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/False.php b/src/Symfony/Component/Validator/Constraints/False.php index fc2e3e49cb..6ec76ee3d1 100644 --- a/src/Symfony/Component/Validator/Constraints/False.php +++ b/src/Symfony/Component/Validator/Constraints/False.php @@ -11,8 +11,6 @@ namespace Symfony\Component\Validator\Constraints; -use Symfony\Component\Validator\Constraint; - /** * @Annotation * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) @@ -21,7 +19,4 @@ use Symfony\Component\Validator\Constraint; * * @api */ -class False extends Constraint -{ - public $message = 'This value should be false.'; -} +class False extends IsFalse {} diff --git a/src/Symfony/Component/Validator/Constraints/FalseValidator.php b/src/Symfony/Component/Validator/Constraints/FalseValidator.php index 280deba3c9..72fd1d44d1 100644 --- a/src/Symfony/Component/Validator/Constraints/FalseValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FalseValidator.php @@ -11,27 +11,9 @@ namespace Symfony\Component\Validator\Constraints; -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintValidator; - /** * @author Bernhard Schussek * * @api */ -class FalseValidator extends ConstraintValidator -{ - /** - * {@inheritdoc} - */ - public function validate($value, Constraint $constraint) - { - if (null === $value || false === $value || 0 === $value || '0' === $value) { - return; - } - - $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->formatValue($value), - )); - } -} +class FalseValidator extends IsFalseValidator {} diff --git a/src/Symfony/Component/Validator/Constraints/IsFalse.php b/src/Symfony/Component/Validator/Constraints/IsFalse.php new file mode 100644 index 0000000000..7b1b72bd51 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IsFalse.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + * + * @author Bernhard Schussek + * + * @api + */ +class IsFalse extends Constraint +{ + public $message = 'This value should be false.'; +} diff --git a/src/Symfony/Component/Validator/Constraints/IsFalseValidator.php b/src/Symfony/Component/Validator/Constraints/IsFalseValidator.php new file mode 100644 index 0000000000..47c200783a --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IsFalseValidator.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * @author Bernhard Schussek + * + * @api + */ +class IsFalseValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (null === $value || false === $value || 0 === $value || '0' === $value) { + return; + } + + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->formatValue($value), + )); + } +} diff --git a/src/Symfony/Component/Validator/Constraints/IsNull.php b/src/Symfony/Component/Validator/Constraints/IsNull.php new file mode 100644 index 0000000000..3e7fef112c --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IsNull.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + * + * @author Bernhard Schussek + * + * @api + */ +class IsNull extends Constraint +{ + public $message = 'This value should be null.'; +} diff --git a/src/Symfony/Component/Validator/Constraints/IsNullValidator.php b/src/Symfony/Component/Validator/Constraints/IsNullValidator.php new file mode 100644 index 0000000000..0072e8ff08 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IsNullValidator.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * @author Bernhard Schussek + * + * @api + */ +class IsNullValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (null !== $value) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->formatValue($value), + )); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/IsTrue.php b/src/Symfony/Component/Validator/Constraints/IsTrue.php new file mode 100644 index 0000000000..c0be6b8272 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IsTrue.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + * + * @author Bernhard Schussek + * + * @api + */ +class IsTrue extends Constraint +{ + public $message = 'This value should be true.'; +} diff --git a/src/Symfony/Component/Validator/Constraints/IsTrueValidator.php b/src/Symfony/Component/Validator/Constraints/IsTrueValidator.php new file mode 100644 index 0000000000..bef1736a1d --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IsTrueValidator.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * @author Bernhard Schussek + * + * @api + */ +class IsTrueValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (null === $value) { + return; + } + + if (true !== $value && 1 !== $value && '1' !== $value) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->formatValue($value), + )); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/Null.php b/src/Symfony/Component/Validator/Constraints/Null.php index 310455021b..5ad789cef8 100644 --- a/src/Symfony/Component/Validator/Constraints/Null.php +++ b/src/Symfony/Component/Validator/Constraints/Null.php @@ -11,8 +11,6 @@ namespace Symfony\Component\Validator\Constraints; -use Symfony\Component\Validator\Constraint; - /** * @Annotation * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) @@ -21,7 +19,4 @@ use Symfony\Component\Validator\Constraint; * * @api */ -class Null extends Constraint -{ - public $message = 'This value should be null.'; -} +class Null extends IsNull {} diff --git a/src/Symfony/Component/Validator/Constraints/NullValidator.php b/src/Symfony/Component/Validator/Constraints/NullValidator.php index 9c5deb36e7..876924f375 100644 --- a/src/Symfony/Component/Validator/Constraints/NullValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NullValidator.php @@ -11,25 +11,9 @@ namespace Symfony\Component\Validator\Constraints; -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintValidator; - /** * @author Bernhard Schussek * * @api */ -class NullValidator extends ConstraintValidator -{ - /** - * {@inheritdoc} - */ - public function validate($value, Constraint $constraint) - { - if (null !== $value) { - $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->formatValue($value), - )); - } - } -} +class NullValidator extends IsNullValidator {} diff --git a/src/Symfony/Component/Validator/Constraints/True.php b/src/Symfony/Component/Validator/Constraints/True.php index 788e36a9ee..832b84b92b 100644 --- a/src/Symfony/Component/Validator/Constraints/True.php +++ b/src/Symfony/Component/Validator/Constraints/True.php @@ -11,8 +11,6 @@ namespace Symfony\Component\Validator\Constraints; -use Symfony\Component\Validator\Constraint; - /** * @Annotation * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) @@ -21,7 +19,4 @@ use Symfony\Component\Validator\Constraint; * * @api */ -class True extends Constraint -{ - public $message = 'This value should be true.'; -} +class True extends IsTrue {} diff --git a/src/Symfony/Component/Validator/Constraints/TrueValidator.php b/src/Symfony/Component/Validator/Constraints/TrueValidator.php index 480230a909..fb0b7d6cdd 100644 --- a/src/Symfony/Component/Validator/Constraints/TrueValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TrueValidator.php @@ -11,29 +11,9 @@ namespace Symfony\Component\Validator\Constraints; -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintValidator; - /** * @author Bernhard Schussek * * @api */ -class TrueValidator extends ConstraintValidator -{ - /** - * {@inheritdoc} - */ - public function validate($value, Constraint $constraint) - { - if (null === $value) { - return; - } - - if (true !== $value && 1 !== $value && '1' !== $value) { - $this->context->addViolation($constraint->message, array( - '{{ value }}' => $this->formatValue($value), - )); - } - } -} +class TrueValidator extends IsTrueValidator {} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php similarity index 68% rename from src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php index e74cf4acb8..3b7909e639 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php @@ -11,33 +11,33 @@ namespace Symfony\Component\Validator\Tests\Constraints; -use Symfony\Component\Validator\Constraints\False; -use Symfony\Component\Validator\Constraints\FalseValidator; +use Symfony\Component\Validator\Constraints\IsFalse; +use Symfony\Component\Validator\Constraints\IsFalseValidator; -class FalseValidatorTest extends AbstractConstraintValidatorTest +class IsFalseValidatorTest extends AbstractConstraintValidatorTest { protected function createValidator() { - return new FalseValidator(); + return new IsFalseValidator(); } public function testNullIsValid() { - $this->validator->validate(null, new False()); + $this->validator->validate(null, new IsFalse()); $this->assertNoViolation(); } public function testFalseIsValid() { - $this->validator->validate(false, new False()); + $this->validator->validate(false, new IsFalse()); $this->assertNoViolation(); } public function testTrueIsInvalid() { - $constraint = new False(array( + $constraint = new IsFalse(array( 'message' => 'myMessage', )); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php similarity index 79% rename from src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php index 713eabf026..6f01cf5edb 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php @@ -11,19 +11,19 @@ namespace Symfony\Component\Validator\Tests\Constraints; -use Symfony\Component\Validator\Constraints\Null; -use Symfony\Component\Validator\Constraints\NullValidator; +use Symfony\Component\Validator\Constraints\IsNull; +use Symfony\Component\Validator\Constraints\IsNullValidator; -class NullValidatorTest extends AbstractConstraintValidatorTest +class IsNullValidatorTest extends AbstractConstraintValidatorTest { protected function createValidator() { - return new NullValidator(); + return new IsNullValidator(); } public function testNullIsValid() { - $this->validator->validate(null, new Null()); + $this->validator->validate(null, new IsNull()); $this->assertNoViolation(); } @@ -33,7 +33,7 @@ class NullValidatorTest extends AbstractConstraintValidatorTest */ public function testInvalidValues($value, $valueAsString) { - $constraint = new Null(array( + $constraint = new IsNull(array( 'message' => 'myMessage', )); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php similarity index 68% rename from src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php index e0c3ce7875..8f952a993b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php @@ -11,33 +11,33 @@ namespace Symfony\Component\Validator\Tests\Constraints; -use Symfony\Component\Validator\Constraints\True; -use Symfony\Component\Validator\Constraints\TrueValidator; +use Symfony\Component\Validator\Constraints\IsTrue; +use Symfony\Component\Validator\Constraints\IsTrueValidator; -class TrueValidatorTest extends AbstractConstraintValidatorTest +class IsTrueValidatorTest extends AbstractConstraintValidatorTest { protected function createValidator() { - return new TrueValidator(); + return new IsTrueValidator(); } public function testNullIsValid() { - $this->validator->validate(null, new True()); + $this->validator->validate(null, new IsTrue()); $this->assertNoViolation(); } public function testTrueIsValid() { - $this->validator->validate(true, new True()); + $this->validator->validate(true, new IsTrue()); $this->assertNoViolation(); } public function testFalseIsInvalid() { - $constraint = new True(array( + $constraint = new IsTrue(array( 'message' => 'myMessage', ));