-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add PHP7 compatible versions for the Null/True/False constraints as they are reserved words in PHP7 #14228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PHP7 compatible versions for the Null/True/False constraints as they are reserved words in PHP7 #14228
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,8 @@ public function guessTypeForConstraint(Constraint $constraint) | |
|
||
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 @@ public function guessRequiredForConstraint(Constraint $constraint) | |
case 'Symfony\Component\Validator\Constraints\NotNull': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you forgot to keep it here |
||
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); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
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 @@ public function guessRequiredProvider() | |
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)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should keep a legacy test (skipped on PHP 7) using the old constraint There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, committed |
||
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 @@ public function testGuessRequired($constraint, $guess) | |
$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)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,27 +11,9 @@ | |
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
/** | ||
* @author Bernhard Schussek <[email protected]> | ||
* | ||
* @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 {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @api | ||
*/ | ||
class IsFalse extends Constraint | ||
{ | ||
public $message = 'This value should be false.'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @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), | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @api | ||
*/ | ||
class IsNull extends Constraint | ||
{ | ||
public $message = 'This value should be null.'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @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), | ||
)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @api | ||
*/ | ||
class IsTrue extends Constraint | ||
{ | ||
public $message = 'This value should be true.'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @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), | ||
)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,25 +11,9 @@ | |
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
/** | ||
* @author Bernhard Schussek <[email protected]> | ||
* | ||
* @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 {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the deprecated names should be kept here (this is the issue when using a
switch(get_class()
: it does not support inheritance)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
2015-05-07 15:00 GMT+02:00 Christophe Coevoet [email protected]: