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;
}
}

View file

@ -43,9 +43,7 @@ namespace App\Validator\Constraints\Misc;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ValidRange extends Constraint
{
public string $message = 'validator.invalid_range';

View file

@ -26,9 +26,8 @@ use Symfony\Component\Validator\Constraint;
/**
* This constraint restricts a user in that way that it can not lock itself out of the user system.
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class NoLockout extends Constraint
{
public string $message = 'validator.noLockout';

View file

@ -34,11 +34,8 @@ use Symfony\Component\Validator\ConstraintValidator;
class NoLockoutValidator extends ConstraintValidator
{
protected array $perm_structure;
public function __construct(protected PermissionManager $resolver, protected Security $security, protected EntityManagerInterface $entityManager)
{
$this->perm_structure = $resolver->getPermissionStructure();
}
/**
@ -71,6 +68,8 @@ class NoLockoutValidator extends ConstraintValidator
) ?? false)) {
$this->context->addViolation($constraint->message);
}
} else {
throw new \LogicException('The NoLockout constraint can only be used on User or Group objects.');
}
}
}

View file

@ -27,9 +27,8 @@ use Symfony\Component\Validator\Constraint;
/**
* Constraints the parent property on StructuralDBElement objects in the way, that neither the object self nor any
* of its children can be assigned.
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class NoneOfItsChildren extends Constraint
{
/**

View file

@ -26,9 +26,8 @@ use Symfony\Component\Validator\Constraint;
/**
* This constraint checks that the given ProjectBuildRequest is valid.
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class ValidProjectBuildRequest extends Constraint
{
public function getTargets(): string

View file

@ -27,9 +27,8 @@ use Symfony\Component\Validator\Constraint;
/**
* If a property is marked with this constraint, the choosen value (of type StructuralDBElement)
* must NOT be marked as not selectable.
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Selectable extends Constraint
{
public $message = 'validator.isSelectable';

View file

@ -27,9 +27,8 @@ use Symfony\Component\Validator\Constraints\Url;
/**
* Constraints the field that way that the content is either an url or a path to a builtin ressource (like %FOOTPRINTS%).
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class UrlOrBuiltin extends Url
{
/**

View file

@ -27,6 +27,7 @@ use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ValidFileFilter extends Constraint
{
}

View file

@ -27,9 +27,8 @@ use Symfony\Component\Validator\Constraint;
/**
* A constraint "dummy" to validate the PartLot.
* We need to access services in our Validator, so we can not use a simple callback on PartLot.
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class ValidPartLot extends Constraint
{
public function getTargets(): string

View file

@ -28,8 +28,8 @@ use Symfony\Component\Validator\Constraint;
* A PermissionEmbed object with this annotation will be checked with ValidPermissionValidator.
* That means the alsoSet values of the permission operations are set.
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ValidPermission extends Constraint
{
}

View file

@ -26,8 +26,8 @@ use Symfony\Component\Validator\Constraint;
/**
* A constraint to validate the theme setting of the user.
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ValidTheme extends Constraint
{
public string $message = 'validator.selected_theme_is_invalid';