Moved custom validators from annotations to attributes

This commit is contained in:
Jan Böhmer 2023-06-11 19:32:15 +02:00
parent e5a14557a2
commit 930adaf439
27 changed files with 50 additions and 148 deletions

View file

@ -22,25 +22,11 @@ declare(strict_types=1);
*/
namespace App\Validator\Constraints\BigDecimal;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\Positive;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
class BigDecimalPositive extends GreaterThan
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class BigDecimalPositive extends Positive
{
use BigNumberConstraintTrait;
public $message = 'This value should be positive.';
public function __construct($options = null)
{
parent::__construct($this->configureNumberConstraintOptions($options));
}
public function validatedBy(): string
{
return BigDecimalGreaterThanValidator::class;

View file

@ -22,25 +22,11 @@ declare(strict_types=1);
*/
namespace App\Validator\Constraints\BigDecimal;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\PositiveOrZero;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
class BigDecimalPositiveOrZero extends GreaterThanOrEqual
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class BigDecimalPositiveOrZero extends PositiveOrZero
{
use BigNumberConstraintTrait;
public $message = 'This value should be either positive or zero.';
public function __construct($options = null)
{
parent::__construct($this->configureNumberConstraintOptions($options));
}
public function validatedBy(): string
{
return BigDecimalGreaterThenOrEqualValidator::class;

View file

@ -1,51 +0,0 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Validator\Constraints\BigDecimal;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use function is_array;
trait BigNumberConstraintTrait
{
private function configureNumberConstraintOptions($options): array
{
if (null === $options) {
$options = [];
} elseif (!is_array($options)) {
$options = [$this->getDefaultOption() => $options];
}
if (isset($options['propertyPath'])) {
throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', static::class));
}
if (isset($options['value'])) {
throw new ConstraintDefinitionException(sprintf('The "value" option of the "%s" constraint cannot be set.', static::class));
}
$options['value'] = 0;
return $options;
}
}