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.     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.     public function __construct(
  54.         array $options = null,
  55.         int|string $maxSize = null,
  56.         bool $binaryFormat = null,
  57.         array|string $mimeTypes = null,
  58.         string $notFoundMessage = null,
  59.         string $notReadableMessage = null,
  60.         string $maxSizeMessage = null,
  61.         string $mimeTypesMessage = null,
  62.         string $disallowEmptyMessage = null,
  63.         string $uploadIniSizeErrorMessage = null,
  64.         string $uploadFormSizeErrorMessage = null,
  65.         string $uploadPartialErrorMessage = null,
  66.         string $uploadNoFileErrorMessage = null,
  67.         string $uploadNoTmpDirErrorMessage = null,
  68.         string $uploadCantWriteErrorMessage = null,
  69.         string $uploadExtensionErrorMessage = null,
  70.         string $uploadErrorMessage = null,
  71.         array $groups = null,
  72.         mixed $payload = null
  73.     ) {
  74.         parent::__construct($options, $groups, $payload);
  75.         $this->maxSize = $maxSize ?? $this->maxSize;
  76.         $this->binaryFormat = $binaryFormat ?? $this->binaryFormat;
  77.         $this->mimeTypes = $mimeTypes ?? $this->mimeTypes;
  78.         $this->notFoundMessage = $notFoundMessage ?? $this->notFoundMessage;
  79.         $this->notReadableMessage = $notReadableMessage ?? $this->notReadableMessage;
  80.         $this->maxSizeMessage = $maxSizeMessage ?? $this->maxSizeMessage;
  81.         $this->mimeTypesMessage = $mimeTypesMessage ?? $this->mimeTypesMessage;
  82.         $this->disallowEmptyMessage = $disallowEmptyMessage ?? $this->disallowEmptyMessage;
  83.         $this->uploadIniSizeErrorMessage = $uploadIniSizeErrorMessage ?? $this->uploadIniSizeErrorMessage;
  84.         $this->uploadFormSizeErrorMessage = $uploadFormSizeErrorMessage ?? $this->uploadFormSizeErrorMessage;
  85.         $this->uploadPartialErrorMessage = $uploadPartialErrorMessage ?? $this->uploadPartialErrorMessage;
  86.         $this->uploadNoFileErrorMessage = $uploadNoFileErrorMessage ?? $this->uploadNoFileErrorMessage;
  87.         $this->uploadNoTmpDirErrorMessage = $uploadNoTmpDirErrorMessage ?? $this->uploadNoTmpDirErrorMessage;
  88.         $this->uploadCantWriteErrorMessage = $uploadCantWriteErrorMessage ?? $this->uploadCantWriteErrorMessage;
  89.         $this->uploadExtensionErrorMessage = $uploadExtensionErrorMessage ?? $this->uploadExtensionErrorMessage;
  90.         $this->uploadErrorMessage = $uploadErrorMessage ?? $this->uploadErrorMessage;
  91.         if (null !== $this->maxSize) {
  92.             $this->normalizeBinaryFormat($this->maxSize);
  93.         }
  94.     }
  95.     public function __set(string $option, mixed $value)
  96.     {
  97.         if ('maxSize' === $option) {
  98.             $this->normalizeBinaryFormat($value);
  99.             return;
  100.         }
  101.         parent::__set($option, $value);
  102.     }
  103.     public function __get(string $option): mixed
  104.     {
  105.         if ('maxSize' === $option) {
  106.             return $this->maxSize;
  107.         }
  108.         return parent::__get($option);
  109.     }
  110.     public function __isset(string $option): bool
  111.     {
  112.         if ('maxSize' === $option) {
  113.             return true;
  114.         }
  115.         return parent::__isset($option);
  116.     }
  117.     private function normalizeBinaryFormat(int|string $maxSize)
  118.     {
  119.         $factors = [
  120.             'k' => 1000,
  121.             'ki' => 1 << 10,
  122.             'm' => 1000 * 1000,
  123.             'mi' => 1 << 20,
  124.             'g' => 1000 * 1000 * 1000,
  125.             'gi' => 1 << 30,
  126.         ];
  127.         if (ctype_digit((string) $maxSize)) {
  128.             $this->maxSize = (int) $maxSize;
  129.             $this->binaryFormat = $this->binaryFormat ?? false;
  130.         } elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
  131.             $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
  132.             $this->binaryFormat = $this->binaryFormat ?? (2 === \strlen($unit));
  133.         } else {
  134.             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size.', $maxSize));
  135.         }
  136.     }
  137. }