comparison vendor/symfony/validator/Constraints/File.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Validator\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16
17 /**
18 * @Annotation
19 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
20 *
21 * @property int $maxSize
22 *
23 * @author Bernhard Schussek <bschussek@gmail.com>
24 */
25 class File extends Constraint
26 {
27 // Check the Image constraint for clashes if adding new constants here
28
29 const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
30 const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
31 const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
32 const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
33 const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
34
35 protected static $errorNames = array(
36 self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
37 self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
38 self::EMPTY_ERROR => 'EMPTY_ERROR',
39 self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
40 self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
41 );
42
43 public $binaryFormat;
44 public $mimeTypes = array();
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 $disallowEmptyMessage = 'An empty file is not allowed.';
50
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
60 protected $maxSize;
61
62 public function __construct($options = null)
63 {
64 parent::__construct($options);
65
66 if (null !== $this->maxSize) {
67 $this->normalizeBinaryFormat($this->maxSize);
68 }
69 }
70
71 public function __set($option, $value)
72 {
73 if ('maxSize' === $option) {
74 $this->normalizeBinaryFormat($value);
75
76 return;
77 }
78
79 parent::__set($option, $value);
80 }
81
82 public function __get($option)
83 {
84 if ('maxSize' === $option) {
85 return $this->maxSize;
86 }
87
88 return parent::__get($option);
89 }
90
91 public function __isset($option)
92 {
93 if ('maxSize' === $option) {
94 return true;
95 }
96
97 return parent::__isset($option);
98 }
99
100 private function normalizeBinaryFormat($maxSize)
101 {
102 $factors = array(
103 'k' => 1000,
104 'ki' => 1 << 10,
105 'm' => 1000000,
106 'mi' => 1 << 20,
107 );
108 if (ctype_digit((string) $maxSize)) {
109 $this->maxSize = (int) $maxSize;
110 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
111 } elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
112 $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
113 $this->binaryFormat = null === $this->binaryFormat ? 2 === strlen($unit) : $this->binaryFormat;
114 } else {
115 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
116 }
117 }
118 }