Skip to content

Commit

Permalink
[Validator] Add Finite constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-a committed Jul 6, 2023
1 parent 6c85287 commit 222910f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

6.3
---
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Finite.php
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(string $message = null, array $groups = null, mixed $payload = null, array $options = [])
{
parent::__construct($options, $groups, $payload);

$this->message = $message ?? $this->message;
}
}
42 changes: 42 additions & 0 deletions src/Symfony/Component/Validator/Constraints/FiniteValidator.php
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_finite($value)) {
return;
}

$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Finite::NOT_FINITE_ERROR)
->addViolation();
}
}

0 comments on commit 222910f

Please sign in to comment.