[Validator] smaller fixes for binary format

This commit is contained in:
Tobias Schultze 2014-06-17 11:58:55 +02:00
parent 3b4afe78df
commit 967576aaa9
2 changed files with 8 additions and 8 deletions

View File

@ -49,19 +49,19 @@ class File extends Constraint
if ($this->maxSize) { if ($this->maxSize) {
if (ctype_digit((string) $this->maxSize)) { if (ctype_digit((string) $this->maxSize)) {
$this->maxSize = (int) $this->maxSize; $this->maxSize = (int) $this->maxSize;
$this->binaryFormat = $this->binaryFormat === null ? false : $this->binaryFormat; $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++k$/i', $this->maxSize)) { } elseif (preg_match('/^\d++k$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000; $this->maxSize = $this->maxSize * 1000;
$this->binaryFormat = $this->binaryFormat === null ? false : $this->binaryFormat; $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++M$/i', $this->maxSize)) { } elseif (preg_match('/^\d++M$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000000; $this->maxSize = $this->maxSize * 1000000;
$this->binaryFormat = $this->binaryFormat === null ? false : $this->binaryFormat; $this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++ki$/i', $this->maxSize)) { } elseif (preg_match('/^\d++Ki$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 10; $this->maxSize = $this->maxSize << 10;
$this->binaryFormat = $this->binaryFormat === null ? true : $this->binaryFormat; $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) { } elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 20; $this->maxSize = $this->maxSize << 20;
$this->binaryFormat = $this->binaryFormat === null ? true : $this->binaryFormat; $this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} else { } else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize)); throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
} }

View File

@ -54,7 +54,7 @@ class FileValidator extends ConstraintValidator
switch ($value->getError()) { switch ($value->getError()) {
case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_INI_SIZE:
if ($constraint->maxSize) { if ($constraint->maxSize) {
$limitInBytes = min(UploadedFile::getMaxFilesize(), (int) $constraint->maxSize); $limitInBytes = min(UploadedFile::getMaxFilesize(), $constraint->maxSize);
} else { } else {
$limitInBytes = UploadedFile::getMaxFilesize(); $limitInBytes = UploadedFile::getMaxFilesize();
} }
@ -118,7 +118,7 @@ class FileValidator extends ConstraintValidator
if (0 === $sizeInBytes) { if (0 === $sizeInBytes) {
$this->context->addViolation($constraint->disallowEmptyMessage); $this->context->addViolation($constraint->disallowEmptyMessage);
} elseif ($constraint->maxSize) { } elseif ($constraint->maxSize) {
$limitInBytes = (int) $constraint->maxSize; $limitInBytes = $constraint->maxSize;
if ($sizeInBytes > $limitInBytes) { if ($sizeInBytes > $limitInBytes) {
// Convert the limit to the smallest possible number // Convert the limit to the smallest possible number