8000 [Validator] Add `Finite` constraint · symfony/symfony@222910f · GitHub
[go: up one dir, main page]

Skip to content

Commit 222910f

Browse files
committed
[Validator] Add Finite constraint
1 parent 6c85287 commit 222910f

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Allow single integer for the `versions` option of the `Uuid` constraint
88
* Allow single constraint to be passed to the `constraints` option of the `When` constraint
9+
* Add `Finite` contraint
910

1011
6.3
1112
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
*
19+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
20+
*
21+
* @author Guillaume Aveline <guillaume@codr.fr>
22+
*/
23+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
24+
class Finite extends Constraint
25+
{
26+
public const NOT_FINITE_ERROR = '5f809eb0-78b9-492d-ad37-5a5188390415';
27+
28+
protected const ERROR_NAMES = [
29+
self::NOT_FINITE_ERROR => 'NOT_FINITE_ERROR',
30+
];
31+
32+
public $message = 'This value should be finite.';
33+
34+
public function __construct(string $message = null, array $groups = null, mixed $payload = null, array $options = [])
35+
{
36+
parent::__construct($options, $groups, $payload);
37+
38+
$this->message = $message ?? $this->message;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* @author Guillaume Aveline <guillaume@codr.fr>
20+
*/
21+
class FiniteValidator extends ConstraintValidator
22+
{
23+
public function validate(mixed $value, Constraint $constraint): void
24+
{
25+
if (!$constraint instanceof Finite) {
26+
throw new UnexpectedTypeException($constraint, Finite::class);
27+
}
28+
29+
if (null === $value) {
30+
return;
31+
}
32+
33+
if(is_finite($value)) {
34+
return;
35+
}
36+
37+
$this->context->buildViolation($constraint->message)
38+
->setParameter('{{ value }}', $this->formatValue($value))
39+
->setCode(Finite::NOT_FINITE_ERROR)
40+
->addViolation();
41+
}
42+
}

0 commit comments

Comments
 (0)
0