1 | <?php |
||
13 | class Normalizer |
||
14 | { |
||
15 | /** |
||
16 | * @var MetadataFactoryInterface |
||
17 | */ |
||
18 | private $metadataFactory; |
||
19 | |||
20 | /** |
||
21 | * @var PropertyAccessorInterface |
||
22 | */ |
||
23 | private $propertyAccessor; |
||
24 | |||
25 | 12 | function __construct(MetadataFactoryInterface $metadataFactory, PropertyAccessorInterface $propertyAccessor) |
|
30 | |||
31 | /** |
||
32 | * @param mixed $data |
||
33 | * @param string[] $includes |
||
34 | * @param int $depth |
||
35 | * @param string $path |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | 12 | public function normalize($data, $includes = [], int $depth = 0, string $path = '') |
|
40 | { |
||
41 | 12 | if (is_array($data)) { |
|
42 | 7 | $normalizedData = []; |
|
43 | 7 | foreach ($data as $datum) { |
|
44 | 7 | $normalizedData[] = $this->normalize($datum, $includes, $depth + 1, $path); |
|
45 | } |
||
46 | |||
47 | 7 | return $normalizedData; |
|
48 | } |
||
49 | |||
50 | 12 | if (is_object($data)) { |
|
51 | |||
52 | 12 | $normalizedData = []; |
|
53 | |||
54 | /** @var ClassMetadata $classMetadata */ |
||
55 | 12 | $classMetadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data)); |
|
56 | |||
57 | /** @var PropertyMetadata $propertyMetadatum */ |
||
58 | 12 | foreach ($classMetadata->propertyMetadata as $propertyMetadatum) { |
|
59 | |||
60 | 12 | if ($propertyMetadatum->isExcluded()) { |
|
61 | 6 | continue; |
|
62 | } |
||
63 | |||
64 | 12 | if ($propertyMetadatum->isAssociation()) { |
|
65 | |||
66 | /* Inlude if includable AND it is on include path */ |
||
67 | 9 | if ($propertyMetadatum->isIncludable() && $this->isIncluded( |
|
68 | $path, |
||
69 | 9 | $propertyMetadatum->getIncludablePaths(), |
|
70 | $includes |
||
71 | ) |
||
72 | ) { |
||
73 | 2 | $value = $this->propertyAccessor->getValue($data, $propertyMetadatum->name); |
|
74 | 2 | if ($propertyMetadatum->isCollection()) { |
|
75 | /** @var Collection $value */ |
||
76 | 1 | $value = $value->getValues(); |
|
77 | } |
||
78 | 2 | $normalizedData[$propertyMetadatum->name] = $this->normalize( |
|
79 | $value, |
||
80 | $includes, |
||
81 | 2 | $depth + 1, |
|
82 | 9 | $this->appendPath($path, $propertyMetadatum->name) |
|
83 | ); |
||
84 | } |
||
85 | } else { |
||
86 | |||
87 | /* Inlude if includable is missing OR it is on include path */ |
||
88 | 12 | if (!$propertyMetadatum->isIncludable() || $this->isIncluded( |
|
89 | $path, |
||
90 | 12 | $propertyMetadatum->getIncludablePaths(), |
|
91 | $includes |
||
92 | ) |
||
93 | ) { |
||
94 | 12 | $value = $this->propertyAccessor->getValue($data, $propertyMetadatum->name); |
|
95 | 12 | if (!array_key_exists($propertyMetadatum->getType(), Type::getTypesMap())) { |
|
96 | 5 | $normalizedData[$propertyMetadatum->name] = $this->normalize( |
|
97 | $value, |
||
98 | $includes, |
||
99 | 5 | $depth + 1, |
|
100 | 5 | $this->appendPath($path, $propertyMetadatum->name) |
|
101 | ); |
||
102 | } else { |
||
103 | 12 | $normalizedData[$propertyMetadatum->name] = $this->normalizeField( |
|
104 | $value, |
||
105 | $propertyMetadatum |
||
106 | ); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | 12 | return $normalizedData; |
|
113 | } |
||
114 | |||
115 | return null; |
||
116 | } |
||
117 | |||
118 | 9 | private function isIncluded($currentPath, array $paths, ?array $includes): bool |
|
132 | |||
133 | 9 | private function appendPath($path, $name) |
|
141 | |||
142 | 12 | private function normalizeField($value, PropertyMetadata $propertyMetadata) |
|
173 | } |
||
174 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.