PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.18
VikAppointments Services Booking Calendar v1.2.18
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / libraries / adapter / html / classes / number.php
vikappointments / libraries / adapter / html / classes Last commit date
access.php 6 months ago behavior.php 6 months ago bootstrap.php 6 months ago contentlanguage.php 6 months ago date.php 6 months ago form.php 6 months ago formbehavior.php 6 months ago grid.php 6 months ago jquery.php 6 months ago list.php 6 months ago number.php 6 months ago select.php 6 months ago user.php 6 months ago
number.php
113 lines
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.html
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * HTML helper class for rendering numbers.
16 *
17 * @since 10.1.17
18 */
19 abstract class JHtmlNumber
20 {
21 /**
22 * Converts bytes to more distinguishable formats such as:
23 * kilobytes, megabytes, etc.
24 *
25 * By default, the proper format will automatically be chosen.
26 * However, one of the allowed unit types (viz. 'b', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB') may also be used instead.
27 * IEC standard unit types ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB') can be used as well.
28 *
29 * @param string $bytes The number of bytes. Can be either numeric or suffixed format: 32M, 60K, 12G or 812b.
30 * @param string $unit The type of unit to return, a few special values are:
31 * - blank string for no unit;
32 * - 'auto' to choose automatically (default);
33 * - 'binary' to choose automatically but use binary unit prefix.
34 * @param integer $precision The number of digits to be used after the decimal place.
35 * @param bool $iec Whether to be aware of IEC standards. IEC prefixes are always acceptable in input.
36 * When IEC is ON: KiB = 1024 B, KB = 1000 B
37 * When IEC is OFF: KiB = 1024 B, KB = 1024 B
38 *
39 * @return string The number of bytes in the proper units.
40 *
41 * @link https://en.wikipedia.org/wiki/Binary_prefix
42 */
43 public static function bytes($bytes, $unit = 'auto', $precision = 2, $iec = false)
44 {
45 /*
46 * 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.
47 * i.e. – Any number in decimal digits or in sci. notation, optional space, optional 1-3 letter unit suffix
48 */
49 if (is_numeric($bytes))
50 {
51 $oBytes = $bytes;
52 }
53 else
54 {
55 preg_match('/(.*?)\s?((?:[KMGTPEZY]i?)?B?)$/i', trim($bytes), $matches);
56 list(, $oBytes, $oUnit) = $matches;
57
58 if ($oUnit && is_numeric($oBytes))
59 {
60 $oBase = $iec && strpos($oUnit, 'i') === false ? 1000 : 1024;
61 $factor = pow($oBase, stripos('BKMGTPEZY', $oUnit[0]));
62 $oBytes *= $factor;
63 }
64 }
65
66 if (empty($oBytes) || !is_numeric($oBytes))
67 {
68 return 0;
69 }
70
71 $oBytes = round($oBytes);
72
73 // if no unit is requested return early
74 if ($unit === '')
75 {
76 return (string) $oBytes;
77 }
78
79 $stdSuffixes = array('b', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
80 $iecSuffixes = array('b', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
81
82 // user supplied method
83 if (in_array($unit, $iecSuffixes))
84 {
85 $base = 1024;
86 $i = array_search($unit, $iecSuffixes, true);
87 $suffix = $unit;
88 }
89 else if (in_array($unit, $stdSuffixes))
90 {
91 $base = $iec ? 1000 : 1024;
92 $i = array_search($unit, $stdSuffixes, true);
93 $suffix = $unit;
94 }
95 else if ($unit === 'binary')
96 {
97 $base = 1024;
98 $i = (int) floor(log($oBytes, $base));
99 $suffix = $iecSuffixes[$i];
100 }
101 else
102 {
103 // default method
104 $base = $iec ? 1000 : 1024;
105 $i = (int) floor(log($oBytes, $base));
106 $suffix = $stdSuffixes[$i];
107 }
108
109 // format number using current locale
110 return number_format_i18n(round($oBytes / pow($base, $i), (int) $precision), (int) $precision) . ' ' . $suffix;
111 }
112 }
113