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@0
|
21 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class File extends Constraint
|
Chris@0
|
24 {
|
Chris@0
|
25 // Check the Image constraint for clashes if adding new constants here
|
Chris@0
|
26
|
Chris@0
|
27 const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
|
Chris@0
|
28 const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
|
Chris@0
|
29 const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
|
Chris@0
|
30 const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
|
Chris@0
|
31 const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
|
Chris@0
|
32
|
Chris@0
|
33 protected static $errorNames = array(
|
Chris@0
|
34 self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
|
Chris@0
|
35 self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
|
Chris@0
|
36 self::EMPTY_ERROR => 'EMPTY_ERROR',
|
Chris@0
|
37 self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
|
Chris@0
|
38 self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
|
Chris@0
|
39 );
|
Chris@0
|
40
|
Chris@0
|
41 public $binaryFormat;
|
Chris@0
|
42 public $mimeTypes = array();
|
Chris@0
|
43 public $notFoundMessage = 'The file could not be found.';
|
Chris@0
|
44 public $notReadableMessage = 'The file is not readable.';
|
Chris@0
|
45 public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
|
Chris@0
|
46 public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
|
Chris@0
|
47 public $disallowEmptyMessage = 'An empty file is not allowed.';
|
Chris@0
|
48
|
Chris@0
|
49 public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
|
Chris@0
|
50 public $uploadFormSizeErrorMessage = 'The file is too large.';
|
Chris@0
|
51 public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
|
Chris@0
|
52 public $uploadNoFileErrorMessage = 'No file was uploaded.';
|
Chris@0
|
53 public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
|
Chris@0
|
54 public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
|
Chris@0
|
55 public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
|
Chris@0
|
56 public $uploadErrorMessage = 'The file could not be uploaded.';
|
Chris@0
|
57
|
Chris@0
|
58 protected $maxSize;
|
Chris@0
|
59
|
Chris@0
|
60 public function __construct($options = null)
|
Chris@0
|
61 {
|
Chris@0
|
62 parent::__construct($options);
|
Chris@0
|
63
|
Chris@0
|
64 if (null !== $this->maxSize) {
|
Chris@0
|
65 $this->normalizeBinaryFormat($this->maxSize);
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 public function __set($option, $value)
|
Chris@0
|
70 {
|
Chris@0
|
71 if ('maxSize' === $option) {
|
Chris@0
|
72 $this->normalizeBinaryFormat($value);
|
Chris@0
|
73
|
Chris@0
|
74 return;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 parent::__set($option, $value);
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 public function __get($option)
|
Chris@0
|
81 {
|
Chris@0
|
82 if ('maxSize' === $option) {
|
Chris@0
|
83 return $this->maxSize;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 return parent::__get($option);
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 private function normalizeBinaryFormat($maxSize)
|
Chris@0
|
90 {
|
Chris@0
|
91 $sizeInt = (int) $maxSize;
|
Chris@0
|
92
|
Chris@0
|
93 if (ctype_digit((string) $maxSize)) {
|
Chris@0
|
94 $this->maxSize = $sizeInt;
|
Chris@0
|
95 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
|
Chris@0
|
96 } elseif (preg_match('/^\d++k$/i', $maxSize)) {
|
Chris@0
|
97 $this->maxSize = $sizeInt * 1000;
|
Chris@0
|
98 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
|
Chris@0
|
99 } elseif (preg_match('/^\d++M$/i', $maxSize)) {
|
Chris@0
|
100 $this->maxSize = $sizeInt * 1000000;
|
Chris@0
|
101 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
|
Chris@0
|
102 } elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
|
Chris@0
|
103 $this->maxSize = $sizeInt << 10;
|
Chris@0
|
104 $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
|
Chris@0
|
105 } elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
|
Chris@0
|
106 $this->maxSize = $sizeInt << 20;
|
Chris@0
|
107 $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
|
Chris@0
|
108 } else {
|
Chris@0
|
109 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
|
Chris@0
|
110 }
|
Chris@0
|
111 }
|
Chris@0
|
112 }
|