Skip to content

Commit

Permalink
minor #21748 Refactor file constraint logic (greg0ire)
Browse files Browse the repository at this point in the history
This PR was submitted for the 2.7 branch but it was merged into the 3.3-dev branch instead (closes #21748).

Discussion
----------

Refactor file constraint logic

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

<!--
- Bug fixes must be submitted against the lowest branch where they apply
  (lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
- Please fill in this template according to the PR you're about to submit.
- Replace this comment by a description of what your PR is solving.
-->

These are pedantic changes done in #21747 , which turned out to be useless.

Commits
-------

b717d7c Refactor file constraint logic
  • Loading branch information
fabpot committed Mar 1, 2017
2 parents e1c28de + b717d7c commit c10f89f
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/Symfony/Component/Validator/Constraints/File.php
Expand Up @@ -88,23 +88,18 @@ public function __get($option)

private function normalizeBinaryFormat($maxSize)
{
$sizeInt = (int) $maxSize;

$factors = array(
'k' => 1000,
'ki' => 1 << 10,
'm' => 1000000,
'mi' => 1 << 20,
);
if (ctype_digit((string) $maxSize)) {
$this->maxSize = $sizeInt;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++k$/i', $maxSize)) {
$this->maxSize = $sizeInt * 1000;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++M$/i', $maxSize)) {
$this->maxSize = $sizeInt * 1000000;
$this->maxSize = (int) $maxSize;
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
$this->maxSize = $sizeInt << 10;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
$this->maxSize = $sizeInt << 20;
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
} elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
$this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
$this->binaryFormat = null === $this->binaryFormat ? 2 === strlen($unit) : $this->binaryFormat;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
Expand Down

0 comments on commit c10f89f

Please sign in to comment.