forked from liip/metadata-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.php
More file actions
110 lines (92 loc) · 3.76 KB
/
Builder.php
File metadata and controls
110 lines (92 loc) · 3.76 KB
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
<?php
declare(strict_types=1);
namespace Liip\MetadataParser;
use Liip\MetadataParser\Exception\ParseException;
use Liip\MetadataParser\Metadata\ClassMetadata;
use Liip\MetadataParser\Metadata\PropertyType;
use Liip\MetadataParser\Metadata\PropertyTypeClass;
use Liip\MetadataParser\Metadata\PropertyTypeIterable;
use Liip\MetadataParser\Metadata\PropertyTypeUnion;
use Liip\MetadataParser\Reducer\PropertyReducerInterface;
/**
* Builder for class metadata.
*
* Future feature idea: In addition to the build method, this builder could
* also provide a whole hashmap of class name => metadata. It should then not
* set the metadata on the property types. This would allow to work with
* general model graphs that may include recursion.
*/
final class Builder
{
private Parser $parser;
private RecursionChecker $recursionChecker;
public function __construct(Parser $parser, RecursionChecker $recursionChecker)
{
$this->parser = $parser;
$this->recursionChecker = $recursionChecker;
}
/**
* Build the tree for the specified class.
*
* This tree is guaranteed to be a tree and can not be a graph with recursion.
*
* @param PropertyReducerInterface[] $reducers
*
* @throws ParseException
*/
public function build(string $className, array $reducers = []): ClassMetadata
{
$rawClassMetadataList = $this->parser->parse($className);
/** @var ClassMetadata[] $classMetadataList */
$classMetadataList = [];
foreach ($rawClassMetadataList as $rawClassMetadata) {
$classMetadataList[$rawClassMetadata->getClassName()] = PropertyReducer::reduce($rawClassMetadata, $reducers);
}
foreach ($classMetadataList as $classMetadata) {
$this->setDiscriminatorClassMetadata($classMetadata, $classMetadataList);
foreach ($classMetadata->getProperties() as $property) {
try {
$this->setTypeClassMetadata($property->getType(), $classMetadataList);
} catch (\UnexpectedValueException $e) {
throw ParseException::classNotParsed($e->getMessage(), (string) $classMetadata, (string) $property);
}
}
}
return $this->recursionChecker->check($classMetadataList[$className]);
}
/**
* @param ClassMetadata[] $classMetadataList
*
* @throws \UnexpectedValueException if the class is not found
*/
private function setTypeClassMetadata(PropertyType $type, array $classMetadataList): void
{
if ($type instanceof PropertyTypeClass) {
if (!\array_key_exists($type->getClassName(), $classMetadataList)) {
throw new \UnexpectedValueException($type->getClassName());
}
$type->setClassMetadata($classMetadataList[$type->getClassName()]);
return;
}
if ($type instanceof PropertyTypeIterable) {
$this->setTypeClassMetadata($type->getLeafType(), $classMetadataList);
}
if ($type instanceof PropertyTypeUnion) {
foreach ($type->getTypes() as $type) {
$this->setTypeClassMetadata($type, $classMetadataList);
}
}
}
private function setDiscriminatorClassMetadata(ClassMetadata $classMetadata, array $classMetadataList): void
{
if (null === $classMetadata->getDiscriminatorMetadata()) {
return;
}
$classes = array_values($classMetadata->getDiscriminatorMetadata()->classMap);
$discriminatorMetadataList = [];
foreach ($classes as $class) {
$discriminatorMetadataList[] = $classMetadataList[$class];
}
$classMetadata->getDiscriminatorMetadata()->setClassMetadataList($discriminatorMetadataList);
}
}