1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Config\Parser\MetadataParser; |
6
|
|
|
|
7
|
|
|
class ClassesTypesMap |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array<string, array{class: string, type: string}> |
11
|
|
|
*/ |
12
|
|
|
protected array $classesMap = []; |
13
|
|
|
|
14
|
|
|
public function hasType(string $gqlType): bool |
15
|
|
|
{ |
16
|
|
|
return isset($this->classesMap[$gqlType]); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getType(string $gqlType): ?array |
20
|
|
|
{ |
21
|
|
|
return $this->classesMap[$gqlType] ?? null; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add a class & a type to the map |
26
|
|
|
*/ |
27
|
|
|
public function addClassType(string $typeName, string $className, string $graphQLType): void |
28
|
|
|
{ |
29
|
|
|
$this->classesMap[$typeName] = ['class' => $className, 'type' => $graphQLType]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Resolve the type associated with given class name |
34
|
|
|
*/ |
35
|
|
|
public function resolveType(string $className, array $filteredTypes = []): ?string |
36
|
|
|
{ |
37
|
|
|
foreach ($this->classesMap as $gqlType => $config) { |
38
|
|
|
if ($config['class'] === $className) { |
39
|
|
|
if (empty($filteredTypes) || in_array($config['type'], $filteredTypes)) { |
40
|
|
|
return $gqlType; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Resolve the class name associated with given type |
50
|
|
|
*/ |
51
|
|
|
public function resolveClass(string $typeName): ?string |
52
|
|
|
{ |
53
|
|
|
return $this->classesMap[$typeName]['class'] ?? null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Search the classes map for class by predicate. |
58
|
|
|
*/ |
59
|
|
|
public function searchClassesMapBy(callable $predicate, string $type): array |
60
|
|
|
{ |
61
|
|
|
$classNames = []; |
62
|
|
|
foreach ($this->classesMap as $gqlType => $config) { |
63
|
|
|
if ($config['type'] !== $type) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($predicate($gqlType, $config)) { |
68
|
|
|
$classNames[$gqlType] = $config; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $classNames; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function toArray(): array |
76
|
|
|
{ |
77
|
|
|
return $this->classesMap; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|