1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\GraphQl\Type\Definition; |
15
|
|
|
|
16
|
|
|
use GraphQL\Error\Error; |
17
|
|
|
use GraphQL\Language\AST\BooleanValueNode; |
18
|
|
|
use GraphQL\Language\AST\FloatValueNode; |
19
|
|
|
use GraphQL\Language\AST\IntValueNode; |
20
|
|
|
use GraphQL\Language\AST\ListValueNode; |
21
|
|
|
use GraphQL\Language\AST\ObjectValueNode; |
22
|
|
|
use GraphQL\Language\AST\StringValueNode; |
23
|
|
|
use GraphQL\Language\AST\ValueNode; |
24
|
|
|
use GraphQL\Type\Definition\ScalarType; |
25
|
|
|
use GraphQL\Utils\Utils; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Represents an iterable type. |
29
|
|
|
* |
30
|
|
|
* @experimental |
31
|
|
|
* |
32
|
|
|
* @author Alan Poulain <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
final class IterableType extends ScalarType implements TypeInterface |
35
|
|
|
{ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->name = 'Iterable'; |
39
|
|
|
$this->description = 'The `Iterable` scalar type represents an array or a Traversable with any kind of data.'; |
40
|
|
|
|
41
|
|
|
parent::__construct(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getName(): string |
45
|
|
|
{ |
46
|
|
|
return $this->name; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function serialize($value) |
53
|
|
|
{ |
54
|
|
|
if (!is_iterable($value)) { |
55
|
|
|
throw new Error(sprintf('Iterable cannot represent non iterable value: %s', Utils::printSafe($value))); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function parseValue($value) |
65
|
|
|
{ |
66
|
|
|
if (!is_iterable($value)) { |
67
|
|
|
throw new Error(sprintf('Iterable cannot represent non iterable value: %s', Utils::printSafeJson($value))); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function parseLiteral($valueNode, array $variables = null) |
77
|
|
|
{ |
78
|
|
|
if ($valueNode instanceof ObjectValueNode || $valueNode instanceof ListValueNode) { |
79
|
|
|
return $this->parseIterableLiteral($valueNode); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Intentionally without message, as all information already in wrapped Exception |
83
|
|
|
throw new \Exception(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param StringValueNode|BooleanValueNode|IntValueNode|FloatValueNode|ObjectValueNode|ListValueNode|ValueNode $valueNode |
88
|
|
|
*/ |
89
|
|
|
private function parseIterableLiteral($valueNode) |
90
|
|
|
{ |
91
|
|
|
switch ($valueNode) { |
92
|
|
|
case $valueNode instanceof StringValueNode: |
93
|
|
|
case $valueNode instanceof BooleanValueNode: |
94
|
|
|
return $valueNode->value; |
95
|
|
|
case $valueNode instanceof IntValueNode: |
96
|
|
|
return (int) $valueNode->value; |
97
|
|
|
case $valueNode instanceof FloatValueNode: |
98
|
|
|
return (float) $valueNode->value; |
99
|
|
|
case $valueNode instanceof ObjectValueNode: |
100
|
|
|
$value = []; |
101
|
|
|
foreach ($valueNode->fields as $field) { |
102
|
|
|
$value[$field->name->value] = $this->parseIterableLiteral($field->value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $value; |
106
|
|
|
case $valueNode instanceof ListValueNode: |
107
|
|
|
$list = []; |
108
|
|
|
foreach ($valueNode->values as $value) { |
109
|
|
|
$list[] = $this->parseIterableLiteral($value); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $list; |
113
|
|
|
default: |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|