File.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Exception\ConstraintDefinitionException;
  13. /**
  14. * @Annotation
  15. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16. *
  17. * @property int $maxSize
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class File extends Constraint
  22. {
  23. // Check the Image constraint for clashes if adding new constants here
  24. const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
  25. const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
  26. const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
  27. const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
  28. const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
  29. protected static $errorNames = array(
  30. self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
  31. self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
  32. self::EMPTY_ERROR => 'EMPTY_ERROR',
  33. self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
  34. self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
  35. );
  36. public $binaryFormat;
  37. public $mimeTypes = array();
  38. public $notFoundMessage = 'The file could not be found.';
  39. public $notReadableMessage = 'The file is not readable.';
  40. public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
  41. public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
  42. public $disallowEmptyMessage = 'An empty file is not allowed.';
  43. public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
  44. public $uploadFormSizeErrorMessage = 'The file is too large.';
  45. public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
  46. public $uploadNoFileErrorMessage = 'No file was uploaded.';
  47. public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
  48. public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
  49. public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
  50. public $uploadErrorMessage = 'The file could not be uploaded.';
  51. protected $maxSize;
  52. public function __construct($options = null)
  53. {
  54. parent::__construct($options);
  55. if (null !== $this->maxSize) {
  56. $this->normalizeBinaryFormat($this->maxSize);
  57. }
  58. }
  59. public function __set($option, $value)
  60. {
  61. if ('maxSize' === $option) {
  62. $this->normalizeBinaryFormat($value);
  63. return;
  64. }
  65. parent::__set($option, $value);
  66. }
  67. public function __get($option)
  68. {
  69. if ('maxSize' === $option) {
  70. return $this->maxSize;
  71. }
  72. return parent::__get($option);
  73. }
  74. public function __isset($option)
  75. {
  76. if ('maxSize' === $option) {
  77. return true;
  78. }
  79. return parent::__isset($option);
  80. }
  81. private function normalizeBinaryFormat($maxSize)
  82. {
  83. $sizeInt = (int) $maxSize;
  84. if (ctype_digit((string) $maxSize)) {
  85. $this->maxSize = $sizeInt;
  86. $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
  87. } elseif (preg_match('/^\d++k$/i', $maxSize)) {
  88. $this->maxSize = $sizeInt * 1000;
  89. $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
  90. } elseif (preg_match('/^\d++M$/i', $maxSize)) {
  91. $this->maxSize = $sizeInt * 1000000;
  92. $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
  93. } elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
  94. $this->maxSize = $sizeInt << 10;
  95. $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
  96. } elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
  97. $this->maxSize = $sizeInt << 20;
  98. $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
  99. } else {
  100. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
  101. }
  102. }
  103. }