annotate vendor/symfony/validator/Constraints/File.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Validator\Constraints;
Chris@0 13
Chris@0 14 use Symfony\Component\Validator\Constraint;
Chris@0 15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * @Annotation
Chris@0 19 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
Chris@0 20 *
Chris@14 21 * @property int $maxSize
Chris@14 22 *
Chris@0 23 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 24 */
Chris@0 25 class File extends Constraint
Chris@0 26 {
Chris@0 27 // Check the Image constraint for clashes if adding new constants here
Chris@0 28
Chris@0 29 const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
Chris@0 30 const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
Chris@0 31 const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
Chris@0 32 const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
Chris@0 33 const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
Chris@0 34
Chris@17 35 protected static $errorNames = [
Chris@0 36 self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
Chris@0 37 self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
Chris@0 38 self::EMPTY_ERROR => 'EMPTY_ERROR',
Chris@0 39 self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
Chris@0 40 self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
Chris@17 41 ];
Chris@0 42
Chris@0 43 public $binaryFormat;
Chris@17 44 public $mimeTypes = [];
Chris@0 45 public $notFoundMessage = 'The file could not be found.';
Chris@0 46 public $notReadableMessage = 'The file is not readable.';
Chris@0 47 public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
Chris@0 48 public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
Chris@0 49 public $disallowEmptyMessage = 'An empty file is not allowed.';
Chris@0 50
Chris@0 51 public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
Chris@0 52 public $uploadFormSizeErrorMessage = 'The file is too large.';
Chris@0 53 public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
Chris@0 54 public $uploadNoFileErrorMessage = 'No file was uploaded.';
Chris@0 55 public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
Chris@0 56 public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
Chris@0 57 public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
Chris@0 58 public $uploadErrorMessage = 'The file could not be uploaded.';
Chris@0 59
Chris@0 60 protected $maxSize;
Chris@0 61
Chris@0 62 public function __construct($options = null)
Chris@0 63 {
Chris@0 64 parent::__construct($options);
Chris@0 65
Chris@0 66 if (null !== $this->maxSize) {
Chris@0 67 $this->normalizeBinaryFormat($this->maxSize);
Chris@0 68 }
Chris@0 69 }
Chris@0 70
Chris@0 71 public function __set($option, $value)
Chris@0 72 {
Chris@0 73 if ('maxSize' === $option) {
Chris@0 74 $this->normalizeBinaryFormat($value);
Chris@0 75
Chris@0 76 return;
Chris@0 77 }
Chris@0 78
Chris@0 79 parent::__set($option, $value);
Chris@0 80 }
Chris@0 81
Chris@0 82 public function __get($option)
Chris@0 83 {
Chris@0 84 if ('maxSize' === $option) {
Chris@0 85 return $this->maxSize;
Chris@0 86 }
Chris@0 87
Chris@0 88 return parent::__get($option);
Chris@0 89 }
Chris@0 90
Chris@14 91 public function __isset($option)
Chris@14 92 {
Chris@14 93 if ('maxSize' === $option) {
Chris@14 94 return true;
Chris@14 95 }
Chris@14 96
Chris@14 97 return parent::__isset($option);
Chris@14 98 }
Chris@14 99
Chris@0 100 private function normalizeBinaryFormat($maxSize)
Chris@0 101 {
Chris@17 102 $factors = [
Chris@14 103 'k' => 1000,
Chris@14 104 'ki' => 1 << 10,
Chris@14 105 'm' => 1000000,
Chris@14 106 'mi' => 1 << 20,
Chris@17 107 ];
Chris@0 108 if (ctype_digit((string) $maxSize)) {
Chris@14 109 $this->maxSize = (int) $maxSize;
Chris@0 110 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
Chris@14 111 } elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
Chris@14 112 $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
Chris@17 113 $this->binaryFormat = null === $this->binaryFormat ? 2 === \strlen($unit) : $this->binaryFormat;
Chris@0 114 } else {
Chris@0 115 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
Chris@0 116 }
Chris@0 117 }
Chris@0 118 }