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