|
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
|
|
|
use UnexpectedValueException; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractDecimalType extends ScalarType |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Return the number of digits after the decimal |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract protected function getScale(): int; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Return the minimum accepted value, if any |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function getMinimum(): ?string |
|
27
|
|
|
{ |
|
28
|
|
|
return null; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return the maximum accepted value, if any |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function getMaximum(): ?string |
|
35
|
|
|
{ |
|
36
|
|
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Validate value |
|
41
|
|
|
*/ |
|
42
|
68 |
|
private function isValid(string $value): bool |
|
43
|
|
|
{ |
|
44
|
68 |
|
$decimal = $this->getScale(); |
|
45
|
|
|
|
|
46
|
68 |
|
if (!preg_match('~^-?\d+(\.\d{0,' . $decimal . '})?$~', $value)) { |
|
47
|
20 |
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
48 |
|
$minimum = $this->getMinimum(); |
|
|
|
|
|
|
51
|
48 |
|
if ($minimum !== null && bccomp($value, $minimum, $decimal) === -1) { |
|
|
|
|
|
|
52
|
2 |
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
46 |
|
$maximum = $this->getMaximum(); |
|
|
|
|
|
|
56
|
46 |
|
if ($maximum !== null && bccomp($value, $maximum, $decimal) === 1) { |
|
|
|
|
|
|
57
|
2 |
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
44 |
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Serializes an internal value to include in a response. |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
34 |
|
public function serialize($value) |
|
71
|
|
|
{ |
|
72
|
|
|
// Assuming internal representation is always correct: |
|
73
|
34 |
|
return $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Parses an externally provided value (query variable) to use as an input |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $value |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
34 |
|
public function parseValue($value) |
|
84
|
|
|
{ |
|
85
|
34 |
|
$parsedValue = (string) $value; |
|
86
|
34 |
|
if (!$this->isValid($parsedValue)) { |
|
87
|
12 |
|
throw new UnexpectedValueException('Query error: Not a valid ' . $this->name . ': ' . Utils::printSafe($value)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
22 |
|
return $parsedValue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Parses an externally provided literal value to use as an input (e.g. in Query AST) |
|
95
|
|
|
* |
|
96
|
|
|
* @return null|string |
|
97
|
|
|
*/ |
|
98
|
34 |
|
public function parseLiteral(Node $ast, ?array $variables = null) |
|
99
|
|
|
{ |
|
100
|
|
|
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL |
|
101
|
|
|
// error location in query: |
|
102
|
|
|
|
|
103
|
34 |
|
if (!($ast instanceof StringValueNode || $ast instanceof IntValueNode || $ast instanceof FloatValueNode)) { |
|
104
|
|
|
throw new Error('Query error: Can only parse strings got: ' . $ast->kind, $ast); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
34 |
|
$parsedValue = (string) $ast->value; |
|
108
|
34 |
|
if (!$this->isValid($parsedValue)) { |
|
109
|
12 |
|
throw new Error('Query error: Not a valid ' . $this->name, $ast); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
22 |
|
return $parsedValue; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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.