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