2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
2010-09-09 13:40:40 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File as FileObject;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class FileValidator extends ConstraintValidator
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
public function isValid($value, Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-12-21 02:59:17 +00:00
|
|
|
if (null === $value || '' === $value) {
|
2010-06-24 10:24:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-15 11:47:10 +01:00
|
|
|
if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString()'))) {
|
2010-06-24 10:24:08 +01:00
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
|
|
|
|
2010-09-09 15:07:12 +01:00
|
|
|
if ($value instanceof FileObject && null === $value->getPath()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-15 11:47:10 +01:00
|
|
|
$path = $value instanceof FileObject ? $value->getPath() : (string)$value;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
if (!file_exists($path)) {
|
2010-09-29 06:59:45 +01:00
|
|
|
$this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_readable($path)) {
|
2010-09-29 06:59:45 +01:00
|
|
|
$this->setMessage($constraint->notReadableMessage, array('{{ file }}' => $path));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($constraint->maxSize) {
|
|
|
|
if (ctype_digit((string)$constraint->maxSize)) {
|
|
|
|
$size = filesize($path);
|
|
|
|
$limit = $constraint->maxSize;
|
|
|
|
$suffix = ' bytes';
|
2010-09-09 15:07:12 +01:00
|
|
|
} else if (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$size = round(filesize($path) / 1000, 2);
|
|
|
|
$limit = $matches[1];
|
|
|
|
$suffix = ' kB';
|
2010-09-09 15:07:12 +01:00
|
|
|
} else if (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$size = round(filesize($path) / 1000000, 2);
|
|
|
|
$limit = $matches[1];
|
|
|
|
$suffix = ' MB';
|
|
|
|
} else {
|
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($size > $limit) {
|
|
|
|
$this->setMessage($constraint->maxSizeMessage, array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ size }}' => $size . $suffix,
|
|
|
|
'{{ limit }}' => $limit . $suffix,
|
|
|
|
'{{ file }}' => $path,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($constraint->mimeTypes) {
|
2010-07-15 11:47:10 +01:00
|
|
|
if (!$value instanceof FileObject) {
|
2010-12-15 18:22:14 +00:00
|
|
|
$value = new FileObject($value);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($value->getMimeType(), (array)$constraint->mimeTypes)) {
|
|
|
|
$this->setMessage($constraint->mimeTypesMessage, array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ type }}' => '"'.$value->getMimeType().'"',
|
|
|
|
'{{ types }}' => '"'.implode('", "', (array)$constraint->mimeTypes).'"',
|
|
|
|
'{{ file }}' => $path,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2010-07-15 11:47:10 +01:00
|
|
|
}
|