add Moderation.php (#14)

Co-authored-by: bastien <bastien.ollier1@gmail.com>
Co-authored-by: clfreville2 <clement.freville2@etu.uca.fr>
Reviewed-on: https://codefirst.iut.uca.fr/git/clement.freville2/herbarium/pulls/14
Reviewed-by: Clément FRÉVILLE <clement.freville2@etu.uca.fr>
This commit is contained in:
Bastien OLLIER
2024-06-13 08:08:46 +02:00
parent 6a6a135891
commit ccb2b541ea
9 changed files with 128 additions and 1 deletions

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class ImageSafety extends Constraint
{
public string $message = 'The uploaded image is not safe.';
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Validator;
use App\Service\ImageSafetyServiceInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ImageSafetyValidator extends ConstraintValidator
{
public function __construct(private readonly ImageSafetyServiceInterface $imageSafetyService)
{
}
/**
* @param mixed $value
* @param ImageSafety $constraint
* @return void
*/
public function validate(mixed $value, Constraint $constraint): void
{
if (null === $value || '' === $value) {
return;
}
if (!$this->imageSafetyService->isValid($value)) {
$this->context->buildViolation($constraint->message)->addViolation();
}
}
}