comparison vendor/symfony/validator/Constraints/File.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException; 15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16 16
17 /** 17 /**
18 * @Annotation 18 * @Annotation
19 * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) 19 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
20 *
21 * @property int $maxSize
20 * 22 *
21 * @author Bernhard Schussek <bschussek@gmail.com> 23 * @author Bernhard Schussek <bschussek@gmail.com>
22 */ 24 */
23 class File extends Constraint 25 class File extends Constraint
24 { 26 {
84 } 86 }
85 87
86 return parent::__get($option); 88 return parent::__get($option);
87 } 89 }
88 90
91 public function __isset($option)
92 {
93 if ('maxSize' === $option) {
94 return true;
95 }
96
97 return parent::__isset($option);
98 }
99
89 private function normalizeBinaryFormat($maxSize) 100 private function normalizeBinaryFormat($maxSize)
90 { 101 {
91 $sizeInt = (int) $maxSize; 102 $factors = array(
92 103 'k' => 1000,
104 'ki' => 1 << 10,
105 'm' => 1000000,
106 'mi' => 1 << 20,
107 );
93 if (ctype_digit((string) $maxSize)) { 108 if (ctype_digit((string) $maxSize)) {
94 $this->maxSize = $sizeInt; 109 $this->maxSize = (int) $maxSize;
95 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat; 110 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
96 } elseif (preg_match('/^\d++k$/i', $maxSize)) { 111 } elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
97 $this->maxSize = $sizeInt * 1000; 112 $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
98 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat; 113 $this->binaryFormat = null === $this->binaryFormat ? 2 === strlen($unit) : $this->binaryFormat;
99 } elseif (preg_match('/^\d++M$/i', $maxSize)) {
100 $this->maxSize = $sizeInt * 1000000;
101 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
102 } elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
103 $this->maxSize = $sizeInt << 10;
104 $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
105 } elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
106 $this->maxSize = $sizeInt << 20;
107 $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
108 } else { 114 } else {
109 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize)); 115 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
110 } 116 }
111 } 117 }
112 } 118 }