1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Type\Definition; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use GraphQL\Error\Error; |
9
|
|
|
use GraphQL\Language\AST\IntValueNode; |
10
|
|
|
use GraphQL\Language\AST\Node; |
11
|
|
|
use GraphQL\Utils\Utils; |
12
|
|
|
use function floatval; |
13
|
|
|
use function floor; |
14
|
|
|
use function intval; |
15
|
|
|
use function is_array; |
16
|
|
|
use function is_bool; |
17
|
|
|
use function is_float; |
18
|
|
|
use function is_int; |
19
|
|
|
use function is_numeric; |
20
|
|
|
use function sprintf; |
21
|
|
|
|
22
|
|
|
class IntType extends ScalarType |
23
|
|
|
{ |
24
|
|
|
// As per the GraphQL Spec, Integers are only treated as valid when a valid |
25
|
|
|
// 32-bit signed integer, providing the broadest support across platforms. |
26
|
|
|
// |
27
|
|
|
// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because |
28
|
|
|
// they are internally represented as IEEE 754 doubles. |
29
|
|
|
private const MAX_INT = 2147483647; |
30
|
|
|
private const MIN_INT = -2147483648; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
public $name = Type::INT; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
public $description = |
37
|
|
|
'The `Int` scalar type represents non-fractional signed whole numeric |
38
|
|
|
values. Int can represent values between -(2^31) and 2^31 - 1. '; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $value |
42
|
|
|
* |
43
|
|
|
* @return int|null |
44
|
|
|
* |
45
|
|
|
* @throws Error |
46
|
|
|
*/ |
47
|
41 |
|
public function serialize($value) |
48
|
|
|
{ |
49
|
|
|
// Fast path for 90+% of cases: |
50
|
41 |
|
if (is_int($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) { |
51
|
25 |
|
return $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
18 |
|
$float = is_numeric($value) || is_bool($value) ? floatval($value) : null; |
55
|
|
|
|
56
|
18 |
|
if ($float === null || floor($float) !== $float) { |
57
|
9 |
|
throw new Error( |
58
|
|
|
'Int cannot represent non-integer value: ' . |
59
|
9 |
|
Utils::printSafe($value) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
9 |
|
if ($float > self::MAX_INT || $float < self::MIN_INT) { |
64
|
6 |
|
throw new Error( |
65
|
|
|
'Int cannot represent non 32-bit signed integer value: ' . |
66
|
6 |
|
Utils::printSafe($value) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
return intval($float); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $value |
75
|
|
|
* |
76
|
|
|
* @return int|null |
77
|
|
|
* |
78
|
|
|
* @throws Error |
79
|
|
|
*/ |
80
|
23 |
|
public function parseValue($value) |
81
|
|
|
{ |
82
|
23 |
|
$isInt = is_int($value) || (is_float($value) && floor($value) === $value); |
83
|
|
|
|
84
|
23 |
|
if (! $isInt) { |
85
|
9 |
|
throw new Error( |
86
|
|
|
'Int cannot represent non-integer value: ' . |
87
|
9 |
|
Utils::printSafe($value) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
14 |
|
if ($value > self::MAX_INT || $value < self::MIN_INT) { |
92
|
4 |
|
throw new Error( |
93
|
|
|
'Int cannot represent non 32-bit signed integer value: ' . |
94
|
4 |
|
Utils::printSafe($value) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
10 |
|
return intval($value); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Node $valueNode |
103
|
|
|
* @param mixed[]|null $variables |
104
|
|
|
* |
105
|
|
|
* @return int|null |
106
|
|
|
* |
107
|
|
|
* @throws Exception |
108
|
|
|
*/ |
109
|
45 |
|
public function parseLiteral($valueNode, ?array $variables = null) |
110
|
|
|
{ |
111
|
45 |
|
if ($valueNode instanceof IntValueNode) { |
112
|
34 |
|
$val = (int) $valueNode->value; |
113
|
34 |
|
if ($valueNode->value === (string) $val && self::MIN_INT <= $val && $val <= self::MAX_INT) { |
114
|
33 |
|
return $val; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Intentionally without message, as all information already in wrapped Exception |
119
|
13 |
|
throw new Exception(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|