An error occurred while loading the file. Please try again.
-
ada66055
<?php declare(strict_types=1);
/*
* Copyright (C) 2015-2017 IRSTEA
* All rights reserved.
*/
namespace Irstea\FileUploadBundle\Validation;
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Vérifie que la taille du fichier.
*/
class FileSizeValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$value instanceof UploadedFileInterface) {
return;
}
if ($constraint->min && $value->getSize() < $constraint->min) {
$this->context->buildViolation($constraint->minMessage)
->setParameter('%displayName%', $value->getDisplayName())
->setParameter('%min%', $constraint->min)
->addViolation();
}
if ($constraint->max && $value->getSize() > $constraint->max) {
$this->context->buildViolation($constraint->maxMessage)
->setParameter('%displayName%', $value->getDisplayName())
->setParameter('%max%', $constraint->max)
->addViolation();
}
}
}