Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Constraints; Chris@0: Chris@0: use Symfony\Component\Validator\Constraint; Chris@0: use Symfony\Component\Validator\Exception\ConstraintDefinitionException; Chris@0: Chris@0: /** Chris@0: * @Annotation Chris@0: * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) Chris@0: * Chris@14: * @property int $maxSize Chris@14: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class File extends Constraint Chris@0: { Chris@0: // Check the Image constraint for clashes if adding new constants here Chris@0: Chris@0: const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998'; Chris@0: const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff'; Chris@0: const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137'; Chris@0: const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654'; Chris@0: const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534'; Chris@0: Chris@17: protected static $errorNames = [ Chris@0: self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR', Chris@0: self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR', Chris@0: self::EMPTY_ERROR => 'EMPTY_ERROR', Chris@0: self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR', Chris@0: self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR', Chris@17: ]; Chris@0: Chris@0: public $binaryFormat; Chris@17: public $mimeTypes = []; Chris@0: public $notFoundMessage = 'The file could not be found.'; Chris@0: public $notReadableMessage = 'The file is not readable.'; Chris@0: public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.'; Chris@0: public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.'; Chris@0: public $disallowEmptyMessage = 'An empty file is not allowed.'; Chris@0: Chris@0: public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.'; Chris@0: public $uploadFormSizeErrorMessage = 'The file is too large.'; Chris@0: public $uploadPartialErrorMessage = 'The file was only partially uploaded.'; Chris@0: public $uploadNoFileErrorMessage = 'No file was uploaded.'; Chris@0: public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.'; Chris@0: public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.'; Chris@0: public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.'; Chris@0: public $uploadErrorMessage = 'The file could not be uploaded.'; Chris@0: Chris@0: protected $maxSize; Chris@0: Chris@0: public function __construct($options = null) Chris@0: { Chris@0: parent::__construct($options); Chris@0: Chris@0: if (null !== $this->maxSize) { Chris@0: $this->normalizeBinaryFormat($this->maxSize); Chris@0: } Chris@0: } Chris@0: Chris@0: public function __set($option, $value) Chris@0: { Chris@0: if ('maxSize' === $option) { Chris@0: $this->normalizeBinaryFormat($value); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: parent::__set($option, $value); Chris@0: } Chris@0: Chris@0: public function __get($option) Chris@0: { Chris@0: if ('maxSize' === $option) { Chris@0: return $this->maxSize; Chris@0: } Chris@0: Chris@0: return parent::__get($option); Chris@0: } Chris@0: Chris@14: public function __isset($option) Chris@14: { Chris@14: if ('maxSize' === $option) { Chris@14: return true; Chris@14: } Chris@14: Chris@14: return parent::__isset($option); Chris@14: } Chris@14: Chris@0: private function normalizeBinaryFormat($maxSize) Chris@0: { Chris@17: $factors = [ Chris@14: 'k' => 1000, Chris@14: 'ki' => 1 << 10, Chris@14: 'm' => 1000000, Chris@14: 'mi' => 1 << 20, Chris@17: ]; Chris@0: if (ctype_digit((string) $maxSize)) { Chris@14: $this->maxSize = (int) $maxSize; Chris@0: $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat; Chris@14: } elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) { Chris@14: $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])]; Chris@17: $this->binaryFormat = null === $this->binaryFormat ? 2 === \strlen($unit) : $this->binaryFormat; Chris@0: } else { Chris@0: throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize)); Chris@0: } Chris@0: } Chris@0: }