1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Factory; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\ComplexType; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema; |
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
14
|
|
|
use KleijnWeb\PhpApi\Descriptions\Hydrator\ProcessorBuilder; |
15
|
|
|
use KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Object\ObjectProcessor; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class ObjectFactory implements Factory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param Schema $schema |
24
|
|
|
* @param ProcessorBuilder $builder |
25
|
|
|
* |
26
|
|
|
* @return ObjectProcessor|null |
27
|
|
|
*/ |
28
|
|
|
public function create(Schema $schema, ProcessorBuilder $builder) |
29
|
|
|
{ |
30
|
|
|
if (!$this->supports($schema)) { |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @var ObjectSchema $objectSchema */ |
35
|
|
|
$objectSchema = $schema; |
36
|
|
|
$processor = $this->instantiate($objectSchema, $builder); |
37
|
|
|
|
38
|
|
|
// TODO: Handle multiple roots |
39
|
|
|
if ($objectSchema->hasComplexType()) { |
40
|
|
|
$root = $objectSchema->getComplexType(); |
41
|
|
|
|
42
|
|
|
// Recurse up to find root |
43
|
|
|
while ($parents = $root->getParents()) { |
44
|
|
|
$root = $parents[0]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Add schema of all children up to root |
48
|
|
|
$descend = function (ComplexType $complexType) use (&$descend, $builder, $processor) { |
49
|
|
|
|
50
|
|
View Code Duplication |
foreach ($complexType->getSchema()->getPropertySchemas() as $propertyName => $propertySchema) { |
|
|
|
|
51
|
|
|
if ($propertySchema instanceof ObjectSchema && $propertySchema->getXType() !== null && !$propertySchema->hasComplexType()) { |
52
|
|
|
throw new \LogicException("ComplexType never resolved"); |
53
|
|
|
} |
54
|
|
|
$propertyProcessor = $builder->build($propertySchema); |
55
|
|
|
|
56
|
|
|
$processor->setPropertyProcessor($propertyName, $propertyProcessor); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($complexType->getChildren() as $child) { |
60
|
|
|
$descend($child); |
61
|
|
|
} |
62
|
|
|
}; |
63
|
|
|
|
64
|
|
|
$descend($root); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
View Code Duplication |
foreach ($objectSchema->getPropertySchemas() as $propertyName => $propertySchema) { |
|
|
|
|
69
|
|
|
if ($propertySchema instanceof ObjectSchema && $propertySchema->getXType() !== null && !$propertySchema->hasComplexType()) { |
70
|
|
|
throw new \LogicException("ComplexType never resolved"); |
71
|
|
|
} |
72
|
|
|
$propertyProcessor = $builder->build($propertySchema); |
73
|
|
|
|
74
|
|
|
$processor->setPropertyProcessor($propertyName, $propertyProcessor); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $processor; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Schema $schema |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function supports(Schema $schema) |
86
|
|
|
{ |
87
|
|
|
return $schema instanceof ObjectSchema; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ObjectSchema $schema |
92
|
|
|
* @param ProcessorBuilder $builder |
93
|
|
|
* |
94
|
|
|
* @return ObjectProcessor |
95
|
|
|
*/ |
96
|
|
|
abstract protected function instantiate(ObjectSchema $schema, ProcessorBuilder $builder): ObjectProcessor; |
97
|
|
|
} |
98
|
|
|
|