vendor/symfony/validator/Constraints/File.php line 26

Open in your IDE?
  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. public const INVALID_EXTENSION_ERROR = 'c8c7315c-6186-4719-8b71-5659e16bdcb7';
  31. public const FILENAME_TOO_LONG = 'e5706483-91a8-49d8-9a59-5e81a3c634a8';
  32. protected const ERROR_NAMES = [
  33. self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
  34. self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
  35. self::EMPTY_ERROR => 'EMPTY_ERROR',
  36. self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
  37. self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
  38. self::INVALID_EXTENSION_ERROR => 'INVALID_EXTENSION_ERROR',
  39. self::FILENAME_TOO_LONG => 'FILENAME_TOO_LONG',
  40. ];
  41. /**
  42. * @deprecated since Symfony 6.1, use const ERROR_NAMES instead
  43. */
  44. protected static $errorNames = self::ERROR_NAMES;
  45. public $binaryFormat;
  46. public $mimeTypes = [];
  47. public ?int $filenameMaxLength = null;
  48. public array|string|null $extensions = [];
  49. public $notFoundMessage = 'The file could not be found.';
  50. public $notReadableMessage = 'The file is not readable.';
  51. public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
  52. public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
  53. public string $extensionsMessage = 'The extension of the file is invalid ({{ extension }}). Allowed extensions are {{ extensions }}.';
  54. public $disallowEmptyMessage = 'An empty file is not allowed.';
  55. public $filenameTooLongMessage = 'The filename is too long. It should have {{ filename_max_length }} character or less.|The filename is too long. It should have {{ filename_max_length }} characters or less.';
  56. public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
  57. public $uploadFormSizeErrorMessage = 'The file is too large.';
  58. public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
  59. public $uploadNoFileErrorMessage = 'No file was uploaded.';
  60. public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
  61. public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
  62. public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
  63. public $uploadErrorMessage = 'The file could not be uploaded.';
  64. protected $maxSize;
  65. /**
  66. * @param array<string|string[]>|string $extensions
  67. */
  68. public function __construct(
  69. ?array $options = null,
  70. int|string|null $maxSize = null,
  71. ?bool $binaryFormat = null,
  72. array|string|null $mimeTypes = null,
  73. ?int $filenameMaxLength = null,
  74. ?string $notFoundMessage = null,
  75. ?string $notReadableMessage = null,
  76. ?string $maxSizeMessage = null,
  77. ?string $mimeTypesMessage = null,
  78. ?string $disallowEmptyMessage = null,
  79. ?string $filenameTooLongMessage = null,
  80. ?string $uploadIniSizeErrorMessage = null,
  81. ?string $uploadFormSizeErrorMessage = null,
  82. ?string $uploadPartialErrorMessage = null,
  83. ?string $uploadNoFileErrorMessage = null,
  84. ?string $uploadNoTmpDirErrorMessage = null,
  85. ?string $uploadCantWriteErrorMessage = null,
  86. ?string $uploadExtensionErrorMessage = null,
  87. ?string $uploadErrorMessage = null,
  88. ?array $groups = null,
  89. mixed $payload = null,
  90. array|string|null $extensions = null,
  91. ?string $extensionsMessage = null,
  92. ) {
  93. parent::__construct($options, $groups, $payload);
  94. $this->maxSize = $maxSize ?? $this->maxSize;
  95. $this->binaryFormat = $binaryFormat ?? $this->binaryFormat;
  96. $this->mimeTypes = $mimeTypes ?? $this->mimeTypes;
  97. $this->filenameMaxLength = $filenameMaxLength ?? $this->filenameMaxLength;
  98. $this->extensions = $extensions ?? $this->extensions;
  99. $this->notFoundMessage = $notFoundMessage ?? $this->notFoundMessage;
  100. $this->notReadableMessage = $notReadableMessage ?? $this->notReadableMessage;
  101. $this->maxSizeMessage = $maxSizeMessage ?? $this->maxSizeMessage;
  102. $this->mimeTypesMessage = $mimeTypesMessage ?? $this->mimeTypesMessage;
  103. $this->extensionsMessage = $extensionsMessage ?? $this->extensionsMessage;
  104. $this->disallowEmptyMessage = $disallowEmptyMessage ?? $this->disallowEmptyMessage;
  105. $this->filenameTooLongMessage = $filenameTooLongMessage ?? $this->filenameTooLongMessage;
  106. $this->uploadIniSizeErrorMessage = $uploadIniSizeErrorMessage ?? $this->uploadIniSizeErrorMessage;
  107. $this->uploadFormSizeErrorMessage = $uploadFormSizeErrorMessage ?? $this->uploadFormSizeErrorMessage;
  108. $this->uploadPartialErrorMessage = $uploadPartialErrorMessage ?? $this->uploadPartialErrorMessage;
  109. $this->uploadNoFileErrorMessage = $uploadNoFileErrorMessage ?? $this->uploadNoFileErrorMessage;
  110. $this->uploadNoTmpDirErrorMessage = $uploadNoTmpDirErrorMessage ?? $this->uploadNoTmpDirErrorMessage;
  111. $this->uploadCantWriteErrorMessage = $uploadCantWriteErrorMessage ?? $this->uploadCantWriteErrorMessage;
  112. $this->uploadExtensionErrorMessage = $uploadExtensionErrorMessage ?? $this->uploadExtensionErrorMessage;
  113. $this->uploadErrorMessage = $uploadErrorMessage ?? $this->uploadErrorMessage;
  114. if (null !== $this->maxSize) {
  115. $this->normalizeBinaryFormat($this->maxSize);
  116. }
  117. }
  118. /**
  119. * @return void
  120. */
  121. public function __set(string $option, mixed $value)
  122. {
  123. if ('maxSize' === $option) {
  124. $this->normalizeBinaryFormat($value);
  125. return;
  126. }
  127. parent::__set($option, $value);
  128. }
  129. public function __get(string $option): mixed
  130. {
  131. if ('maxSize' === $option) {
  132. return $this->maxSize;
  133. }
  134. return parent::__get($option);
  135. }
  136. public function __isset(string $option): bool
  137. {
  138. if ('maxSize' === $option) {
  139. return true;
  140. }
  141. return parent::__isset($option);
  142. }
  143. private function normalizeBinaryFormat(int|string $maxSize): void
  144. {
  145. $factors = [
  146. 'k' => 1000,
  147. 'ki' => 1 << 10,
  148. 'm' => 1000 * 1000,
  149. 'mi' => 1 << 20,
  150. 'g' => 1000 * 1000 * 1000,
  151. 'gi' => 1 << 30,
  152. ];
  153. if (ctype_digit((string) $maxSize)) {
  154. $this->maxSize = (int) $maxSize;
  155. $this->binaryFormat ??= false;
  156. } elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
  157. $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
  158. $this->binaryFormat ??= 2 === \strlen($unit);
  159. } else {
  160. throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum size.', $maxSize));
  161. }
  162. }
  163. }