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:
15
src/Validator/ImageSafety.php
Normal file
15
src/Validator/ImageSafety.php
Normal 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.';
|
||||
}
|
30
src/Validator/ImageSafetyValidator.php
Normal file
30
src/Validator/ImageSafetyValidator.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user