Completed
Push — master ( bc03eb...ec8ced )
by Kévin
04:05
created

IterableType::parseIterableLiteral()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 9
nop 1
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\Error\InvariantViolation;
18
use GraphQL\Language\AST\BooleanValueNode;
19
use GraphQL\Language\AST\FloatValueNode;
20
use GraphQL\Language\AST\IntValueNode;
21
use GraphQL\Language\AST\ListValueNode;
22
use GraphQL\Language\AST\ObjectValueNode;
23
use GraphQL\Language\AST\StringValueNode;
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
35
{
36
    public $name = 'Iterable';
37
38
    public $description = 'The `Iterable` scalar type represents an array or a Traversable with any kind of data.';
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function serialize($value)
44
    {
45
        // is_iterable
46
        if (!(\is_array($value) || $value instanceof \Traversable)) {
47
            throw new InvariantViolation(sprintf('Iterable cannot represent non iterable value: %s', Utils::printSafe($value)));
48
        }
49
50
        return $value;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function parseValue($value)
57
    {
58
        // is_iterable
59
        if (!(\is_array($value) || $value instanceof \Traversable)) {
60
            throw new Error(sprintf('Iterable cannot represent non iterable value: %s', Utils::printSafeJson($value)));
61
        }
62
63
        return $value;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function parseLiteral($valueNode)
70
    {
71
        if ($valueNode instanceof ObjectValueNode || $valueNode instanceof ListValueNode) {
72
            return $this->parseIterableLiteral($valueNode);
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * @param StringValueNode|BooleanValueNode|IntValueNode|FloatValueNode|ObjectValueNode|ListValueNode $valueNode
80
     */
81
    private function parseIterableLiteral($valueNode)
82
    {
83
        switch ($valueNode) {
84
            case $valueNode instanceof StringValueNode:
85
            case $valueNode instanceof BooleanValueNode:
86
                return $valueNode->value;
87
            case $valueNode instanceof IntValueNode:
88
                return (int) $valueNode->value;
89
            case $valueNode instanceof FloatValueNode:
90
                return (float) $valueNode->value;
91
            case $valueNode instanceof ObjectValueNode:
92
                $value = [];
93
                foreach ($valueNode->fields as $field) {
94
                    $value[$field->name->value] = $this->parseIterableLiteral($field->value);
95
                }
96
97
                return $value;
98
            case $valueNode instanceof ListValueNode:
99
                $list = [];
100
                foreach ($valueNode->values as $value) {
101
                    $list[] = $this->parseIterableLiteral($value);
102
                }
103
104
                return $list;
105
            default:
106
                return null;
107
        }
108
    }
109
}
110