1 | <?php |
||
2 | |||
3 | namespace Fnash\GraphQL; |
||
4 | |||
5 | final class Fragment |
||
6 | { |
||
7 | /** |
||
8 | * @var string |
||
9 | */ |
||
10 | public $name; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | public $type; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | public $fields = []; |
||
21 | |||
22 | /** |
||
23 | * @param string $name |
||
24 | * @param string $type |
||
25 | * @param array $fields |
||
26 | * |
||
27 | * @return Fragment |
||
28 | */ |
||
29 | public static function create(string $name, string $type, array $fields = []): Fragment |
||
30 | { |
||
31 | return new self($name, $type, $fields); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param array $fields |
||
36 | * |
||
37 | * @return Fragment |
||
38 | */ |
||
39 | public function fields(array $fields = []): Fragment |
||
40 | { |
||
41 | foreach ($fields as $fieldAlias => $field) { |
||
42 | if (\is_string($field)) { |
||
43 | if (\is_string($fieldAlias)) { |
||
44 | $this->fields[$fieldAlias] = $field; |
||
45 | } else { |
||
46 | $this->fields[$field] = $field; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | if ($field instanceof Query) { |
||
51 | $this->fields[$fieldAlias] = $field; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | ksort($this->fields); |
||
56 | |||
57 | return $this; |
||
58 | } |
||
59 | |||
60 | public function __toString() |
||
61 | { |
||
62 | $query = sprintf('fragment %s on %s { %s }', $this->name, $this->type, static::printFields($this->fields)); |
||
63 | |||
64 | $query = \GraphQL\Language\Printer::doPrint(\GraphQL\Language\Parser::parse((string) $query)); |
||
65 | |||
66 | return $query; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param array $value |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | private static function printFields(array $value): string |
||
75 | { |
||
76 | $fields = []; |
||
77 | |||
78 | foreach ($value as $fieldAlias => $field) { |
||
79 | $directive = ''; |
||
80 | |||
81 | if (\is_string($field)) { |
||
82 | if ($fieldAlias !== $field) { |
||
83 | $fields[] = sprintf('%s: %s %s', $fieldAlias, $field, $directive); |
||
84 | } else { |
||
85 | $fields[] = sprintf('%s %s', $field, $directive); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | if ($field instanceof Query) { |
||
90 | if (null !== $field->type) { |
||
91 | $fieldAlias = sprintf('%s: %s', $fieldAlias, $field->type); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
92 | } |
||
93 | |||
94 | $fields[] = sprintf('%s %s { %s }', $fieldAlias, $directive, static::printFields($field->fields)); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return implode(', ', $fields); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param string $name |
||
103 | * @param string $type |
||
104 | * @param array $fields |
||
105 | */ |
||
106 | private function __construct(string $name, string $type, array $fields) |
||
107 | { |
||
108 | $this->name = $name; |
||
109 | |||
110 | $this->type = $type; |
||
111 | |||
112 | $this->fields($fields); |
||
113 | } |
||
114 | } |
||
115 |