4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
19 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class File extends Constraint
25 // Check the Image constraint for clashes if adding new constants here
27 const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
28 const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
29 const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
30 const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
31 const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
33 protected static $errorNames = array(
34 self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
35 self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
36 self::EMPTY_ERROR => 'EMPTY_ERROR',
37 self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
38 self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
42 public $mimeTypes = array();
43 public $notFoundMessage = 'The file could not be found.';
44 public $notReadableMessage = 'The file is not readable.';
45 public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
46 public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
47 public $disallowEmptyMessage = 'An empty file is not allowed.';
49 public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
50 public $uploadFormSizeErrorMessage = 'The file is too large.';
51 public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
52 public $uploadNoFileErrorMessage = 'No file was uploaded.';
53 public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
54 public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
55 public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
56 public $uploadErrorMessage = 'The file could not be uploaded.';
60 public function __construct($options = null)
62 parent::__construct($options);
64 if (null !== $this->maxSize) {
65 $this->normalizeBinaryFormat($this->maxSize);
69 public function __set($option, $value)
71 if ('maxSize' === $option) {
72 $this->normalizeBinaryFormat($value);
77 parent::__set($option, $value);
80 public function __get($option)
82 if ('maxSize' === $option) {
83 return $this->maxSize;
86 return parent::__get($option);
89 private function normalizeBinaryFormat($maxSize)
91 $sizeInt = (int) $maxSize;
93 if (ctype_digit((string) $maxSize)) {
94 $this->maxSize = $sizeInt;
95 $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
96 } elseif (preg_match('/^\d++k$/i', $maxSize)) {
97 $this->maxSize = $sizeInt * 1000;
98 $this->binaryFormat = null === $this->binaryFormat ? false : $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;
109 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));