PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.41
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.41
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Support / Number.php

Number.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.41, at vendor/wpfluent/framework/src/WPFluent/Support/Number.php

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