DataIncludedHelper::addToIncludedArray()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 2
Metric Value
c 7
b 0
f 2
dl 0
loc 42
rs 8.439
cc 5
eloc 25
nc 8
nop 4
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
0 ignored issues
show
Unused Code introduced by
The call to DataIncludedHelper::addR...psToIncludedResources() has too many arguments starting with $attributes.

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.

Loading history...
159
                );
160
161
                if ($relationshipData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationshipData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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