1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Types; |
5
|
|
|
|
6
|
|
|
use TheCodingMachine\GraphQL\Controllers\FieldsBuilderFactory; |
7
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An object type built from the Type annotation |
11
|
|
|
*/ |
12
|
|
|
class TypeAnnotatedObjectType extends MutableObjectType |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $className; |
18
|
|
|
|
19
|
|
|
public function __construct(string $className, array $config) |
20
|
|
|
{ |
21
|
|
|
$this->className = $className; |
22
|
|
|
|
23
|
|
|
parent::__construct($config); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function createFromAnnotatedClass(string $typeName, string $className, $annotatedObject, FieldsBuilderFactory $fieldsBuilderFactory, RecursiveTypeMapperInterface $recursiveTypeMapper): self |
27
|
|
|
{ |
28
|
|
|
return new self($className, [ |
29
|
|
|
'name' => $typeName, |
30
|
|
|
'fields' => function() use ($annotatedObject, $recursiveTypeMapper, $className, $fieldsBuilderFactory) { |
31
|
|
|
$parentClass = get_parent_class($className); |
32
|
|
|
$parentType = null; |
33
|
|
|
if ($parentClass !== false) { |
34
|
|
|
if ($recursiveTypeMapper->canMapClassToType($parentClass)) { |
35
|
|
|
$parentType = $recursiveTypeMapper->mapClassToType($parentClass, null); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$fieldProvider = $fieldsBuilderFactory->buildFieldsBuilder($recursiveTypeMapper); |
40
|
|
|
$fields = $fieldProvider->getFields($annotatedObject); |
41
|
|
|
if ($parentType !== null) { |
42
|
|
|
$fields = $parentType->getFields() + $fields; |
43
|
|
|
} |
44
|
|
|
return $fields; |
45
|
|
|
}, |
46
|
|
|
'interfaces' => function() use ($className, $recursiveTypeMapper) { |
47
|
|
|
return $recursiveTypeMapper->findInterfaces($className); |
48
|
|
|
} |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getMappedClassName(): string |
53
|
|
|
{ |
54
|
|
|
return $this->className; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|