1 | <?php |
||
18 | class Normalizer |
||
19 | { |
||
20 | /** |
||
21 | * @var MetadataFactoryInterface |
||
22 | */ |
||
23 | private $metadataFactory; |
||
24 | |||
25 | /** |
||
26 | * @var PropertyAccessorInterface |
||
27 | */ |
||
28 | private $propertyAccessor; |
||
29 | |||
30 | /** |
||
31 | * @var UrlGeneratorInterface |
||
32 | */ |
||
33 | private $urlGenerator; |
||
34 | |||
35 | 74 | function __construct( |
|
44 | |||
45 | /** |
||
46 | * @param mixed $data |
||
47 | * @param string[] $includes |
||
48 | * @param int $depth |
||
49 | * @param string $path |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | 66 | public function normalize($data, $includes = [], int $depth = 0, string $path = '') |
|
54 | { |
||
55 | 66 | if (is_array($data)) { |
|
56 | 36 | $normalizedData = []; |
|
57 | 36 | foreach ($data as $datum) { |
|
58 | 36 | $normalizedData[] = $this->normalize($datum, $includes, $depth + 1, $path); |
|
59 | } |
||
60 | |||
61 | 36 | return $normalizedData; |
|
62 | } |
||
63 | |||
64 | 66 | if (is_object($data)) { |
|
65 | |||
66 | /** @var ClassMetadata $classMetadata */ |
||
67 | 66 | $classMetadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data)); |
|
68 | |||
69 | 66 | $normalizedData = []; |
|
70 | |||
71 | 66 | if ($classMetadata->isRestResource() && $classMetadata->hasMethod(Method::GET) && $this->isIncluded( |
|
72 | 50 | $path, |
|
73 | 50 | ['_links'], |
|
74 | 66 | $includes |
|
75 | ) |
||
76 | ) { |
||
77 | 2 | $selfLink = $this->urlGenerator->generate( |
|
78 | 2 | $classMetadata->namePrefix . '.get', |
|
79 | 2 | ['id' => $this->propertyAccessor->getValue($data, $classMetadata->getIdField())], |
|
80 | 2 | UrlGeneratorInterface::ABSOLUTE_URL |
|
81 | ); |
||
82 | 2 | $normalizedData['_links'] = [ |
|
83 | 'self' => [ |
||
84 | 2 | 'href' => $selfLink |
|
85 | ] |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | /** @var PropertyMetadata $propertyMetadatum */ |
||
90 | 66 | foreach ($classMetadata->propertyMetadata as $propertyMetadatum) { |
|
91 | |||
92 | 66 | if ($propertyMetadatum->isExcluded()) { |
|
93 | 40 | continue; |
|
94 | } |
||
95 | |||
96 | 66 | if ($propertyMetadatum->isAssociation()) { |
|
97 | |||
98 | /* Inlude if includable AND it is on include path */ |
||
99 | 48 | if ($propertyMetadatum->isIncludable() && $this->isIncluded( |
|
100 | 44 | $path, |
|
101 | 44 | $propertyMetadatum->getIncludablePaths(), |
|
102 | 48 | $includes |
|
103 | ) |
||
104 | ) { |
||
105 | 28 | $value = $this->propertyAccessor->getValue($data, $propertyMetadatum->name); |
|
106 | 28 | if ($propertyMetadatum->isCollection()) { |
|
107 | /** @var Collection $value */ |
||
108 | 14 | $value = $value->getValues(); |
|
109 | } |
||
110 | 28 | $normalizedData[$propertyMetadatum->name] = $this->normalize( |
|
111 | 28 | $value, |
|
112 | 28 | $includes, |
|
113 | 28 | $depth + 1, |
|
114 | 48 | $this->appendPath($path, $propertyMetadatum->name) |
|
115 | ); |
||
116 | } |
||
117 | } else { |
||
118 | |||
119 | /* Inlude if includable is missing OR it is on include path */ |
||
120 | 66 | if (!$propertyMetadatum->isIncludable() || $this->isIncluded( |
|
121 | 18 | $path, |
|
122 | 18 | $propertyMetadatum->getIncludablePaths(), |
|
123 | 66 | $includes |
|
124 | ) |
||
125 | ) { |
||
126 | 66 | $value = $this->propertyAccessor->getValue($data, $propertyMetadatum->name); |
|
127 | 66 | if (is_scalar($value) || array_key_exists($propertyMetadatum->getType(), Type::getTypesMap())) { |
|
128 | 66 | $normalizedData[$propertyMetadatum->name] = $this->normalizeField( |
|
129 | 66 | $value, |
|
130 | 66 | $propertyMetadatum |
|
131 | ); |
||
132 | } else { |
||
133 | 28 | $normalizedData[$propertyMetadatum->name] = $this->normalize( |
|
134 | 28 | $value, |
|
135 | 28 | $includes, |
|
136 | 28 | $depth + 1, |
|
137 | 66 | $this->appendPath($path, $propertyMetadatum->name) |
|
138 | ); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | 66 | return $normalizedData; |
|
145 | } |
||
146 | |||
147 | 22 | return $data; |
|
148 | } |
||
149 | |||
150 | 50 | private function isIncluded($currentPath, array $paths, ?array $includes): bool |
|
164 | |||
165 | 50 | private function appendPath($path, $name) |
|
173 | |||
174 | 66 | private function normalizeField($value, PropertyMetadata $propertyMetadata) |
|
205 | } |
||
206 |
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.