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

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