1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 7/25/15 |
6
|
|
|
* Time: 5:15 PM. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace NilPortugues\Api\JsonApi\Helpers; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Api\JsonApi\JsonApiTransformer; |
14
|
|
|
use NilPortugues\Serializer\Serializer; |
15
|
|
|
|
16
|
|
|
class DataIncludedHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings |
20
|
|
|
* @param array $array |
21
|
|
|
* @param array $data |
22
|
|
|
* @param null $parentType |
23
|
|
|
*/ |
24
|
|
|
public static function setResponseDataIncluded(array &$mappings, array $array, array &$data, $parentType = null) |
25
|
|
|
{ |
26
|
|
|
$parentType = (null === $parentType) ? $array[Serializer::CLASS_IDENTIFIER_KEY] : $parentType; |
27
|
|
|
|
28
|
|
|
foreach (self::removeTypeAndId($mappings, $array) as $value) { |
29
|
|
|
if (\is_array($value)) { |
30
|
|
|
if (\array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $value)) { |
31
|
|
|
$attributes = []; |
32
|
|
|
$relationships = []; |
33
|
|
|
$type = $value[Serializer::CLASS_IDENTIFIER_KEY]; |
34
|
|
|
|
35
|
|
|
self::addToRelationshipsArray($mappings, $data, $value, $type, $relationships, $attributes); |
36
|
|
|
self::addToIncludedArray($mappings, $data, $attributes, $value); |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (\is_array($value)) { |
41
|
|
|
foreach ($value as $inArrayValue) { |
42
|
|
|
if (\is_array($inArrayValue)) { |
43
|
|
|
|
44
|
|
|
//Remove those resources that do not to appear in the getIncludedResources array. |
45
|
|
|
foreach ($inArrayValue as $position => $includableValue) { |
46
|
|
|
if ($mappings[$parentType]->isFilteringIncludedResources() |
47
|
|
|
&& false === in_array( |
48
|
|
|
$includableValue[Serializer::CLASS_IDENTIFIER_KEY], |
49
|
|
|
$mappings[$parentType]->getIncludedResources(), |
50
|
|
|
true |
51
|
|
|
) |
52
|
|
|
) { |
53
|
|
|
unset($inArrayValue[$position]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
self::setResponseDataIncluded($mappings, $inArrayValue, $data, $parentType); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings |
67
|
|
|
* @param array $copy |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected static function removeTypeAndId(array &$mappings, array $copy) |
72
|
|
|
{ |
73
|
|
|
if (!empty($copy[Serializer::CLASS_IDENTIFIER_KEY])) { |
74
|
|
|
$type = $copy[Serializer::CLASS_IDENTIFIER_KEY]; |
75
|
|
|
|
76
|
|
|
if (\is_scalar($type)) { |
77
|
|
|
foreach ($mappings[$type]->getIdProperties() as $propertyName) { |
78
|
|
|
unset($copy[$propertyName]); |
79
|
|
|
} |
80
|
|
|
unset($copy[Serializer::CLASS_IDENTIFIER_KEY]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $copy; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings |
89
|
|
|
* @param array $data |
90
|
|
|
* @param array $value |
91
|
|
|
* @param string $type |
92
|
|
|
* @param array $relationships |
93
|
|
|
* @param array $attributes |
94
|
|
|
*/ |
95
|
|
|
protected static function addToRelationshipsArray( |
96
|
|
|
array &$mappings, |
97
|
|
|
array &$data, |
98
|
|
|
array &$value, |
99
|
|
|
$type, |
100
|
|
|
array &$relationships, |
101
|
|
|
array &$attributes |
102
|
|
|
) { |
103
|
|
|
foreach ($value as $propertyName => $attribute) { |
104
|
|
|
if (PropertyHelper::isAttributeProperty($mappings, $propertyName, $type)) { |
105
|
|
|
$propertyName = DataAttributesHelper::transformToValidMemberName($propertyName); |
106
|
|
|
|
107
|
|
|
if (\array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $attribute)) { |
108
|
|
|
self::setResponseDataIncluded($mappings, $value, $data); |
109
|
|
|
|
110
|
|
|
$relationships[$propertyName] = \array_merge( |
111
|
|
|
DataLinksHelper::setResponseDataLinks($mappings, $attribute), |
112
|
|
|
[ |
113
|
|
|
JsonApiTransformer::DATA_KEY => [ |
114
|
|
|
$propertyName => PropertyHelper::setResponseDataTypeAndId( |
115
|
|
|
$mappings, |
116
|
|
|
$attribute |
117
|
|
|
), |
118
|
|
|
], |
119
|
|
|
], |
120
|
|
|
$mappings[$type]->getRelationships() |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
$attributes[$propertyName] = $attribute; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings |
132
|
|
|
* @param array $data |
133
|
|
|
* @param array $attributes |
134
|
|
|
* @param array $value |
135
|
|
|
*/ |
136
|
|
|
protected static function addToIncludedArray(array &$mappings, array &$data, array &$attributes, array &$value) |
137
|
|
|
{ |
138
|
|
|
if (\count($attributes) > 0) { |
139
|
|
|
$includedData = PropertyHelper::setResponseDataTypeAndId($mappings, $value); |
140
|
|
|
|
141
|
|
|
if (self::hasIdKey($includedData)) { |
142
|
|
|
$arrayData = \array_merge( |
143
|
|
|
[ |
144
|
|
|
JsonApiTransformer::TYPE_KEY => $includedData[JsonApiTransformer::TYPE_KEY], |
145
|
|
|
JsonApiTransformer::ID_KEY => $includedData[JsonApiTransformer::ID_KEY], |
146
|
|
|
JsonApiTransformer::ATTRIBUTES_KEY => $attributes, |
147
|
|
|
JsonApiTransformer::RELATIONSHIPS_KEY => [], |
148
|
|
|
], |
149
|
|
|
DataLinksHelper::setResponseDataLinks($mappings, $value) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$relationshipData = []; |
153
|
|
|
self::addRelationshipsToIncludedResources( |
154
|
|
|
$mappings, |
155
|
|
|
$relationshipData, |
156
|
|
|
$value, |
157
|
|
|
$value[Serializer::CLASS_IDENTIFIER_KEY], |
158
|
|
|
$attributes |
|
|
|
|
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
if ($relationshipData) { |
|
|
|
|
162
|
|
|
$arrayData[JsonApiTransformer::RELATIONSHIPS_KEY] = \array_merge( |
163
|
|
|
$arrayData[JsonApiTransformer::RELATIONSHIPS_KEY], |
164
|
|
|
$relationshipData |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$data[JsonApiTransformer::INCLUDED_KEY][] = \array_filter($arrayData); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!empty($data[JsonApiTransformer::INCLUDED_KEY])) { |
173
|
|
|
$data[JsonApiTransformer::INCLUDED_KEY] = \array_values( |
174
|
|
|
\array_unique($data[JsonApiTransformer::INCLUDED_KEY], SORT_REGULAR) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $mappings |
181
|
|
|
* @param array $data |
182
|
|
|
* @param array $value |
183
|
|
|
* @param string $type |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
protected static function addRelationshipsToIncludedResources( |
|
|
|
|
186
|
|
|
array &$mappings, |
187
|
|
|
array &$data, |
188
|
|
|
array &$value, |
189
|
|
|
$type |
190
|
|
|
) { |
191
|
|
|
foreach ($value as $propertyName => $attribute) { |
192
|
|
|
if (PropertyHelper::isAttributeProperty($mappings, $propertyName, $type)) { |
193
|
|
|
$propertyName = DataAttributesHelper::transformToValidMemberName($propertyName); |
194
|
|
|
|
195
|
|
|
if (\is_array($attribute) && \array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $attribute)) { |
196
|
|
|
$data[$propertyName][JsonApiTransformer::DATA_KEY] = PropertyHelper::setResponseDataTypeAndId( |
197
|
|
|
$mappings, |
198
|
|
|
$attribute |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
continue; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param array $includedData |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
protected static function hasIdKey(array &$includedData) |
213
|
|
|
{ |
214
|
|
|
return \array_key_exists(JsonApiTransformer::ID_KEY, $includedData) |
215
|
|
|
&& !empty($includedData[JsonApiTransformer::ID_KEY]); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.