1 | <?php |
||
11 | class Normalizer |
||
12 | { |
||
13 | /** |
||
14 | * @var MetadataFactoryInterface |
||
15 | */ |
||
16 | private $metadataFactory; |
||
17 | |||
18 | 14 | function __construct(MetadataFactoryInterface $metadataFactory) |
|
22 | |||
23 | /** |
||
24 | * @param mixed $data |
||
25 | * @param string[] $includes |
||
26 | * @param int $depth |
||
27 | * @param string $path |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | 14 | public function normalize($data, $includes = [], int $depth = 0, string $path = '') |
|
32 | { |
||
33 | 14 | if (is_array($data)) { |
|
34 | 8 | $normalizedData = []; |
|
35 | 8 | foreach ($data as $datum) { |
|
36 | 8 | $normalizedData[] = $this->normalize($datum, $includes, $depth + 1, $path); |
|
37 | } |
||
38 | |||
39 | 8 | return $normalizedData; |
|
40 | } |
||
41 | |||
42 | 14 | if (is_object($data)) { |
|
43 | |||
44 | 14 | $normalizedData = []; |
|
45 | |||
46 | /** @var ClassMetadata $classMetadata */ |
||
47 | 14 | $classMetadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data)); |
|
48 | |||
49 | /** @var PropertyMetadata $propertyMetadatum */ |
||
50 | 14 | foreach ($classMetadata->propertyMetadata as $propertyMetadatum) { |
|
51 | |||
52 | 14 | if ($propertyMetadatum->isExcluded()) { |
|
53 | 8 | continue; |
|
54 | } |
||
55 | |||
56 | 14 | if ($propertyMetadatum->isAssociation()) { |
|
57 | |||
58 | /* Inlude if includable AND it is on include path */ |
||
59 | 10 | if ($propertyMetadatum->isIncludable() && $this->isIncluded( |
|
60 | $path, |
||
61 | 10 | $propertyMetadatum->getIncludablePaths(), |
|
62 | $includes |
||
63 | ) |
||
64 | ) { |
||
65 | 2 | $value = $propertyMetadatum->getValue($data); |
|
66 | 2 | if ($propertyMetadatum->isCollection()) { |
|
67 | /** @var Collection $value */ |
||
68 | 2 | $value = $value->getValues(); |
|
69 | } |
||
70 | 2 | $normalizedData[$propertyMetadatum->name] = $this->normalize( |
|
71 | $value, |
||
72 | $includes, |
||
73 | 2 | $depth + 1, |
|
74 | 10 | $this->appendPath($path, $propertyMetadatum->name) |
|
75 | ); |
||
76 | } |
||
77 | } else { |
||
78 | |||
79 | /* Inlude if includable is missing OR it is on include path */ |
||
80 | 14 | if (!$propertyMetadatum->isIncludable() || $this->isIncluded( |
|
81 | $path, |
||
82 | 14 | $propertyMetadatum->getIncludablePaths(), |
|
83 | $includes |
||
84 | ) |
||
85 | ) { |
||
86 | 14 | $value = $propertyMetadatum->getValue($data); |
|
87 | 14 | $normalizedData[$propertyMetadatum->name] = $this->normalizeField($value, $propertyMetadatum); |
|
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | 14 | return $normalizedData; |
|
93 | } |
||
94 | |||
95 | return null; |
||
96 | } |
||
97 | |||
98 | 8 | private function isIncluded($currentPath, array $paths, ?array $includes): bool |
|
99 | { |
||
100 | 8 | if (null === $includes) { |
|
101 | return false; |
||
102 | } |
||
103 | |||
104 | 8 | foreach ($paths as $path) { |
|
105 | 8 | if (in_array($this->appendPath($currentPath, $path), $includes)) { |
|
106 | 8 | return true; |
|
107 | } |
||
108 | } |
||
109 | |||
110 | 6 | return false; |
|
111 | } |
||
112 | |||
113 | 8 | private function appendPath($path, $name) |
|
114 | { |
||
115 | 8 | if (null === $path || '' === $path) { |
|
116 | 8 | return $name; |
|
117 | } |
||
118 | |||
119 | return $path . '.' . $name; |
||
120 | } |
||
121 | |||
122 | 14 | private function normalizeField($value, PropertyMetadata $propertyMetadata) |
|
153 | } |
||
154 |
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.