File.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  22. class File extends Constraint
  23. {
  24. // Check the Image constraint for clashes if adding new constants here
  25. public const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
  26. public const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
  27. public const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
  28. public const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
  29. public const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
  30. protected static $errorNames = [
  31. self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
  32. self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
  33. self::EMPTY_ERROR => 'EMPTY_ERROR',
  34. self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
  35. self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
  36. ];
  37. public $binaryFormat;
  38. public $mimeTypes = [];
  39. public $notFoundMessage = 'The file could not be found.';
  40. public $notReadableMessage = 'The file is not readable.';
  41. public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
  42. public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
  43. public $disallowEmptyMessage = 'An empty file is not allowed.';
  44. public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
  45. public $uploadFormSizeErrorMessage = 'The file is too large.';
  46. public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
  47. public $uploadNoFileErrorMessage = 'No file was uploaded.';
  48. public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
  49. public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
  50. public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
  51. public $uploadErrorMessage = 'The file could not be uploaded.';
  52. protected $maxSize;
  53. /**
  54. * {@inheritdoc}
  55. *
  56. * @param int|string|null $maxSize
  57. * @param string[]|string|null $mimeTypes
  58. */
  59. public function __construct(
  60. array $options = null,
  61. $maxSize = null,
  62. bool $binaryFormat = null,
  63. $mimeTypes = null,
  64. string $notFoundMessage = null,
  65. string $notReadableMessage = null,
  66. string $maxSizeMessage = null,
  67. string $mimeTypesMessage = null,
  68. string $disallowEmptyMessage = null,
  69. string $uploadIniSizeErrorMessage = null,
  70. string $uploadFormSizeErrorMessage = null,
  71. string $uploadPartialErrorMessage = null,
  72. string $uploadNoFileErrorMessage = null,
  73. string $uploadNoTmpDirErrorMessage = null,
  74. string $uploadCantWriteErrorMessage = null,
  75. string $uploadExtensionErrorMessage = null,
  76. string $uploadErrorMessage = null,
  77. array $groups = null,
  78. $payload = null
  79. ) {
  80. if (null !== $maxSize && !\is_int($maxSize) && !\is_string($maxSize)) {
  81. throw new \TypeError(sprintf('"%s": Expected argument $maxSize to be either null, an integer or a string, got "%s".', __METHOD__, get_debug_type($maxSize)));
  82. }
  83. if (null !== $mimeTypes && !\is_array($mimeTypes) && !\is_string($mimeTypes)) {
  84. throw new \TypeError(sprintf('"%s": Expected argument $mimeTypes to be either null, an array or a string, got "%s".', __METHOD__, get_debug_type($mimeTypes)));
  85. }
  86. parent::__construct($options, $groups, $payload);
  87. $this->maxSize = $maxSize ?? $this->maxSize;
  88. $this->binaryFormat = $binaryFormat ?? $this->binaryFormat;
  89. $this->mimeTypes = $mimeTypes ?? $this->mimeTypes;
  90. $this->notFoundMessage = $notFoundMessage ?? $this->notFoundMessage;
  91. $this->notReadableMessage = $notReadableMessage ?? $this->notReadableMessage;
  92. $this->maxSizeMessage = $maxSizeMessage ?? $this->maxSizeMessage;
  93. $this->mimeTypesMessage = $mimeTypesMessage ?? $this->mimeTypesMessage;
  94. $this->disallowEmptyMessage = $disallowEmptyMessage ?? $this->disallowEmptyMessage;
  95. $this->uploadIniSizeErrorMessage = $uploadIniSizeErrorMessage ?? $this->uploadIniSizeErrorMessage;
  96. $this->uploadFormSizeErrorMessage = $uploadFormSizeErrorMessage ?? $this->uploadFormSizeErrorMessage;
  97. $this->uploadPartialErrorMessage = $uploadPartialErrorMessage ?? $this->uploadPartialErrorMessage;
  98. $this->uploadNoFileErrorMessage = $uploadNoFileErrorMessage ?? $this->uploadNoFileErrorMessage;
  99. $this->uploadNoTmpDirErrorMessage = $uploadNoTmpDirErrorMessage ?? $this->uploadNoTmpDirErrorMessage;
  100. $this->uploadCantWriteErrorMessage = $uploadCantWriteErrorMessage ?? $this->uploadCantWriteErrorMessage;
  101. $this->uploadExtensionErrorMessage = $uploadExtensionErrorMessage ?? $this->uploadExtensionErrorMessage;
  102. $this->uploadErrorMessage = $uploadErrorMessage ?? $this->uploadErrorMessage;
  103. if (null !== $this->maxSize) {
  104. $this->normalizeBinaryFormat($this->maxSize);
  105. }
  106. }
  107. public function __set(string $option, $value)
  108. {
  109. if ('maxSize' === $option) {
  110. $this->normalizeBinaryFormat($value);
  111. return;
  112. }
  113. parent::__set($option, $value);
  114. }
  115. public function __get(string $option)
  116. {
  117. if ('maxSize' === $option) {
  118. return $this->maxSize;
  119. }
  120. return parent::__get($option);
  121. }
  122. public function __isset(string $option)
  123. {
  124. if ('maxSize' === $option) {
  125. return true;
  126. }
  127. return parent::__isset($option);
  128. }
  129. /**
  130. * @param int|string $maxSize
  131. */
  132. private function normalizeBinaryFormat($maxSize)
  133. {
  134. $factors = [
  135. 'k' => 1000,
  136. 'ki' => 1 << 10,
  137. 'm' => 1000 * 1000,
  138. 'mi' => 1 << 20,
  139. 'g' => 1000 * 1000 * 1000,
  140. 'gi' => 1 << 30,
  141. ];
  142. if (ctype_digit((string) $maxSize)) {
  143. $this->maxSize = (int) $maxSize;
  144. $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
  145. } elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
  146. $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
  147. $this->binaryFormat = null === $this->binaryFormat ? 2 === \strlen($unit) : $this->binaryFormat;
  148. } else {
  149. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size.', $this->maxSize));
  150. }
  151. }
  152. }