Example #1
0
 /**
  * Format a quantity of bytes for human consumption.
  *
  * @param int $bytes
  *
  * @return string
  */
 public function format($bytes)
 {
     $bytes = Numeric::ensure($bytes);
     if (is_float($bytes) && $bytes > 0.0 && $bytes < 1.0) {
         throw new \InvalidArgumentException('Floats smaller than one can not be formatted.');
     }
     // 0 bytes won't work with log(), so set defaults for this case
     $exponent = 0;
     $normBytes = 0;
     if ($bytes !== 0) {
         $exponent = log(abs($bytes), $this->base);
         $normBytes = pow($this->base, $exponent - floor($exponent));
         // Make bytes negative again if needed
         $normBytes *= $bytes >= 0 ? 1 : -1;
     }
     $suffix = self::$suffixes[$this->suffix][$exponent];
     $number = number_format($normBytes, $this->precision, '.', '');
     $number = $this->trimPrecision($number);
     $suffix = Quantity::format($suffix, $number);
     return sprintf('%s %s', $number, $suffix);
 }
Example #2
0
 /**
  * @dataProvider formatProvider
  *
  * @param string    $expected
  * @param string    $format
  * @param int|float $quantity
  */
 public function testFormat($expected, $format, $quantity)
 {
     $this->assertSame($expected, Quantity::format($format, $quantity));
 }
Example #3
0
 public function getPrettyOperations()
 {
     return $this->formatNumber($this->operations, 0) . Quantity::format(' op(s)', $this->operations);
 }
Example #4
0
 /**
  * @param int|\DateInterval $duration
  * @param array             $steps
  *
  * @return string
  */
 private function formatPeriods($duration, $steps)
 {
     $seconds = $this->ensureSeconds($duration);
     $parts = [];
     foreach ($steps as $minValue => $suffix) {
         if ($seconds >= $minValue) {
             $stepValue = $seconds / $minValue;
             if ($minValue < 1) {
                 $stepValue = round($stepValue);
             } else {
                 $stepValue = floor($stepValue);
             }
             if ($stepValue > 0) {
                 $suffix = Quantity::format($suffix, $stepValue);
                 $parts[] = $stepValue . $suffix;
                 $seconds -= $stepValue * $minValue;
             }
         }
     }
     if (count($parts) === 0) {
         $parts[] = $seconds . Quantity::format($steps[self::SECOND], $seconds);
     }
     if ($this->limit > 0) {
         $parts = array_slice($parts, 0, $this->limit);
     }
     return implode(' ', $parts);
 }
Example #5
0
 private function showSummary($microseconds)
 {
     $statusFileMap = $this->getFilesGroupedByStatus();
     $changedCount = 0;
     if (isset($statusFileMap[FormatJobFile::STATUS_CHANGED])) {
         $changedCount = count($statusFileMap[FormatJobFile::STATUS_CHANGED]);
     }
     $duration = Duration::create()->format((int) $microseconds) . ' ';
     if ($microseconds < 10) {
         if ($microseconds < 1) {
             $duration = '';
         }
         $duration .= round(($microseconds - (int) $microseconds) * 1000) . 'ms';
     }
     $this->stdio->success(sprintf('Formatted %d file%s in %s.', $changedCount, Quantity::format('(s)', $changedCount), $duration));
 }