Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 1 commit into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Copy link
Member

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)

Copy link
Contributor Author

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]:

In src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php
#14228 (comment):

@@ -155,8 +155,8 @@ public function guessTypeForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\Count':
return new TypeGuess('collection', array(), Guess::LOW_CONFIDENCE);

  •        case 'Symfony\Component\Validator\Constraints\True':
    
  •        case 'Symfony\Component\Validator\Constraints\False':
    
  •        case 'Symfony\Component\Validator\Constraints\IsTrue':
    
  •        case 'Symfony\Component\Validator\Constraints\IsFalse':
    

the deprecated names should be kept here (this is the issue when using a
switch(get_class(): it does not support inheritance)


Reply to this email directly or view it on GitHub
https://github.com/symfony/symfony/pull/14228/files#r29848859.

return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);
}
}
Expand All @@ -174,6 +176,7 @@ public function guessRequiredForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\NotNull':
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)),
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)),
);
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----

Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/Validator/Constraints/False.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
Expand All @@ -21,7 +19,4 @@
*
* @api
*/
class False extends Constraint
{
public $message = 'This value should be false.';
}
class False extends IsFalse {}
20 changes: 1 addition & 19 deletions src/Symfony/Component/Validator/Constraints/FalseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
27 changes: 27 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsFalse.php
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.';
}
37 changes: 37 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsFalseValidator.php
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),
));
}
}
27 changes: 27 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsNull.php
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.';
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsNullValidator.php
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),
));
}
}
}
27 changes: 27 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsTrue.php
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.';
}
39 changes: 39 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsTrueValidator.php
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),
));
}
}
}
7 changes: 1 addition & 6 deletions src/Symfony/Component/Validator/Constraints/Null.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
Expand All @@ -21,7 +19,4 @@
*
* @api
*/
class Null extends Constraint
{
public $message = 'This value should be null.';
}
class Null extends IsNull {}
18 changes: 1 addition & 17 deletions src/Symfony/Component/Validator/Constraints/NullValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
7 changes: 1 addition & 6 deletions src/Symfony/Component/Validator/Constraints/True.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
Expand All @@ -21,7 +19,4 @@
*
* @api
*/
class True extends Constraint
{
public $message = 'This value should be true.';
}
class True extends IsTrue {}
Loading