PageRenderTime 70ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Validator/Constraints/FileValidator.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 125 lines | 83 code | 21 blank | 21 comment | 18 complexity | 44381fe40f42066796a25f817812df23 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  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. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. /**
  18. * @api
  19. */
  20. class FileValidator extends ConstraintValidator
  21. {
  22. /**
  23. * Checks if the passed value is valid.
  24. *
  25. * @param mixed $value The value that should be validated
  26. * @param Constraint $constraint The constraint for the validation
  27. *
  28. * @return Boolean Whether or not the value is valid
  29. *
  30. * @api
  31. */
  32. public function isValid($value, Constraint $constraint)
  33. {
  34. if (null === $value || '' === $value) {
  35. return true;
  36. }
  37. if ($value instanceof UploadedFile && !$value->isValid()) {
  38. switch ($value->getError()) {
  39. case UPLOAD_ERR_INI_SIZE:
  40. $maxSize = UploadedFile::getMaxFilesize();
  41. $maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
  42. $this->setMessage($constraint->uploadIniSizeErrorMessage, array('{{ limit }}' => $maxSize.' bytes'));
  43. return false;
  44. case UPLOAD_ERR_FORM_SIZE:
  45. $this->setMessage($constraint->uploadFormSizeErrorMessage);
  46. return false;
  47. default:
  48. $this->setMessage($constraint->uploadErrorMessage);
  49. return false;
  50. }
  51. }
  52. if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
  53. throw new UnexpectedTypeException($value, 'string');
  54. }
  55. $path = $value instanceof FileObject ? $value->getPathname() : (string) $value;
  56. if (!file_exists($path)) {
  57. $this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path));
  58. return false;
  59. }
  60. if (!is_readable($path)) {
  61. $this->setMessage($constraint->notReadableMessage, array('{{ file }}' => $path));
  62. return false;
  63. }
  64. if ($constraint->maxSize) {
  65. if (ctype_digit((string) $constraint->maxSize)) {
  66. $size = filesize($path);
  67. $limit = $constraint->maxSize;
  68. $suffix = ' bytes';
  69. } elseif (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
  70. $size = round(filesize($path) / 1000, 2);
  71. $limit = $matches[1];
  72. $suffix = ' kB';
  73. } elseif (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
  74. $size = round(filesize($path) / 1000000, 2);
  75. $limit = $matches[1];
  76. $suffix = ' MB';
  77. } else {
  78. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
  79. }
  80. if ($size > $limit) {
  81. $this->setMessage($constraint->maxSizeMessage, array(
  82. '{{ size }}' => $size.$suffix,
  83. '{{ limit }}' => $limit.$suffix,
  84. '{{ file }}' => $path,
  85. ));
  86. return false;
  87. }
  88. }
  89. if ($constraint->mimeTypes) {
  90. if (!$value instanceof FileObject) {
  91. $value = new FileObject($value);
  92. }
  93. if (!in_array($value->getMimeType(), (array) $constraint->mimeTypes)) {
  94. $this->setMessage($constraint->mimeTypesMessage, array(
  95. '{{ type }}' => '"'.$value->getMimeType().'"',
  96. '{{ types }}' => '"'.implode('", "', (array) $constraint->mimeTypes).'"',
  97. '{{ file }}' => $path,
  98. ));
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. }