1 | <?php |
||
7 | class Numeric { |
||
8 | /** |
||
9 | * Ensure integer or or float value by safely converting. |
||
10 | * |
||
11 | * Safe conversions to int or float: |
||
12 | * |
||
13 | * ``` |
||
14 | * '-1' => -1 |
||
15 | * '+00.0' => 0.0 |
||
16 | * '1' => 1 |
||
17 | * ' 1 ' => 1 |
||
18 | * '01' => 1 |
||
19 | * '0.1' => 0.1 |
||
20 | * '.1' => 0.1 |
||
21 | * ``` |
||
22 | * |
||
23 | * Invalid conversions: |
||
24 | * |
||
25 | * ``` |
||
26 | * 'x' |
||
27 | * '0a' |
||
28 | * '' |
||
29 | * '-' |
||
30 | * ' ' |
||
31 | * '0+1' |
||
32 | * '.' |
||
33 | * ',' |
||
34 | * ``` |
||
35 | * |
||
36 | * @param mixed $value |
||
37 | * |
||
38 | * @throws \InvalidArgumentException If the value could not be safely converted to int or float. |
||
39 | * |
||
40 | * @return int|float |
||
41 | */ |
||
42 | public static function ensure($value) { |
||
58 | |||
59 | /** |
||
60 | * ensureInteger values by safely converting. |
||
61 | * |
||
62 | * These are safe conversions because no information is lost: |
||
63 | * |
||
64 | * ``` |
||
65 | * 1 => 1 |
||
66 | * '1.00' => 1 |
||
67 | * '1' => 1 |
||
68 | * '+1' => 1 |
||
69 | * ``` |
||
70 | * |
||
71 | * These are invalid conversions because information would be lost: |
||
72 | * |
||
73 | * ``` |
||
74 | * 0.1 |
||
75 | * '0.1' |
||
76 | * '.1' |
||
77 | * ``` |
||
78 | * |
||
79 | * If you don't care about this, you should cast to int instead: `(int)$value` |
||
80 | * |
||
81 | * @param mixed $value |
||
82 | * |
||
83 | * @throws \InvalidArgumentException If floating point information would be lost, i.e. it does not look like an integer. |
||
84 | * |
||
85 | * @return int |
||
86 | */ |
||
87 | public static function ensureInteger($value) { |
||
100 | |||
101 | /** |
||
102 | * ensureFloat values by safely converting. |
||
103 | * |
||
104 | * For example the following conversions are safe: |
||
105 | * |
||
106 | * ``` |
||
107 | * '0' => 0.0 |
||
108 | * '0.0' => 0.0 |
||
109 | * '0.1' => 0.1 |
||
110 | * '-5.1' => 5.1 |
||
111 | * ``` |
||
112 | * |
||
113 | * @param mixed $value |
||
114 | * |
||
115 | * @throws \InvalidArgumentException If value could not be safely converted to float. |
||
116 | * |
||
117 | * @return float |
||
118 | */ |
||
119 | public static function ensureFloat($value) { |
||
122 | } |
||
123 |