Conditions | 8 |
Paths | 7 |
Total Lines | 24 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
12 | public function coerce($value) |
||
13 | { |
||
14 | if ($value === '') { |
||
15 | throw new InvalidTypeException('Int cannot represent non 32-bit signed integer value: (empty string)'); |
||
16 | } |
||
17 | |||
18 | if (\is_bool($value)) { |
||
19 | $value = (int)$value; |
||
20 | } |
||
21 | |||
22 | if (!\is_int($value) || $value > PHP_INT_MAX || $value < PHP_INT_MIN) { |
||
23 | throw new InvalidTypeException( |
||
24 | \sprintf('Int cannot represent non 32-bit signed integer value: %s', $value) |
||
25 | ); |
||
26 | } |
||
27 | |||
28 | $intValue = (int)$value; |
||
29 | $floatValue = (float)$value; |
||
30 | |||
31 | if ($floatValue != $intValue || \floor($floatValue) !== $floatValue) { |
||
32 | throw new InvalidTypeException(\sprintf('Int cannot represent non-integer value: %s', $value)); |
||
33 | } |
||
34 | |||
35 | return $intValue; |
||
36 | } |
||
38 |