1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | // ------------------------------------------------------------------------ |
||
12 | /** |
||
13 | * Number Helper |
||
14 | * |
||
15 | * A collection of helper function to work with a numeric value. |
||
16 | */ |
||
17 | // ------------------------------------------------------------------------ |
||
18 | |||
19 | if ( ! function_exists('is_positive')) { |
||
20 | /** |
||
21 | * is_positive |
||
22 | * |
||
23 | * Determine if the number is a positive value. |
||
24 | * |
||
25 | * @param int $number The numeric value. |
||
26 | * |
||
27 | * @return bool |
||
28 | */ |
||
29 | function is_positive($number) |
||
30 | { |
||
31 | if ($number > 0) { |
||
32 | return true; |
||
33 | } |
||
34 | |||
35 | return false; |
||
36 | } |
||
37 | } |
||
38 | // ------------------------------------------------------------------------ |
||
39 | |||
40 | if ( ! function_exists('is_negative')) { |
||
41 | /** |
||
42 | * is_negative |
||
43 | * |
||
44 | * Determine if the number is a negative value. |
||
45 | * |
||
46 | * @param int $number The numeric value. |
||
47 | * |
||
48 | * @return bool |
||
49 | */ |
||
50 | function is_negative($number) |
||
51 | { |
||
52 | if ($number < 0) { |
||
53 | return true; |
||
54 | } |
||
55 | |||
56 | return false; |
||
57 | } |
||
58 | } |
||
59 | // ------------------------------------------------------------------------ |
||
60 | |||
61 | if ( ! function_exists('is_odd')) { |
||
62 | /** |
||
63 | * is_odd |
||
64 | * |
||
65 | * Determine if the number is an odd value. |
||
66 | * |
||
67 | * @param int $number The numeric value. |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | function is_odd($number) |
||
72 | { |
||
73 | if ($number % 2 == 0) { |
||
74 | return true; |
||
75 | } |
||
76 | |||
77 | return false; |
||
78 | } |
||
79 | } |
||
80 | // ------------------------------------------------------------------------ |
||
81 | |||
82 | if ( ! function_exists('is_even')) { |
||
83 | /** |
||
84 | * is_even |
||
85 | * |
||
86 | * Determine if the number is an even value. |
||
87 | * |
||
88 | * @param int $number The numeric value. |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | function is_even($number) |
||
93 | { |
||
94 | if ($number % 2 == 0) { |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | return true; |
||
99 | } |
||
100 | } |
||
101 | // ------------------------------------------------------------------------ |
||
102 | |||
103 | if ( ! function_exists('currency_format')) { |
||
104 | /** |
||
105 | * currency_format |
||
106 | * |
||
107 | * Format a number into string of formatted currency value. |
||
108 | * |
||
109 | * @param int $number The numeric value. |
||
110 | * @param string $locale The locale code indicating the language to use. |
||
111 | * @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use. |
||
112 | * @param bool $add_space Add a space between currency and the formatted number. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | function currency_format($number, $locale = 'en_US', $currency = 'USD', $add_space = false) |
||
117 | { |
||
118 | $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); |
||
119 | |||
120 | if ($add_space) { |
||
121 | $formatter->setPattern(str_replace('¤#', '¤ #', $formatter->getPattern())); |
||
122 | } |
||
123 | |||
124 | return $formatter->formatCurrency($number, $currency); |
||
125 | } |
||
126 | } |
||
127 | // ------------------------------------------------------------------------ |
||
128 | |||
129 | if ( ! function_exists('unit_format')) { |
||
130 | /** |
||
131 | * unit format |
||
132 | * |
||
133 | * Format a number with grouped thousands and added a custom unit suffix. |
||
134 | * |
||
135 | * @param int $number The numeric value. |
||
136 | * @param string $unit The custom unit suffix |
||
137 | * @param int $decimals The number of decimal points. |
||
138 | * @param string $decimal_point The separator for the decimal point. |
||
139 | * @param string $thousands_separator The thousands separator. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | function unit_format($number, $unit = null, $decimals = 0, $decimal_point = '.', $thousands_separator = ',') |
||
144 | { |
||
145 | $number = number_format($number, $decimals, $decimal_point, $thousands_separator); |
||
146 | |||
147 | if (isset($unit)) { |
||
148 | return $number . ' ' . $unit; |
||
149 | } |
||
150 | |||
151 | return $number; |
||
152 | } |
||
153 | } |
||
154 | // ------------------------------------------------------------------------ |
||
155 | |||
156 | if ( ! function_exists('hertz_format')) { |
||
157 | /** |
||
158 | * hertz_format |
||
159 | * |
||
160 | * Formats a numbers into a string with the appropriate hertz unit based on size. |
||
161 | * |
||
162 | * @param int $number The numeric value. |
||
163 | * @param int $decimals The number of decimal points. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | function hertz_format($number, $decimals = 1) |
||
168 | { |
||
169 | if ($number >= 1000000000000) { |
||
170 | $number = round($number / 1099511627776, $decimals); |
||
171 | $unit = 'THz'; |
||
172 | } elseif ($number >= 1000000000) { |
||
173 | $number = round($number / 1073741824, $decimals); |
||
174 | $unit = 'GHz'; |
||
175 | } elseif ($number >= 1000000) { |
||
176 | $number = round($number / 1048576, $decimals); |
||
177 | $unit = 'MHz'; |
||
178 | } elseif ($number >= 1000) { |
||
179 | $number = round($number / 1024, $decimals); |
||
180 | $unit = 'KHz'; |
||
181 | } else { |
||
182 | $unit = 'Hz'; |
||
183 | |||
184 | return number_format($number) . ' ' . $unit; |
||
185 | } |
||
186 | |||
187 | return number_format($number, $decimals) . ' ' . $unit; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // ------------------------------------------------------------------------ |
||
192 | |||
193 | if ( ! function_exists('roman_format')) { |
||
194 | /** |
||
195 | * roman_format |
||
196 | * |
||
197 | * Formats a number into string of roman format. |
||
198 | * |
||
199 | * @param int $number The numeric value. |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | function roman_format($number) |
||
204 | { |
||
205 | $romans = [ |
||
206 | 'M' => 1000, |
||
207 | 'CM' => 900, |
||
208 | 'D' => 500, |
||
209 | 'CD' => 400, |
||
210 | 'C' => 100, |
||
211 | 'XC' => 90, |
||
212 | 'L' => 50, |
||
213 | 'XL' => 40, |
||
214 | 'X' => 10, |
||
215 | 'IX' => 9, |
||
216 | 'V' => 5, |
||
217 | 'IV' => 4, |
||
218 | 'I' => 1, |
||
219 | ]; |
||
220 | |||
221 | $return = ''; |
||
222 | |||
223 | while ($number > 0) { |
||
224 | foreach ($romans as $rom => $arb) { |
||
225 | if ($number >= $arb) { |
||
226 | $number -= $arb; |
||
227 | $return .= $rom; |
||
228 | break; |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return $return; |
||
234 | } |
||
235 | } |
||
236 | |||
237 | // ------------------------------------------------------------------------ |
||
238 | |||
239 | if ( ! function_exists('short_format')) { |
||
240 | /** |
||
241 | * short_format |
||
242 | * |
||
243 | * Formats a number into shorted string with the appropriate unit based on size. |
||
244 | * |
||
245 | * @param int $number The numeric value. |
||
246 | * @param int $decimals The number of decimal points. |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | function short_format($number, $decimals = 0) |
||
251 | { |
||
252 | $divisors = [ |
||
253 | pow(1000, 0) => '', // 1000^0 == 1 |
||
254 | pow(1000, 1) => 'K', // Thousand |
||
255 | pow(1000, 2) => 'M', // Million |
||
256 | pow(1000, 3) => 'B', // Billion |
||
257 | pow(1000, 4) => 'T', // Trillion |
||
258 | pow(1000, 5) => 'Qa', // Quadrillion |
||
259 | pow(1000, 6) => 'Qi', // Quintillion |
||
260 | ]; |
||
261 | |||
262 | // Loop through each $divisor and find the |
||
263 | // lowest amount that matches |
||
264 | foreach ($divisors as $divisor => $shorthand) { |
||
265 | if ($number < ($divisor * 1000)) { |
||
266 | // We found a match! |
||
267 | // We found our match, or there were no matches. |
||
268 | // Either way, use the last defined value for $divisor. |
||
269 | return number_format($number / $divisor, $decimals) . $shorthand; |
||
270 | break; |
||
0 ignored issues
–
show
|
|||
271 | } |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | // ------------------------------------------------------------------------ |
||
277 | |||
278 | if ( ! function_exists('byte_format')) { |
||
279 | /** |
||
280 | * byte_format |
||
281 | * |
||
282 | * Formats a numbers into a string with the appropriate byte unit based on size. |
||
283 | * |
||
284 | * @param int $number The numeric value. |
||
285 | * @param int $decimals The number of decimal points. |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | function byte_format($number, $decimals = 1) |
||
290 | { |
||
291 | language()->loadFile('number'); |
||
292 | |||
293 | if ($number >= 1000000000000) { |
||
294 | $number = round($number / 1099511627776, $decimals); |
||
295 | $unit = language()->getLine('TERABYTE_ABBR'); |
||
296 | } elseif ($number >= 1000000000) { |
||
297 | $number = round($number / 1073741824, $decimals); |
||
298 | $unit = language()->getLine('GIGABYTE_ABBR'); |
||
299 | } elseif ($number >= 1000000) { |
||
300 | $number = round($number / 1048576, $decimals); |
||
301 | $unit = language()->getLine('MEGABYTE_ABBR'); |
||
302 | } elseif ($number >= 1000) { |
||
303 | $number = round($number / 1024, $decimals); |
||
304 | $unit = language()->getLine('KILOBYTE_ABBR'); |
||
305 | } else { |
||
306 | $unit = language()->getLine('BYTES'); |
||
307 | |||
308 | return number_format($number) . ' ' . $unit; |
||
309 | } |
||
310 | |||
311 | return number_format($number, $decimals) . ' ' . $unit; |
||
312 | } |
||
313 | } |
||
314 | |||
315 | // ------------------------------------------------------------------------ |
||
316 | |||
317 | if ( ! function_exists('ordinal_format')) { |
||
318 | /** |
||
319 | * ordinal_format |
||
320 | * |
||
321 | * Formats a number into an ordinal string such as 1st, 2nd, 3rd, 4th. |
||
322 | * |
||
323 | * @param int $number The numeric value. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | function ordinal_format($number) |
||
328 | { |
||
329 | $suffixes = |
||
330 | [ |
||
331 | 'th', |
||
332 | 'st', |
||
333 | 'nd', |
||
334 | 'rd', |
||
335 | 'th', |
||
336 | 'th', |
||
337 | 'th', |
||
338 | 'th', |
||
339 | 'th', |
||
340 | 'th', |
||
341 | ]; |
||
342 | |||
343 | return $number . ($number % 100 >= 11 && $number % 100 <= 13 |
||
344 | ? 'th' |
||
345 | : $suffixes[ $number % 10 ]); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | // ------------------------------------------------------------------------ |
||
350 | |||
351 | if ( ! function_exists('words_format')) { |
||
352 | /** |
||
353 | * words_format |
||
354 | * |
||
355 | * Formats a number into a words of spelling string. |
||
356 | * |
||
357 | * @param int $number The numeric value. |
||
358 | * @param string $locale The locale code indicating the language to use. |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | function words_format($number, $locale = 'en_US') |
||
363 | { |
||
364 | $formatter = new NumberFormatter($locale, NumberFormatter::SPELLOUT); |
||
365 | |||
366 | return $formatter->format($number); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | // ------------------------------------------------------------------------ |
||
371 | |||
372 | if ( ! function_exists('zero_fill')) { |
||
373 | /** |
||
374 | * zero_fill |
||
375 | * |
||
376 | * Formats a number into a string of leading zero number. |
||
377 | * |
||
378 | * @param int $number The numeric value |
||
379 | * @param int $digits The number of digits. |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | function zero_fill($number, $digits = 2) |
||
384 | { |
||
385 | return str_pad($number, $digits, '0', STR_PAD_LEFT); |
||
386 | } |
||
387 | } |
||
388 | // ------------------------------------------------------------------------ |
||
389 | |||
390 | if ( ! function_exists('calculate')) { |
||
391 | /** |
||
392 | * calculate |
||
393 | * |
||
394 | * Calculate from string |
||
395 | * |
||
396 | * @param string $formula |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | function calculate($formula) |
||
401 | { |
||
402 | static $function_map = [ |
||
403 | 'floor' => 'floor', |
||
404 | 'ceil' => 'ceil', |
||
405 | 'round' => 'round', |
||
406 | 'sin' => 'sin', |
||
407 | 'cos' => 'cos', |
||
408 | 'tan' => 'tan', |
||
409 | 'asin' => 'asin', |
||
410 | 'acos' => 'acos', |
||
411 | 'atan' => 'atan', |
||
412 | 'abs' => 'abs', |
||
413 | 'log' => 'log', |
||
414 | 'pi' => 'pi', |
||
415 | 'exp' => 'exp', |
||
416 | 'min' => 'min', |
||
417 | 'max' => 'max', |
||
418 | 'rand' => 'rand', |
||
419 | 'fmod' => 'fmod', |
||
420 | 'sqrt' => 'sqrt', |
||
421 | 'deg2rad' => 'deg2rad', |
||
422 | 'rad2deg' => 'rad2deg', |
||
423 | ]; |
||
424 | |||
425 | // Remove any whitespace |
||
426 | $formula = strtolower(preg_replace('~\s+~', '', $formula)); |
||
427 | |||
428 | // Empty formula |
||
429 | if ($formula === '') { |
||
430 | trigger_error('Empty formula', E_USER_ERROR); |
||
431 | |||
432 | return null; |
||
433 | } |
||
434 | |||
435 | // Illegal function |
||
436 | $formula = preg_replace_callback( |
||
437 | '~\b[a-z]\w*\b~', |
||
438 | function ($match) use ($function_map) { |
||
439 | $function = $match[ 0 ]; |
||
440 | if ( ! isset($function_map[ $function ])) { |
||
441 | trigger_error("Illegal function '{$match[0]}'", E_USER_ERROR); |
||
442 | |||
443 | return ''; |
||
444 | } |
||
445 | |||
446 | return $function_map[ $function ]; |
||
447 | }, |
||
448 | $formula |
||
449 | ); |
||
450 | |||
451 | // Invalid function calls |
||
452 | if (preg_match('~[a-z]\w*(?![\(\w])~', $formula, $match) > 0) { |
||
453 | trigger_error("Invalid function call '{$match[0]}'", E_USER_ERROR); |
||
454 | |||
455 | return null; |
||
456 | } |
||
457 | |||
458 | // Legal characters |
||
459 | if (preg_match('~[^-+/%*&|<>!=.()0-9a-z,]~', $formula, $match) > 0) { |
||
460 | trigger_error("Illegal character '{$match[0]}'", E_USER_ERROR); |
||
461 | |||
462 | return null; |
||
463 | } |
||
464 | |||
465 | return eval("return({$formula});"); |
||
0 ignored issues
–
show
|
|||
466 | } |
||
467 | } |
||
468 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.