-
-
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
80f1096
commit e1434ba
Showing
5 changed files
with
218 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 IsFinite 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/IsFiniteValidator.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 IsFiniteValidator extends ConstraintValidator | ||
{ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof IsFinite) { | ||
throw new UnexpectedTypeException($constraint, IsFinite::class); | ||
} | ||
|
||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if (is_numeric($value) && is_finite($value)) { | ||
return; | ||
} | ||
|
||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(IsFinite::NOT_FINITE_ERROR) | ||
->addViolation(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Validator/Tests/Constraints/IsFiniteTest.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\IsFinite; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; | ||
|
||
class IsFiniteTest 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 | ||
{ | ||
#[IsFinite] | ||
private $a; | ||
|
||
#[IsFinite(message: 'myMessage')] | ||
private $b; | ||
|
||
#[IsFinite(groups: ['my_group'], payload: 'some attached data')] | ||
private $c; | ||
} |
88 changes: 88 additions & 0 deletions
88
src/Symfony/Component/Validator/Tests/Constraints/IsFiniteValidatorTest.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,88 @@ | ||
<?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\IsFinite; | ||
use Symfony\Component\Validator\Constraints\IsFiniteValidator; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
||
class IsFiniteValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
protected function createValidator(): IsFiniteValidator | ||
{ | ||
return new IsFiniteValidator(); | ||
} | ||
|
||
public function testNullIsValid() | ||
{ | ||
$constraint = new IsFinite(); | ||
|
||
$this->validator->validate(null, $constraint); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidValues | ||
*/ | ||
public function testValidValues(mixed $value) | ||
{ | ||
$constraint = new IsFinite(); | ||
|
||
$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 IsFinite([ | ||
'message' => 'myMessage', | ||
]); | ||
|
||
$this->validator->validate($value, $constraint); | ||
|
||
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', $valueAsString) | ||
->setCode(IsFinite::NOT_FINITE_ERROR) | ||
->assertRaised(); | ||
} | ||
|
||
public static function getInvalidValues(): array | ||
{ | ||
$object = new \stdClass(); | ||
$object->foo = 'bar'; | ||
|
||
return [ | ||
[\NAN, 'NAN'], | ||
[\INF, 'INF'], | ||
['', '""'], | ||
['foo', '"foo"'], | ||
[['foo' => 'bar'], 'array'], | ||
[$object, 'object'], | ||
]; | ||
} | ||
} |