public function FileValidator::validate in Plug 7
Checks if the passed value is valid.
@api
Parameters
mixed $value The value that should be validated:
Constraint $constraint The constraint for the validation:
Overrides ConstraintValidatorInterface::validate
1 call to FileValidator::validate()
- ImageValidator::validate in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Constraints/ ImageValidator.php - Checks if the passed value is valid.
1 method overrides FileValidator::validate()
- ImageValidator::validate in lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Constraints/ ImageValidator.php - Checks if the passed value is valid.
File
- lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Constraints/ FileValidator.php, line 43
Class
- FileValidator
- @author Bernhard Schussek <bschussek@gmail.com>
Namespace
Symfony\Component\Validator\ConstraintsCode
public function validate($value, Constraint $constraint) {
if (!$constraint instanceof File) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\File');
}
if (null === $value || '' === $value) {
return;
}
if ($value instanceof UploadedFile && !$value
->isValid()) {
switch ($value
->getError()) {
case UPLOAD_ERR_INI_SIZE:
$iniLimitSize = UploadedFile::getMaxFilesize();
if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
$limitInBytes = $constraint->maxSize;
$binaryFormat = $constraint->binaryFormat;
}
else {
$limitInBytes = $iniLimitSize;
$binaryFormat = true;
}
list($sizeAsString, $limitAsString, $suffix) = $this
->factorizeSizes(0, $limitInBytes, $binaryFormat);
$this
->buildViolation($constraint->uploadIniSizeErrorMessage)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setCode(UPLOAD_ERR_INI_SIZE)
->addViolation();
return;
case UPLOAD_ERR_FORM_SIZE:
$this
->buildViolation($constraint->uploadFormSizeErrorMessage)
->setCode(UPLOAD_ERR_FORM_SIZE)
->addViolation();
return;
case UPLOAD_ERR_PARTIAL:
$this
->buildViolation($constraint->uploadPartialErrorMessage)
->setCode(UPLOAD_ERR_PARTIAL)
->addViolation();
return;
case UPLOAD_ERR_NO_FILE:
$this
->buildViolation($constraint->uploadNoFileErrorMessage)
->setCode(UPLOAD_ERR_NO_FILE)
->addViolation();
return;
case UPLOAD_ERR_NO_TMP_DIR:
$this
->buildViolation($constraint->uploadNoTmpDirErrorMessage)
->setCode(UPLOAD_ERR_NO_TMP_DIR)
->addViolation();
return;
case UPLOAD_ERR_CANT_WRITE:
$this
->buildViolation($constraint->uploadCantWriteErrorMessage)
->setCode(UPLOAD_ERR_CANT_WRITE)
->addViolation();
return;
case UPLOAD_ERR_EXTENSION:
$this
->buildViolation($constraint->uploadExtensionErrorMessage)
->setCode(UPLOAD_ERR_EXTENSION)
->addViolation();
return;
default:
$this
->buildViolation($constraint->uploadErrorMessage)
->setCode($value
->getError())
->addViolation();
return;
}
}
if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$path = $value instanceof FileObject ? $value
->getPathname() : (string) $value;
if (!is_file($path)) {
$this
->buildViolation($constraint->notFoundMessage)
->setParameter('{{ file }}', $this
->formatValue($path))
->setCode(File::NOT_FOUND_ERROR)
->addViolation();
return;
}
if (!is_readable($path)) {
$this
->buildViolation($constraint->notReadableMessage)
->setParameter('{{ file }}', $this
->formatValue($path))
->setCode(File::NOT_READABLE_ERROR)
->addViolation();
return;
}
$sizeInBytes = filesize($path);
if (0 === $sizeInBytes) {
$this
->buildViolation($constraint->disallowEmptyMessage)
->setParameter('{{ file }}', $this
->formatValue($path))
->setCode(File::EMPTY_ERROR)
->addViolation();
return;
}
if ($constraint->maxSize) {
$limitInBytes = $constraint->maxSize;
if ($sizeInBytes > $limitInBytes) {
list($sizeAsString, $limitAsString, $suffix) = $this
->factorizeSizes($sizeInBytes, $limitInBytes, $constraint->binaryFormat);
$this
->buildViolation($constraint->maxSizeMessage)
->setParameter('{{ file }}', $this
->formatValue($path))
->setParameter('{{ size }}', $sizeAsString)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setCode(File::TOO_LARGE_ERROR)
->addViolation();
return;
}
}
if ($constraint->mimeTypes) {
if (!$value instanceof FileObject) {
$value = new FileObject($value);
}
$mimeTypes = (array) $constraint->mimeTypes;
$mime = $value
->getMimeType();
foreach ($mimeTypes as $mimeType) {
if ($mimeType === $mime) {
return;
}
if ($discrete = strstr($mimeType, '/*', true)) {
if (strstr($mime, '/', true) === $discrete) {
return;
}
}
}
$this
->buildViolation($constraint->mimeTypesMessage)
->setParameter('{{ file }}', $this
->formatValue($path))
->setParameter('{{ type }}', $this
->formatValue($mime))
->setParameter('{{ types }}', $this
->formatValues($mimeTypes))
->setCode(File::INVALID_MIME_TYPE_ERROR)
->addViolation();
}
}