[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> Number.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\HTML\Helpers;
  11  
  12  use Joomla\CMS\Language\Text;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * HTML helper class for rendering numbers.
  20   *
  21   * @since  1.6
  22   */
  23  abstract class Number
  24  {
  25      /**
  26       * Converts bytes to more distinguishable formats such as:
  27       * kilobytes, megabytes, etc.
  28       *
  29       * By default, the proper format will automatically be chosen.
  30       * However, one of the allowed unit types (viz. 'b', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB') may also be used instead.
  31       * IEC standard unit types ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB') can be used as well.
  32       *
  33       * @param   string   $bytes      The number of bytes. Can be either numeric or suffixed format: 32M, 60K, 12G or 812b
  34       * @param   string   $unit       The type of unit to return, few special values are:
  35       *                               Blank string '' for no unit,
  36       *                               'auto' to choose automatically (default)
  37       *                               'binary' to choose automatically but use binary unit prefix
  38       * @param   integer  $precision  The number of digits to be used after the decimal place.
  39       * @param   bool     $iec        Whether to be aware of IEC standards. IEC prefixes are always acceptable in input.
  40       *                               When IEC is ON:  KiB = 1024 B, KB = 1000 B
  41       *                               When IEC is OFF: KiB = 1024 B, KB = 1024 B
  42       *
  43       * @return  string   The number of bytes in the proper units.
  44       *
  45       * @since   1.6
  46       * @link    https://en.wikipedia.org/wiki/Binary_prefix
  47       */
  48      public static function bytes($bytes, $unit = 'auto', $precision = 2, $iec = false)
  49      {
  50          /*
  51           * Allowed 123.45, 123.45 M, 123.45 Mi, 123.45 MB, 123.45 MiB, 1.2345E+12MB, 1.2345E+12 MB , 1.2345E+12 MiB etc.
  52           * Meaning any number in decimal digits or in sci. notation, optional space, optional 1-3 letter unit suffix
  53           */
  54          if (is_numeric($bytes)) {
  55              $oBytes = $bytes;
  56          } else {
  57              preg_match('/(.*?)\s?((?:[KMGTPEZY]i?)?B?)$/i', trim($bytes), $matches);
  58              list(, $oBytes, $oUnit) = $matches;
  59  
  60              if ($oUnit && is_numeric($oBytes)) {
  61                  $oBase  = $iec && strpos($oUnit, 'i') === false ? 1000 : 1024;
  62                  $factor = pow($oBase, stripos('BKMGTPEZY', $oUnit[0]));
  63                  $oBytes *= $factor;
  64              }
  65          }
  66  
  67          if (empty($oBytes) || !is_numeric($oBytes)) {
  68              return 0;
  69          }
  70  
  71          $oBytes = round($oBytes);
  72  
  73          // If no unit is requested return early
  74          if ($unit === '') {
  75              return (string) $oBytes;
  76          }
  77  
  78          $stdSuffixes = array('b', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  79          $iecSuffixes = array('b', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
  80  
  81          // User supplied method
  82          if (in_array($unit, $iecSuffixes)) {
  83              $base   = 1024;
  84              $i      = array_search($unit, $iecSuffixes, true);
  85              $suffix = $unit;
  86          } elseif (in_array($unit, $stdSuffixes)) {
  87              $base   = $iec ? 1000 : 1024;
  88              $i      = array_search($unit, $stdSuffixes, true);
  89              $suffix = $unit;
  90          } elseif ($unit === 'binary') {
  91              $base   = 1024;
  92              $i      = (int) floor(log($oBytes, $base));
  93              $suffix = $iecSuffixes[$i];
  94          } else {
  95              // Default method
  96              $base   = $iec ? 1000 : 1024;
  97              $i      = (int) floor(log($oBytes, $base));
  98              $suffix = $stdSuffixes[$i];
  99          }
 100  
 101          return number_format(
 102              round($oBytes / pow($base, $i), (int) $precision),
 103              (int) $precision,
 104              Text::_('DECIMALS_SEPARATOR'),
 105              Text::_('THOUSANDS_SEPARATOR')
 106          ) . ' ' . $suffix;
 107      }
 108  }


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