Skip to content

Commit 44edbdf

Browse files
Fixed compatibility with PHP7 and up by introducing new constraints (IsNull, IsTrue, IsFalse) and related validators (IsNullValidator, IsTrueValidator, IsFalseValidator)
1 parent 89f6e1e commit 44edbdf

19 files changed

+241
-98
lines changed

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ public function guessTypeForConstraint(Constraint $constraint)
157157

158158
case 'Symfony\Component\Validator\Constraints\True':
159159
case 'Symfony\Component\Validator\Constraints\False':
160+
case 'Symfony\Component\Validator\Constraints\IsTrue':
161+
case 'Symfony\Component\Validator\Constraints\IsFalse':
160162
return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);
161163
}
162164
}
@@ -174,6 +176,7 @@ public function guessRequiredForConstraint(Constraint $constraint)
174176
case 'Symfony\Component\Validator\Constraints\NotNull':
175177
case 'Symfony\Component\Validator\Constraints\NotBlank':
176178
case 'Symfony\Component\Validator\Constraints\True':
179+
case 'Symfony\Component\Validator\Constraints\IsTrue':
177180
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
178181
}
179182
}

src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Symfony\Component\Validator\Constraints\NotBlank;
2020
use Symfony\Component\Validator\Constraints\NotNull;
2121
use Symfony\Component\Validator\Constraints\Range;
22-
use Symfony\Component\Validator\Constraints\True;
22+
use Symfony\Component\Validator\Constraints\IsTrue;
2323
use Symfony\Component\Validator\Constraints\Type;
2424
use Symfony\Component\Validator\Mapping\ClassMetadata;
2525

@@ -64,7 +64,7 @@ public function guessRequiredProvider()
6464
return array(
6565
array(new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
6666
array(new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
67-
array(new True(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
67+
array(new IsTrue(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
6868
array(new Length(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
6969
array(new Range(array('min' => 1, 'max' => 20)), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
7070
);
@@ -84,6 +84,18 @@ public function testGuessRequired($constraint, $guess)
8484
$this->assertEquals($guess, $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
8585
}
8686

87+
/**
88+
* @group legacy
89+
*/
90+
public function testLegacyGuessRequired()
91+
{
92+
if (PHP_VERSION_ID >= 70000) {
93+
$this->markTestSkipped('Cannot use a class called True on PHP 7 or higher.');
94+
}
95+
$true = 'Symfony\Component\Validator\Constraints\True';
96+
$this->testGuessRequired(new $true(), new ValueGuess(true, Guess::HIGH_CONFIDENCE));
97+
}
98+
8799
public function testGuessRequiredReturnsFalseForUnmappedProperties()
88100
{
89101
$this->assertEquals(new ValueGuess(false, Guess::LOW_CONFIDENCE), $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));

src/Symfony/Component/Form/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require-dev": {
2626
"symfony/phpunit-bridge": "~2.7",
2727
"doctrine/collections": "~1.0",
28-
"symfony/validator": "~2.3.0,>=2.3.20",
28+
"symfony/validator": "~2.3.29",
2929
"symfony/translation": "~2.0,>=2.0.5",
3030
"symfony/http-foundation": "~2.2"
3131
},

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.3.29
5+
------
6+
7+
* fixed compatibility with PHP7 and up by introducing new constraints (IsNull, IsTrue, IsFalse) and related validators (IsNullValidator, IsTrueValidator, IsFalseValidator)
8+
49
2.3.0
510
-----
611

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Constraint;
15-
1614
/**
1715
* @Annotation
1816
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@@ -21,7 +19,4 @@
2119
*
2220
* @api
2321
*/
24-
class False extends Constraint
25-
{
26-
public $message = 'This value should be false.';
27-
}
22+
class False extends IsFalse {}

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,9 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\ConstraintValidator;
16-
1714
/**
1815
* @author Bernhard Schussek <bschussek@gmail.com>
1916
*
2017
* @api
2118
*/
22-
class FalseValidator extends ConstraintValidator
23-
{
24-
/**
25-
* {@inheritdoc}
26-
*/
27-
public function validate($value, Constraint $constraint)
28-
{
29-
if (null === $value || false === $value || 0 === $value || '0' === $value) {
30-
return;
31-
}
32-
33-
$this->context->addViolation($constraint->message, array(
34-
'{{ value }}' => $this->formatValue($value),
35-
));
36-
}
37-
}
19+
class FalseValidator extends IsFalseValidator {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Bernhard Schussek <bschussek@gmail.com>
21+
*
22+
* @api
23+
*/
24+
class IsFalse extends Constraint
25+
{
26+
public $message = 'This value should be false.';
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
17+
/**
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*
20+
* @api
21+
*/
22+
class IsFalseValidator extends ConstraintValidator
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function validate($value, Constraint $constraint)
28+
{
29+
if (null === $value || false === $value || 0 === $value || '0' === $value) {
30+
return;
31+
}
32+
33+
$this->context->addViolation($constraint->message, array(
34+
'{{ value }}' => $this->formatValue($value),
35+
));
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Bernhard Schussek <bschussek@gmail.com>
21+
*
22+
* @api
23+
*/
24+
class IsNull extends Constraint
25+
{
26+
public $message = 'This value should be null.';
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
17+
/**
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*
20+
* @api
21+
*/
22+
class IsNullValidator extends ConstraintValidator
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function validate($value, Constraint $constraint)
28+
{
29+
if (null !== $value) {
30+
$this->context->addViolation($constraint->message, array(
31+
'{{ value }}' => $this->formatValue($value),
32+
));
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)