|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ecodev\Felix\Api\Scalar; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Error\Error; |
|
8
|
|
|
use GraphQL\Language\AST\FloatValueNode; |
|
9
|
|
|
use GraphQL\Language\AST\IntValueNode; |
|
10
|
|
|
use GraphQL\Language\AST\Node; |
|
11
|
|
|
use GraphQL\Language\AST\StringValueNode; |
|
12
|
|
|
use GraphQL\Type\Definition\ScalarType; |
|
13
|
|
|
use GraphQL\Utils\Utils; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractDecimalType extends ScalarType |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Return the number of digits after the decimal. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract protected function getScale(): int; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Return the minimum accepted value, if any. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function getMinimum(): ?string |
|
26
|
|
|
{ |
|
27
|
|
|
return null; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return the maximum accepted value, if any. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function getMaximum(): ?string |
|
34
|
|
|
{ |
|
35
|
|
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Validate value. |
|
40
|
|
|
*/ |
|
41
|
40 |
|
private function isValid(string $value): bool |
|
42
|
|
|
{ |
|
43
|
40 |
|
$decimal = $this->getScale(); |
|
44
|
|
|
|
|
45
|
40 |
|
if (!preg_match('~^-?\d+(\.\d{0,' . $decimal . '})?$~', $value)) { |
|
46
|
12 |
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
28 |
|
$minimum = $this->getMinimum(); |
|
|
|
|
|
|
50
|
28 |
|
if ($minimum !== null && bccomp($value, $minimum, $decimal) === -1) { |
|
|
|
|
|
|
51
|
2 |
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
26 |
|
$maximum = $this->getMaximum(); |
|
|
|
|
|
|
55
|
26 |
|
if ($maximum !== null && bccomp($value, $maximum, $decimal) === 1) { |
|
|
|
|
|
|
56
|
2 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
24 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Serializes an internal value to include in a response. |
|
64
|
|
|
*/ |
|
65
|
20 |
|
public function serialize(mixed $value): mixed |
|
66
|
|
|
{ |
|
67
|
|
|
// Assuming internal representation is always correct: |
|
68
|
20 |
|
return $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Parses an externally provided value (query variable) to use as an input. |
|
73
|
|
|
* |
|
74
|
|
|
* @param null|float|int|string $value |
|
75
|
|
|
*/ |
|
76
|
20 |
|
public function parseValue(mixed $value): string |
|
77
|
|
|
{ |
|
78
|
20 |
|
$parsedValue = (string) $value; |
|
79
|
20 |
|
if (!$this->isValid($parsedValue)) { |
|
80
|
8 |
|
throw new Error('Query error: Not a valid ' . $this->name . ': ' . Utils::printSafe($value)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
12 |
|
return $parsedValue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Parses an externally provided literal value to use as an input (e.g. in Query AST). |
|
88
|
|
|
*/ |
|
89
|
21 |
|
public function parseLiteral(Node $valueNode, ?array $variables = null): string |
|
90
|
|
|
{ |
|
91
|
|
|
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL |
|
92
|
|
|
// error location in query: |
|
93
|
|
|
|
|
94
|
21 |
|
if (!($valueNode instanceof StringValueNode || $valueNode instanceof IntValueNode || $valueNode instanceof FloatValueNode)) { |
|
95
|
1 |
|
throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
20 |
|
$parsedValue = (string) $valueNode->value; |
|
99
|
20 |
|
if (!$this->isValid($parsedValue)) { |
|
100
|
8 |
|
throw new Error('Query error: Not a valid ' . $this->name, $valueNode); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
12 |
|
return $parsedValue; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.