PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.94
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.94
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Support / Number.php

Number.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.94, at vendor/wpfluent/framework/src/WPFluent/Support/Number.php

502 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Support;
4
5 use RangeException;
6 use TypeError;
7
8 class Number
9 {
10 /**
11 * Format a number depending on the locale
12 *
13 * @param int|float $value
14 * @param integer $dec
15 * @return Formatted number
16 * @see https://developer.wordpress.org/reference/functions/number_format_i18n
17 */
18 public static function format($value, $dec = 0)
19 {
20 return number_format_i18n($value, $dec);
21 }
22
23 /**
24 * Format a number as int
25 *
26 * @param int|float $value
27 * @return int
28 */
29 public static function toInt($val)
30 {
31 return intval($val);
32 }
33
34 /**
35 * Format a number as float
36 *
37 * @param int|float $value
38 * @param integer $dec
39 * @return float
40 */
41 public static function toFloat($val, $dec = 2)
42 {
43 return round((float) $val, $dec);
44 }
45
46 /**
47 * Convert a number to bool
48 *
49 * @param int $val
50 * @return bool
51 */
52 public static function toBool($val)
53 {
54 return boolval(intval($val));
55 }
56
57 /**
58 * Format a number to currency depending on the locale
59 *
60 * @param int|float $value
61 * @param array $before Options fpr formatting
62 * @return Formatted number with currency symbol
63 */
64 public static function toCurrency($value, $options = [])
65 {
66 $defaults = [
67 'locale' => get_locale(),
68 'currency_symbol' => '$',
69 'number_of_decimals' => 2,
70 'space_with_currency' => 0,
71 'currency_position' => 'left',
72 ];
73
74 $args = wp_parse_args($options, $defaults);
75
76 // Switch to the specified locale
77 $originalLocale = get_locale();
78 switch_to_locale($args['locale']);
79
80 // Format the number with the
81 // specified number of decimals
82 $formattedNumber = static::format(
83 $value, $args['number_of_decimals']
84 );
85
86 // Switch back to the original locale
87 switch_to_locale($originalLocale);
88
89 // Prepare the currency symbol and spacing
90 $symbol = $args['currency_symbol'];
91 $space = $args['space_with_currency'] ? ' ' : '';
92
93 // Return the formatted currency string based
94 // on the position of the currency symbol
95 if ($args['currency_position'] === 'left') {
96 return $symbol . $space . $formattedNumber;
97 } else {
98 return $formattedNumber . $space . $symbol;
99 }
100 }
101
102 /**
103 * Notation to numbers.
104 *
105 * This function transforms the php.ini notation
106 * for numbers (like '2M') to an integer.
107 *
108 * @param string $size Size value.
109 * @return int
110 */
111 public static function notationToNum($num)
112 {
113 $l = substr($num, -1);
114 $ret = (int) substr($num, 0, -1);
115
116 switch (strtoupper($l)) {
117 case 'P':
118 $ret *= 1024;
119 // No break.
120 case 'T':
121 $ret *= 1024;
122 // No break.
123 case 'G':
124 $ret *= 1024;
125 // No break.
126 case 'M':
127 $ret *= 1024;
128 // No break.
129 case 'K':
130 $ret *= 1024;
131 break; // Added for clarity
132 }
133
134 return $ret;
135 }
136
137 /**
138 * Calculates the percentage/$percent from the $value/$total
139 *
140 * @param int|float $percent
141 * @param int|float $total
142 * @return int|float
143 */
144 public static function getPercentage($percent, $total)
145 {
146 return ($percent / 100) * $total;
147 }
148
149 /**
150 * Converts a number of bytes to human readable format
151 * using the maximum unit available to convert the bytes.
152 *
153 * @param int|float $bytes
154 * @param integer $decimals
155 * @return Formatted size units of bytes, i.e: 1mb/1gb e.t.c.
156 */
157 public static function formatBytes($bytes, $decimals = 0)
158 {
159 return size_format($bytes, $decimals);
160 }
161
162 /**
163 * Makes an ordinal number from the integer
164 *
165 * @param int $number
166 * @return The ordinal number, i.e: 1st, 5th e.t.c.
167 */
168 public static function toOrdinal($number)
169 {
170 if (!is_numeric($number)) {
171 return $number;
172 }
173
174 if (($number % 100) >= 11 && ($number % 100) <= 13) {
175 $suffix = 'th';
176 } else {
177 switch ($number % 10) {
178 case 1:
179 $suffix = 'st';
180 break;
181 case 2:
182 $suffix = 'nd';
183 break;
184 case 3:
185 $suffix = 'rd';
186 break;
187 default:
188 $suffix = 'th';
189 break;
190 }
191 }
192
193 return $number . $suffix;
194 }
195
196 /**
197 * Convert the number to its human readable equivalent.
198 *
199 * @param int $number
200 * @param int $precision
201 * @param int|null $maxPrecision
202 * @return string
203 */
204 public static function forHumans(
205 $number, $precision = 0, $maxPrecision = null, $abbr = false
206 )
207 {
208 return static::summarize($number, $precision, $maxPrecision, $abbr ? [
209 3 => 'K',
210 6 => 'M',
211 9 => 'B',
212 12 => 'T',
213 15 => 'Q',
214 ] : [
215 3 => ' thousand',
216 6 => ' million',
217 9 => ' billion',
218 12 => ' trillion',
219 15 => ' quadrillion',
220 ]);
221 }
222
223 /**
224 * Convert the number to its human readable equivalent.
225 *
226 * @param int $number
227 * @param int $precision
228 * @param int|null $maxPrecision
229 * @param array $units
230 * @return string
231 */
232 protected static function summarize(
233 $number, $precision = 0, $maxPrecision = null, $units = []
234 )
235 {
236 if (empty($units)) {
237 $units = [
238 3 => 'K',
239 6 => 'M',
240 9 => 'B',
241 12 => 'T',
242 15 => 'Q',
243 ];
244 }
245
246 switch (true) {
247 case floatval($number) === 0.0:
248 return $precision > 0 ? static::format(0, $precision, $maxPrecision) : '0';
249 case $number < 0:
250 return sprintf('-%s', static::summarize(abs($number), $precision, $maxPrecision, $units));
251 case $number >= 1e15:
252 return sprintf('%s'.end($units), static::summarize($number / 1e15, $precision, $maxPrecision, $units));
253 }
254
255 $numberExponent = floor(log10($number));
256 $displayExponent = $numberExponent - ($numberExponent % 3);
257 $number /= pow(10, $displayExponent);
258
259 return trim(
260 sprintf('%s%s', static::format(
261 $number, $precision, $maxPrecision
262 ), $units[$displayExponent] ?? '')
263 );
264 }
265
266 /**
267 * Converts a numeric number in words
268 *
269 * @param int|float $number
270 * @param boolean $round whether to round (float to int)
271 * @param boolean $inCents whether the result should say
272 * "and n cents" for the fraction.
273 *
274 * @return string number in words (in human readable words),
275 * i.e: from 199900010500.91 to:
276 * one hundred and ninety nine billion nine hundred million
277 * ten thousand five hundred and ninety one cents.
278 *
279 * @throws \RangeException
280 */
281 public static function inWords($number, $options = [])
282 {
283 return (new NumberToWords)->inWords($number, $options);
284 }
285
286 /**
287 * Make words from integers
288 *
289 * @param int $number
290 * @param array $numberWords
291 * @param array $tensWords
292 * @return string
293 */
294 protected static function spellInteger($number, $numberWords, $tensWords)
295 {
296 $result = '';
297
298 $result .= static::convertToWords($number, $numberWords, $tensWords);
299
300 return trim($result);
301 }
302
303 /**
304 * Make words from fraction part
305 *
306 * @param int $number
307 * @param array $numberWords
308 * @return string
309 */
310 protected static function spellDecimal($number, $numberWords, $decimalSeparator)
311 {
312 $result = '';
313
314 $decimalPosition = strpos($number, $decimalSeparator);
315
316 if ($decimalPosition !== false) {
317
318 $decimalAsString = substr($number, $decimalPosition + 1);
319
320 for ($i = 0; $i < strlen($decimalAsString); $i++) {
321
322 $digit = intval($decimalAsString[$i]);
323
324 $result .= $numberWords[$digit] . ' ';
325 }
326 } else {
327 $result .= 'zero';
328 }
329
330 return trim($result);
331 }
332
333 /**
334 * Main function to make the unit words
335 *
336 * @param int|float $number
337 * @param array $numberWords
338 * @param array $tensWords
339 * @return string
340 */
341 protected static function convertToWords($number, $numberWords, $tensWords)
342 {
343 if ($number < 20) {
344 return $numberWords[$number];
345
346 } elseif ($number < 100) {
347
348 $result = $tensWords[intval($number / 10)];
349
350 if (intval($number) % 10 !== 0) {
351 $result .= ' ' . static::convertToWords(
352 intval($number) % 10, $numberWords, $tensWords
353 );
354 }
355
356 return $result;
357
358 } elseif ($number < 1000) {
359
360 $result = $numberWords[intval($number / 100)] . ' hundred';
361
362 if (intval($number) % 100 !== 0) {
363 $result .= ' and ' . static::convertToWords(
364 intval($number) % 100, $numberWords, $tensWords
365 );
366 }
367
368 return $result;
369
370 } elseif ($number < 1000000) {
371
372 $result = static::convertToWords(
373 intval($number / 1000), $numberWords, $tensWords
374 );
375
376 $result .= ' thousand';
377
378 if (intval($number) % 1000 !== 0) {
379 $result .= ' ' . static::convertToWords(
380 intval($number) % 1000, $numberWords, $tensWords
381 );
382 }
383
384 return $result;
385
386 } elseif ($number < 1000000000) {
387
388 $result = static::convertToWords(
389 intval($number / 1000000), $numberWords, $tensWords
390 );
391
392 $result .= ' million';
393
394 if (intval($number) % 1000000 !== 0) {
395 $result .= ' ' . static::convertToWords(
396 intval($number) % 1000000, $numberWords, $tensWords
397 );
398 }
399
400 return $result;
401
402 } elseif ($number < 1000000000000) {
403
404 $result = static::convertToWords(
405 intval($number / 1000000000), $numberWords, $tensWords
406 );
407
408 $result .= ' billion';
409
410 if (intval($number) % 1000000000 !== 0) {
411 $result .= ' ' . static::convertToWords(
412 intval($number) % 1000000000, $numberWords, $tensWords
413 );
414 }
415
416 return $result;
417
418 } elseif ($number < 1000000000000000) {
419
420 $result = static::convertToWords(
421 intval($number / 1000000000000), $numberWords, $tensWords
422 );
423
424 $result .= ' trillion';
425
426 if (intval($number) % 1000000000000 !== 0) {
427 $result .= ' ' . static::convertToWords(
428 intval($number) % 1000000000000, $numberWords, $tensWords
429 );
430 }
431
432 return $result;
433
434 } elseif (intval($number) < 1000000000000000000) {
435
436 $result = static::convertToWords(
437 intval($number / 1000000000000000), $numberWords, $tensWords
438 );
439
440 $result .= ' quadrillion';
441
442 if (intval($number) % 1000000000000000 !== 0) {
443 $result .= ' ' . static::convertToWords(
444 intval($number) % 1000000000000000, $numberWords, $tensWords
445 );
446 }
447
448 return $result;
449
450 } elseif (intval($number) < 1000000000000000000000) {
451
452 $result = static::convertToWords(
453 intval($number / 1000000000000000000), $numberWords, $tensWords
454 );
455
456 $result .= ' quintillion';
457
458 if (intval($number) % 1000000000000000000 !== 0) {
459 $result .= ' ' . static::convertToWords(
460 intval($number) % 1000000000000000000, $numberWords, $tensWords
461 );
462 }
463
464 return $result;
465 } else {
466 throw new RangeException('Out of range', 500);
467 }
468 }
469
470 /**
471 * Make words for cents
472 *
473 * @param int|float $cents
474 * @param array $numberWords
475 * @param array $tensWords
476 * @return string
477 */
478 protected static function toCents($cents, $numberWords, $tensWords)
479 {
480 if (array_key_exists($cents, $numberWords)) {
481 return $numberWords[$cents];
482 } elseif (array_key_exists($cents, $tensWords)) {
483 return $tensWords[$cents];
484 }
485
486 $result = '';
487 $tens = substr(floor($cents / 10) * 10, 0, 1);
488 $unitsPart = $cents % 10;
489
490 if (array_key_exists($tens, $tensWords)) {
491 $result .= $tensWords[$tens];
492 if ($unitsPart > 0) {
493 $result .= ' ' . $numberWords[$unitsPart];
494 }
495 } elseif (array_key_exists($unitsPart, $numberWords)) {
496 $result .= $numberWords[$unitsPart];
497 }
498
499 return $result;
500 }
501 }
502