forked from webonyx/graphql-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarWarsData.php
153 lines (136 loc) · 3.72 KB
/
StarWarsData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php declare(strict_types=1);
namespace GraphQL\Tests;
final class StarWarsData
{
/** @return array<string, mixed> */
public static function character(string $id): ?array
{
$key = (int) $id;
return self::humans()[$key]
?? self::droids()[$key]
?? null;
}
/** @return array<int, mixed> */
public static function humans(): array
{
return [
1000 => self::luke(),
1001 => self::vader(),
1002 => self::han(),
1003 => self::leia(),
1004 => self::tarkin(),
];
}
/** @return array<string, mixed> */
private static function luke(): array
{
return [
'id' => '1000',
'name' => 'Luke Skywalker',
'friends' => ['1002', '1003', '2000', '2001'],
'appearsIn' => [4, 5, 6],
'homePlanet' => 'Tatooine',
];
}
/** @return array<string, mixed> */
private static function vader(): array
{
return [
'id' => '1001',
'name' => 'Darth Vader',
'friends' => ['1004'],
'appearsIn' => [4, 5, 6],
'homePlanet' => 'Tatooine',
];
}
/** @return array<string, mixed> */
private static function han(): array
{
return [
'id' => '1002',
'name' => 'Han Solo',
'friends' => ['1000', '1003', '2001'],
'appearsIn' => [4, 5, 6],
];
}
/** @return array<string, mixed> */
private static function leia(): array
{
return [
'id' => '1003',
'name' => 'Leia Organa',
'friends' => ['1000', '1002', '2000', '2001'],
'appearsIn' => [4, 5, 6],
'homePlanet' => 'Alderaan',
];
}
/** @return array<string, mixed> */
private static function tarkin(): array
{
return [
'id' => '1004',
'name' => 'Wilhuff Tarkin',
'friends' => ['1001'],
'appearsIn' => [4],
];
}
/** @return array<int, mixed> */
public static function droids(): array
{
return [
2000 => self::threepio(),
2001 => self::artoo(),
];
}
/** @return array<string, mixed> */
private static function threepio(): array
{
return [
'id' => '2000',
'name' => 'C-3PO',
'friends' => ['1000', '1002', '1003', '2001'],
'appearsIn' => [4, 5, 6],
'primaryFunction' => 'Protocol',
];
}
/** @return array<string, mixed> */
private static function artoo(): array
{
return [
'id' => '2001',
'name' => 'R2-D2',
'friends' => ['1000', '1002', '1003'],
'appearsIn' => [4, 5, 6],
'primaryFunction' => 'Astromech',
];
}
/**
* @param array<string, mixed> $character
*
* @return array<int, mixed>
*/
public static function friends(array $character): array
{
return \array_map([self::class, 'character'], $character['friends']);
}
/** @return array<string, mixed> */
public static function hero(?int $episode): array
{
if ($episode === 5) {
// Luke is the hero of Episode V.
return self::luke();
}
// Artoo is the hero otherwise.
return self::artoo();
}
/** @return array<string, mixed>|null */
public static function human(string $id): ?array
{
return self::humans()[(int) $id] ?? null;
}
/** @return array<string, mixed>|null */
public static function droid(string $id): ?array
{
return self::droids()[(int) $id] ?? null;
}
}