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 * @property int $maxSize
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 class File extends Constraint
27 // Check the Image constraint for clashes if adding new constants here
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';
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',
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.';
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.';
62 public function __construct($options = null)
64 parent::__construct($options);
66 if (null !== $this->maxSize) {
67 $this->normalizeBinaryFormat($this->maxSize);
71 public function __set($option, $value)
73 if ('maxSize' === $option) {
74 $this->normalizeBinaryFormat($value);
79 parent::__set($option, $value);
82 public function __get($option)
84 if ('maxSize' === $option) {
85 return $this->maxSize;
88 return parent::__get($option);
91 public function __isset($option)
93 if ('maxSize' === $option) {
97 return parent::__isset($option);
100 private function normalizeBinaryFormat($maxSize)
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;
115 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));