-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c85287
commit 679acd9
Showing
5 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 Guillaume Aveline <guillaume@codr.fr> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Finite extends Constraint | ||
{ | ||
public const NOT_FINITE_ERROR = '5f809eb0-78b9-492d-ad37-5a5188390415'; | ||
|
||
protected const ERROR_NAMES = [ | ||
self::NOT_FINITE_ERROR => 'NOT_FINITE_ERROR', | ||
]; | ||
|
||
public $message = 'This value should be finite.'; | ||
|
||
public function __construct(array $options = [], array $groups = null, mixed $payload = null, string $message = null) | ||
{ | ||
parent::__construct($options, $groups, $payload); | ||
|
||
$this->message = $message ?? $this->message; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/Validator/Constraints/FiniteValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* @author Guillaume Aveline <guillaume@codr.fr> | ||
*/ | ||
class FiniteValidator extends ConstraintValidator | ||
{ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof Finite) { | ||
throw new UnexpectedTypeException($constraint, Finite::class); | ||
} | ||
|
||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if (is_numeric($value) && is_finite($value)) { | ||
return; | ||
} | ||
|
||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Finite::NOT_FINITE_ERROR) | ||
->addViolation(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Validator/Tests/Constraints/FiniteTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Constraints; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\Constraints\Finite; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; | ||
|
||
class FiniteTest extends TestCase | ||
{ | ||
public function testAttributes() | ||
{ | ||
$metadata = new ClassMetadata(FiniteDummy::class); | ||
$loader = new AnnotationLoader(); | ||
self::assertTrue($loader->loadClassMetadata($metadata)); | ||
|
||
[$bConstraint] = $metadata->properties['b']->getConstraints(); | ||
self::assertSame('myMessage', $bConstraint->message); | ||
self::assertSame(['Default', 'FiniteDummy'], $bConstraint->groups); | ||
|
||
[$cConstraint] = $metadata->properties['c']->getConstraints(); | ||
self::assertSame(['my_group'], $cConstraint->groups); | ||
self::assertSame('some attached data', $cConstraint->payload); | ||
} | ||
} | ||
|
||
class FiniteDummy | ||
{ | ||
#[Finite] | ||
private $a; | ||
|
||
#[Finite(message: 'myMessage')] | ||
private $b; | ||
|
||
#[Finite(groups: ['my_group'], payload: 'some attached data')] | ||
private $c; | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraints\Finite; | ||
use Symfony\Component\Validator\Constraints\FiniteValidator; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
||
class FiniteValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
protected function createValidator(): FiniteValidator | ||
{ | ||
return new FiniteValidator(); | ||
} | ||
|
||
public function testNullIsValid() | ||
{ | ||
$constraint = new Finite(); | ||
|
||
$this->validator->validate(null, $constraint); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidValues | ||
*/ | ||
public function testValidValues(mixed $value) | ||
{ | ||
$constraint = new Finite(); | ||
|
||
$this->validator->validate($value, $constraint); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public static function getValidValues(): array | ||
{ | ||
return [ | ||
[0], | ||
['0'], | ||
[1.5], | ||
['1.5'], | ||
['12345'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getInvalidValues | ||
*/ | ||
public function testInvalidValues(mixed $value, string $valueAsString) | ||
{ | ||
$constraint = new Finite([ | ||
'message' => 'myMessage', | ||
]); | ||
|
||
$this->validator->validate($value, $constraint); | ||
|
||
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', $valueAsString) | ||
->setCode(Finite::NOT_FINITE_ERROR) | ||
->assertRaised(); | ||
} | ||
|
||
public static function getInvalidValues(): array | ||
{ | ||
return [ | ||
[\NAN, 'NAN'], | ||
[\INF, 'INF'], | ||
['', '""'], | ||
['foo', '"foo"'], | ||
]; | ||
} | ||
} |