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

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. class File extends Constraint
  22. {
  23.     // Check the Image constraint for clashes if adding new constants here
  24.     public const NOT_FOUND_ERROR 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
  25.     public const NOT_READABLE_ERROR 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
  26.     public const EMPTY_ERROR '5d743385-9775-4aa5-8ff5-495fb1e60137';
  27.     public const TOO_LARGE_ERROR 'df8637af-d466-48c6-a59d-e7126250a654';
  28.     public const INVALID_MIME_TYPE_ERROR '744f00bc-4389-4c74-92de-9a43cde55534';
  29.     protected static $errorNames = [
  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 = [];
  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.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function __construct($options null)
  56.     {
  57.         parent::__construct($options);
  58.         if (null !== $this->maxSize) {
  59.             $this->normalizeBinaryFormat($this->maxSize);
  60.         }
  61.     }
  62.     public function __set($option$value)
  63.     {
  64.         if ('maxSize' === $option) {
  65.             $this->normalizeBinaryFormat($value);
  66.             return;
  67.         }
  68.         parent::__set($option$value);
  69.     }
  70.     public function __get($option)
  71.     {
  72.         if ('maxSize' === $option) {
  73.             return $this->maxSize;
  74.         }
  75.         return parent::__get($option);
  76.     }
  77.     public function __isset($option)
  78.     {
  79.         if ('maxSize' === $option) {
  80.             return true;
  81.         }
  82.         return parent::__isset($option);
  83.     }
  84.     private function normalizeBinaryFormat($maxSize)
  85.     {
  86.         $factors = [
  87.             'k' => 1000,
  88.             'ki' => << 10,
  89.             'm' => 1000 1000,
  90.             'mi' => << 20,
  91.             'g' => 1000 1000 1000,
  92.             'gi' => << 30,
  93.         ];
  94.         if (ctype_digit((string) $maxSize)) {
  95.             $this->maxSize = (int) $maxSize;
  96.             $this->binaryFormat $this->binaryFormat ?? false;
  97.         } elseif (preg_match('/^(\d++)('.implode('|'array_keys($factors)).')$/i'$maxSize$matches)) {
  98.             $this->maxSize $matches[1] * $factors[$unit strtolower($matches[2])];
  99.             $this->binaryFormat $this->binaryFormat ?? (=== \strlen($unit));
  100.         } else {
  101.             throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size.'$this->maxSize));
  102.         }
  103.     }
  104. }