You are here

private function FileValidator::factorizeSizes in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/FileValidator.php \Symfony\Component\Validator\Constraints\FileValidator::factorizeSizes()

Convert the limit to the smallest possible number (i.e. try "MB", then "kB", then "bytes").

1 call to FileValidator::factorizeSizes()
FileValidator::validate in vendor/symfony/validator/Constraints/FileValidator.php
Checks if the passed value is valid.

File

vendor/symfony/validator/Constraints/FileValidator.php, line 297

Class

FileValidator
@author Bernhard Schussek <bschussek@gmail.com>

Namespace

Symfony\Component\Validator\Constraints

Code

private function factorizeSizes($size, $limit, $binaryFormat) {
  if ($binaryFormat) {
    $coef = self::MIB_BYTES;
    $coefFactor = self::KIB_BYTES;
  }
  else {
    $coef = self::MB_BYTES;
    $coefFactor = self::KB_BYTES;
  }
  $limitAsString = (string) ($limit / $coef);

  // Restrict the limit to 2 decimals (without rounding! we
  // need the precise value)
  while (self::moreDecimalsThan($limitAsString, 2)) {
    $coef /= $coefFactor;
    $limitAsString = (string) ($limit / $coef);
  }

  // Convert size to the same measure, but round to 2 decimals
  $sizeAsString = (string) round($size / $coef, 2);

  // If the size and limit produce the same string output
  // (due to rounding), reduce the coefficient
  while ($sizeAsString === $limitAsString) {
    $coef /= $coefFactor;
    $limitAsString = (string) ($limit / $coef);
    $sizeAsString = (string) round($size / $coef, 2);
  }
  return array(
    $sizeAsString,
    $limitAsString,
    self::$suffices[$coef],
  );
}