nelson6e65 /
php_nml
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * PHP: Nelson Martell Library file |
||
| 5 | * |
||
| 6 | * Copyright © 2021 Nelson Martell (http://nelson6e65.github.io) |
||
| 7 | * |
||
| 8 | * Licensed under The MIT License (MIT) |
||
| 9 | * For full copyright and license information, please see the LICENSE |
||
| 10 | * Redistributions of files must retain the above copyright notice. |
||
| 11 | * |
||
| 12 | * @copyright 2021 Nelson Martell |
||
| 13 | * @link http://nelson6e65.github.io/php_nml/ |
||
| 14 | * @since 1.0.0 |
||
| 15 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
||
| 16 | * */ |
||
| 17 | |||
| 18 | namespace NelsonMartell\Extensions; |
||
| 19 | |||
| 20 | use InvalidArgumentException; |
||
| 21 | use NelsonMartell\IComparer; |
||
| 22 | use NelsonMartell\StrictObject; |
||
| 23 | |||
| 24 | use function NelsonMartell\msg; |
||
| 25 | use function NelsonMartell\typeof; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Provides extension methods to handle numbers. |
||
| 29 | * |
||
| 30 | * @since 1.0.0 |
||
| 31 | * @author Nelson Martell <[email protected]> |
||
| 32 | * */ |
||
| 33 | class Numbers implements IComparer |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Ensures that object given is an integer. Else, thows an exception. |
||
| 37 | * |
||
| 38 | * @param mixed|float|int $obj Object to validate. |
||
| 39 | * |
||
| 40 | * @return float|int Same object given, but ensured that is a number. |
||
| 41 | * |
||
| 42 | * @throws InvalidArgumentException if object is not a `int` or `float`. |
||
| 43 | */ |
||
| 44 | public static function ensureIsNumeric($obj) |
||
| 45 | { |
||
| 46 | if (is_int($obj) || is_float($obj)) { |
||
| 47 | return $obj; |
||
| 48 | } |
||
| 49 | |||
| 50 | $msg = msg('Provided object must to be an integer or float; "{0}" given.', typeof($obj)); |
||
| 51 | throw new InvalidArgumentException($msg); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritDoc} |
||
| 56 | * |
||
| 57 | * This methods is specific for the case when one of them are `numeric`. In other case, will fallback to |
||
| 58 | * `Objects::compare()`.` You should use it directly instead of this method as comparation function |
||
| 59 | * for `usort()`. |
||
| 60 | * |
||
| 61 | * @param int|float|mixed $left |
||
| 62 | * @param int|float|mixed $right |
||
| 63 | * |
||
| 64 | * @return int|null |
||
| 65 | * |
||
| 66 | * @since 1.0.0 |
||
| 67 | * @see Objects::compare() |
||
| 68 | */ |
||
| 69 | 24 | public static function compare($left, $right) |
|
| 70 | { |
||
| 71 | 24 | if (is_numeric($left)) { |
|
| 72 | 24 | if (is_numeric($right) && !is_nan($right)) { |
|
| 73 | 16 | $r = $left - $right; |
|
| 74 | |||
| 75 | 16 | if ($r > 0) { |
|
| 76 | 8 | $r = (int) ceil($r); |
|
| 77 | } else { |
||
| 78 | 9 | $r = (int) floor($r); |
|
| 79 | } |
||
| 80 | |||
| 81 | 16 | return $r; |
|
| 82 | 9 | } elseif ($right === null) { |
|
| 83 | 4 | return 1; |
|
| 84 | } else { |
||
| 85 | 5 | return -1; |
|
| 86 | } |
||
| 87 | 5 | } elseif (is_numeric($right)) { |
|
| 88 | 5 | $r = static::compare($right, $left); |
|
| 89 | |||
| 90 | 5 | if ($r !== null) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 91 | 5 | $r *= -1; |
|
| 92 | } |
||
| 93 | |||
| 94 | 5 | return $r; |
|
| 95 | } |
||
| 96 | |||
| 97 | return Objects::compare($left, $right); |
||
| 98 | } |
||
| 99 | } |
||
| 100 |