-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDataAttributesHelper.php
234 lines (205 loc) · 6.67 KB
/
DataAttributesHelper.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Author: Nil Portugués Calderó <[email protected]>
* Date: 7/25/15
* Time: 5:05 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Api\JsonApi\Helpers;
use NilPortugues\Api\JsonApi\JsonApiTransformer;
use NilPortugues\Api\Transformer\Helpers\RecursiveFormatterHelper;
use NilPortugues\Serializer\Serializer;
/**
* Class DataAttributesHelper.
*/
class DataAttributesHelper
{
/**
* @link http://jsonapi.org/format/#document-member-names-reserved-characters
*
* @var array
*/
protected static $forbiddenMemberNameCharacters = [
'+',
',',
'.',
'[',
']',
'!',
'"',
'#',
'$',
'%',
'&',
'\'',
'(',
')',
'*',
'/',
':',
';',
'<',
'=',
'>',
'?',
'@',
'\\',
'^',
'`',
'{',
'|',
'}',
'~',
];
/**
* @link http://jsonapi.org/format/#document-member-names-allowed-characters
*
* @var array
*/
protected static $forbiddenAsFirstOrLastCharacter = [
'-',
'_',
' ',
];
/**
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings
* @param array $array
* @param string $attributesCase
*
* @return array
*/
public static function setResponseDataAttributes(array &$mappings, array &$array, string $attributesCase)
{
$attributes = [];
$type = $array[Serializer::CLASS_IDENTIFIER_KEY];
$idProperties = RecursiveFormatterHelper::getIdProperties($mappings, $type);
foreach ($array as $propertyName => $value) {
if ($attributesCase == 'snake_case') {
$propertyName = RecursiveFormatterHelper::camelCaseToUnderscore($propertyName);
}
$keyName = self::transformToValidMemberName($propertyName);
if (\in_array($propertyName, $idProperties, true)) {
self::addIdPropertiesInAttribute($mappings, $type, $keyName, $value, $attributes);
continue;
}
if (!empty($value[Serializer::CLASS_IDENTIFIER_KEY])
&& empty($mappings[$value[Serializer::CLASS_IDENTIFIER_KEY]])
) {
$copy = $value;
if ($attributesCase == 'snake_case') {
self::recursiveSetKeysToUnderScore($copy);
}
$attributes[$keyName] = $copy;
continue;
}
if (self::isScalarValue($value) && empty($mappings[$value[Serializer::SCALAR_TYPE]])) {
$attributes[$keyName] = $value;
continue;
}
if (\is_array($value) && !array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $value)) {
if (false === self::hasClassIdentifierKey($value)) {
$attributes[$keyName] = $value;
}
}
}
//Guarantee it always returns the same attribute order. No matter what.
ksort($attributes, SORT_STRING);
return [JsonApiTransformer::ATTRIBUTES_KEY => $attributes];
}
/**
* @param string $attributeName
*
* @return string
*/
public static function transformToValidMemberName($attributeName)
{
$attributeName = \str_replace(self::$forbiddenMemberNameCharacters, '', $attributeName);
$attributeName = \ltrim($attributeName, \implode('', self::$forbiddenAsFirstOrLastCharacter));
$attributeName = \rtrim($attributeName, \implode('', self::$forbiddenAsFirstOrLastCharacter));
return $attributeName;
}
/**
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings
* @param string $type
* @param string $keyName
* @param array $value
* @param array $attributes
*/
protected static function addIdPropertiesInAttribute(array &$mappings, $type, $keyName, array $value, array &$attributes)
{
$keepKeys = str_replace(
array_values($mappings[$type]->getAliasedProperties()),
array_keys($mappings[$type]->getAliasedProperties()),
$mappings[$type]->getFilterKeys()
);
$keepIdKeys = (0 === count($keepKeys));
if (false !== array_search($keyName, $keepKeys, true)) {
$keepIdKeys = false;
}
if ($keepIdKeys) {
$ids = PropertyHelper::getIdValues($mappings, $value, $type);
if (count($ids) > 0) {
if (1 === count($ids)) {
$ids = array_pop($ids);
}
$attributes[$keyName] = $ids;
} else {
RecursiveFormatterHelper::formatScalarValues($value);
$attributes[$keyName] = $value;
}
}
}
/**
* Changes all array keys to under_score format using recursion.
*
* @param array $array
*/
protected static function recursiveSetKeysToUnderScore(array &$array)
{
$newArray = [];
foreach ($array as $key => &$value) {
$underscoreKey = RecursiveFormatterHelper::camelCaseToUnderscore($key);
$newArray[$underscoreKey] = $value;
if (\is_array($value)) {
self::recursiveSetKeysToUnderScore($newArray[$underscoreKey]);
}
}
$array = $newArray;
}
/**
* @param mixed $value
*
* @return bool
*/
protected static function isScalarValue($value)
{
return \is_array($value)
&& \array_key_exists(Serializer::SCALAR_TYPE, $value)
&& \array_key_exists(Serializer::SCALAR_VALUE, $value);
}
/**
* @param array $input
*
* @return bool
*/
protected static function hasClassIdentifierKey(array $input)
{
if (!empty($input[Serializer::CLASS_IDENTIFIER_KEY])) {
return true;
}
$foundIdentifierKey = false;
if (!empty($input[Serializer::SCALAR_VALUE]) && !empty($input[Serializer::MAP_TYPE])) {
$input = $input[Serializer::SCALAR_VALUE];
if (\is_array($input)) {
foreach ($input as $value) {
if (\is_array($value)) {
$foundIdentifierKey = $foundIdentifierKey || self::hasClassIdentifierKey($value);
}
}
}
}
return $foundIdentifierKey;
}
}