[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/DataFormatter/ -> DataFormatter.php (source)

   1  <?php
   2  /*
   3   * This file is part of the DebugBar package.
   4   *
   5   * (c) 2013 Maxime Bouroumeau-Fuseau
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  namespace DebugBar\DataFormatter;
  12  
  13  use Symfony\Component\VarDumper\Cloner\VarCloner;
  14  use Symfony\Component\VarDumper\Dumper\CliDumper;
  15  
  16  class DataFormatter implements DataFormatterInterface
  17  {
  18      /**
  19       * DataFormatter constructor.
  20       */
  21      public function __construct()
  22      {
  23          $this->cloner = new VarCloner();
  24          $this->dumper = new CliDumper();
  25      }
  26  
  27      /**
  28       * @param $data
  29       * @return string
  30       */
  31      public function formatVar($data)
  32      {
  33          $output = '';
  34  
  35          $this->dumper->dump(
  36              $this->cloner->cloneVar($data),
  37              function ($line, $depth) use (&$output) {
  38                  // A negative depth means "end of dump"
  39                  if ($depth >= 0) {
  40                      // Adds a two spaces indentation to the line
  41                      $output .= str_repeat('  ', $depth).$line."\n";
  42                  }
  43              }
  44          );
  45  
  46          return trim($output);
  47      }
  48  
  49      /**
  50       * @param float $seconds
  51       * @return string
  52       */
  53      public function formatDuration($seconds)
  54      {
  55          if ($seconds < 0.001) {
  56              return round($seconds * 1000000) . 'μs';
  57          } elseif ($seconds < 0.1) {
  58              return round($seconds * 1000, 2) . 'ms';
  59          } elseif ($seconds < 1) {
  60              return round($seconds * 1000) . 'ms';
  61          }
  62          return round($seconds, 2) . 's';
  63      }
  64  
  65      /**
  66       * @param string $size
  67       * @param int $precision
  68       * @return string
  69       */
  70      public function formatBytes($size, $precision = 2)
  71      {
  72          if ($size === 0 || $size === null) {
  73              return "0B";
  74          }
  75  
  76          $sign = $size < 0 ? '-' : '';
  77          $size = abs($size);
  78  
  79          $base = log($size) / log(1024);
  80          $suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
  81          return $sign . round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int) floor($base)];
  82      }
  83  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer