FileValidator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  15. use Symfony\Component\HttpFoundation\File\File as FileObject;
  16. class FileValidator extends ConstraintValidator
  17. {
  18. public function isValid($value, Constraint $constraint)
  19. {
  20. if (null === $value || '' === $value) {
  21. return true;
  22. }
  23. if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString()'))) {
  24. throw new UnexpectedTypeException($value, 'string');
  25. }
  26. if ($value instanceof FileObject && null === $value->getPath()) {
  27. return true;
  28. }
  29. $path = $value instanceof FileObject ? $value->getPath() : (string) $value;
  30. if (!file_exists($path)) {
  31. $this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path));
  32. return false;
  33. }
  34. if (!is_readable($path)) {
  35. $this->setMessage($constraint->notReadableMessage, array('{{ file }}' => $path));
  36. return false;
  37. }
  38. if ($constraint->maxSize) {
  39. if (ctype_digit((string) $constraint->maxSize)) {
  40. $size = filesize($path);
  41. $limit = $constraint->maxSize;
  42. $suffix = ' bytes';
  43. } else if (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
  44. $size = round(filesize($path) / 1000, 2);
  45. $limit = $matches[1];
  46. $suffix = ' kB';
  47. } else if (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
  48. $size = round(filesize($path) / 1000000, 2);
  49. $limit = $matches[1];
  50. $suffix = ' MB';
  51. } else {
  52. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
  53. }
  54. if ($size > $limit) {
  55. $this->setMessage($constraint->maxSizeMessage, array(
  56. '{{ size }}' => $size . $suffix,
  57. '{{ limit }}' => $limit . $suffix,
  58. '{{ file }}' => $path,
  59. ));
  60. return false;
  61. }
  62. }
  63. if ($constraint->mimeTypes) {
  64. if (!$value instanceof FileObject) {
  65. $value = new FileObject($value);
  66. }
  67. if (!in_array($value->getMimeType(), (array) $constraint->mimeTypes)) {
  68. $this->setMessage($constraint->mimeTypesMessage, array(
  69. '{{ type }}' => '"'.$value->getMimeType().'"',
  70. '{{ types }}' => '"'.implode('", "', (array) $constraint->mimeTypes).'"',
  71. '{{ file }}' => $path,
  72. ));
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. }