1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Type\Definition; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\InvariantException; |
6
|
|
|
use Digia\GraphQL\Language\Node\ASTNodeAwareInterface; |
7
|
|
|
use Digia\GraphQL\Language\Node\ASTNodeTrait; |
8
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
9
|
|
|
use Digia\GraphQL\Language\Node\ScalarTypeDefinitionNode; |
10
|
|
|
use function Digia\GraphQL\Util\invariant; |
11
|
|
|
|
12
|
|
|
class ScalarType implements TypeInterface, NamedTypeInterface, LeafTypeInterface, InputTypeInterface, |
13
|
|
|
OutputTypeInterface, ASTNodeAwareInterface |
14
|
|
|
{ |
15
|
|
|
use NameTrait; |
16
|
|
|
use DescriptionTrait; |
17
|
|
|
use ASTNodeTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var callable |
21
|
|
|
*/ |
22
|
|
|
protected $serializeCallback; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var callable|null |
26
|
|
|
*/ |
27
|
|
|
protected $parseValueCallback; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var callable|null |
31
|
|
|
*/ |
32
|
|
|
protected $parseLiteralCallback; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ScalarType constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* @param null|string $description |
39
|
|
|
* @param callable|null $serializeCallback |
40
|
|
|
* @param callable|null $parseValueCallback |
41
|
|
|
* @param callable|null $parseLiteralCallback |
42
|
|
|
* @param ScalarTypeDefinitionNode|null $astNode |
43
|
|
|
* @throws InvariantException |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
string $name, |
47
|
|
|
?string $description, |
48
|
|
|
?callable $serializeCallback, |
49
|
|
|
?callable $parseValueCallback, |
50
|
|
|
?callable $parseLiteralCallback, |
51
|
|
|
?ScalarTypeDefinitionNode $astNode |
52
|
|
|
) { |
53
|
|
|
$this->name = $name; |
54
|
|
|
$this->description = $description; |
55
|
|
|
$this->serializeCallback = $serializeCallback; |
56
|
|
|
$this->parseValueCallback = $parseValueCallback; |
57
|
|
|
$this->parseLiteralCallback = $parseLiteralCallback; |
58
|
|
|
$this->astNode = $astNode; |
59
|
|
|
|
60
|
|
|
invariant( |
61
|
|
|
\is_callable($this->serializeCallback), |
62
|
|
|
\sprintf( |
63
|
|
|
'%s must provide "serialize" function. If this custom Scalar ' . |
64
|
|
|
'is also used as an input type, ensure "parseValue" and "parseLiteral" ' . |
65
|
|
|
'functions are also provided.', |
66
|
|
|
$this->name |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if (null !== $this->parseValueCallback || null !== $this->parseLiteralCallback) { |
71
|
|
|
invariant( |
72
|
|
|
\is_callable($this->parseValueCallback) && \is_callable($this->parseLiteralCallback), |
73
|
|
|
\sprintf('%s must provide both "parseValue" and "parseLiteral" functions.', $this->name) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $value |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function serialize($value) |
83
|
|
|
{ |
84
|
|
|
return \call_user_func($this->serializeCallback, $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param mixed $value |
89
|
|
|
* @return mixed|null |
90
|
|
|
*/ |
91
|
|
|
public function parseValue($value) |
92
|
|
|
{ |
93
|
|
|
return null !== $this->parseValueCallback |
94
|
|
|
? \call_user_func($this->parseValueCallback, $value) |
95
|
|
|
: null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param NodeInterface $node |
100
|
|
|
* @param array|null $variables |
101
|
|
|
* @return mixed|null |
102
|
|
|
*/ |
103
|
|
|
public function parseLiteral(NodeInterface $node, ?array $variables = null) |
104
|
|
|
{ |
105
|
|
|
return null !== $this->parseLiteralCallback |
106
|
|
|
? \call_user_func($this->parseLiteralCallback, $node, $variables) |
107
|
|
|
: null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param mixed $value |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isValidValue($value): bool |
115
|
|
|
{ |
116
|
|
|
return null !== $this->parseValue($value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param NodeInterface $node |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function isValidLiteral(NodeInterface $node): bool |
124
|
|
|
{ |
125
|
|
|
return null !== $this->parseLiteral($node); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|