2
2
3
3
namespace Swis \JsonApi \Client \JsonApi ;
4
4
5
- use Art4 \JsonApiClient \AccessInterface ;
5
+ use Art4 \JsonApiClient \ElementInterface ;
6
6
use Art4 \JsonApiClient \ResourceCollectionInterface ;
7
- use Art4 \JsonApiClient \ResourceCollectionInterface as JsonApiCollection ;
8
- use Art4 \JsonApiClient \ResourceIdentifierCollection as IdentifierCollection ;
9
7
use Art4 \JsonApiClient \ResourceIdentifierCollectionInterface ;
10
8
use Art4 \JsonApiClient \ResourceIdentifierInterface ;
11
- use Art4 \JsonApiClient \ResourceItemInterface as JsonApItem ;
9
+ use Art4 \JsonApiClient \ResourceItemInterface ;
12
10
use Swis \JsonApi \Client \Collection ;
13
11
use Swis \JsonApi \Client \Interfaces \ItemInterface ;
14
12
use Swis \JsonApi \Client \Interfaces \TypeMapperInterface ;
@@ -20,7 +18,7 @@ class Hydrator
20
18
/**
21
19
* @var \Swis\JsonApi\Client\Interfaces\TypeMapperInterface
22
20
*/
23
- private $ typeMapper ;
21
+ protected $ typeMapper ;
24
22
25
23
/**
26
24
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface $typeMapper
@@ -30,62 +28,37 @@ public function __construct(TypeMapperInterface $typeMapper)
30
28
$ this ->typeMapper = $ typeMapper ;
31
29
}
32
30
33
- /**
34
- * @param \Art4\JsonApiClient\ResourceCollectionInterface $jsonApiCollection
35
- *
36
- * @return \Swis\JsonApi\Client\Collection
37
- */
38
- public function hydrateCollection (JsonApiCollection $ jsonApiCollection )
39
- {
40
- $ collection = new Collection ();
41
- foreach ($ jsonApiCollection ->asArray () as $ item ) {
42
- $ collection ->push ($ this ->hydrateItem ($ item ));
43
- }
44
-
45
- return $ collection ;
46
- }
47
-
48
31
/**
49
32
* @param \Art4\JsonApiClient\ResourceItemInterface $jsonApiItem
50
33
*
51
34
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface
52
35
*/
53
- public function hydrateItem (JsonApItem $ jsonApiItem )
36
+ public function hydrateItem (ResourceItemInterface $ jsonApiItem ): ItemInterface
54
37
{
55
- $ item = $ this ->getItemClass ($ jsonApiItem );
38
+ $ item = $ this ->getItemClass ($ jsonApiItem-> get ( ' type ' ) );
56
39
57
- $ item ->setType ($ jsonApiItem ->get ('type ' ))
58
- ->setId ($ jsonApiItem ->get ('id ' ));
40
+ $ item ->setId ($ jsonApiItem ->get ('id ' ));
59
41
60
- $ this ->hydrateAttributes ($ jsonApiItem , $ item );
42
+ if ($ jsonApiItem ->has ('attributes ' )) {
43
+ $ item ->fill ($ jsonApiItem ->get ('attributes ' )->asArray (true ));
44
+ }
61
45
62
46
return $ item ;
63
47
}
64
48
65
49
/**
66
- * @param \Art4\JsonApiClient\ResourceItemInterface $jsonApiItem
50
+ * @param \Art4\JsonApiClient\ResourceCollectionInterface $jsonApiCollection
67
51
*
68
- * @return \Swis\JsonApi\Client\Interfaces\ItemInterface
52
+ * @return \Swis\JsonApi\Client\Collection
69
53
*/
70
- protected function getItemClass ( JsonApItem $ jsonApiItem ): ItemInterface
54
+ public function hydrateCollection ( ResourceCollectionInterface $ jsonApiCollection ): Collection
71
55
{
72
- $ type = $ jsonApiItem -> get ( ' type ' );
73
- if ($ this -> typeMapper -> hasMapping ( $ type ) ) {
74
- return $ this -> typeMapper -> getMapping ( $ type );
56
+ $ collection = new Collection ( );
57
+ foreach ($ jsonApiCollection -> asArray () as $ item ) {
58
+ $ collection -> push ( $ this -> hydrateItem ( $ item ) );
75
59
}
76
60
77
- return new JenssegersItem ();
78
- }
79
-
80
- /**
81
- * @param \Art4\JsonApiClient\ResourceItemInterface $jsonApiItem
82
- * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
83
- */
84
- protected function hydrateAttributes (JsonApItem $ jsonApiItem , ItemInterface $ item )
85
- {
86
- if ($ jsonApiItem ->has ('attributes ' )) {
87
- $ item ->fill ($ jsonApiItem ->get ('attributes ' )->asArray (true ));
88
- }
61
+ return $ collection ;
89
62
}
90
63
91
64
/**
@@ -94,35 +67,39 @@ protected function hydrateAttributes(JsonApItem $jsonApiItem, ItemInterface $ite
94
67
*/
95
68
public function hydrateRelationships (Collection $ jsonApiItems , Collection $ items )
96
69
{
70
+ $ keyedItems = $ items ->keyBy (
71
+ function (ItemInterface $ item ) {
72
+ return $ this ->getItemKey ($ item );
73
+ }
74
+ );
75
+
97
76
$ jsonApiItems ->each (
98
- function (JsonApItem $ jsonApiItem ) use ($ items ) {
77
+ function (ResourceItemInterface $ jsonApiItem ) use ($ keyedItems ) {
99
78
if (!$ jsonApiItem ->has ('relationships ' )) {
100
79
return ;
101
80
}
102
81
103
- $ item = $ this ->getIncludedItem ( $ items , $ jsonApiItem );
82
+ $ item = $ this ->getItem ( $ keyedItems , $ jsonApiItem );
104
83
105
84
if ($ item instanceof NullItem) {
106
85
return ;
107
86
}
108
87
109
- $ relationships = $ this ->getJsonApiDocumentRelationships ($ jsonApiItem );
110
-
111
- foreach ($ relationships as $ name => $ relationship ) {
112
- /** @var \Art4\JsonApiClient\ResourceItemInterface $data */
88
+ foreach ($ jsonApiItem ->get ('relationships ' )->asArray () as $ name => $ relationship ) {
89
+ /** @var \Art4\JsonApiClient\ElementInterface $data */
113
90
$ data = $ relationship ->get ('data ' );
114
91
$ method = camel_case ($ name );
115
92
116
93
if ($ data instanceof ResourceIdentifierInterface) {
117
- $ includedItem = $ this ->getIncludedItem ( $ items , $ data );
94
+ $ includedItem = $ this ->getItem ( $ keyedItems , $ data );
118
95
119
96
if ($ includedItem instanceof NullItem) {
120
97
continue ;
121
98
}
122
99
123
100
$ item ->setRelation ($ method , $ includedItem );
124
- } elseif ($ data instanceof ResourceCollectionInterface || $ data instanceof ResourceIdentifierCollectionInterface) {
125
- $ collection = $ this ->getIncludedItems ( $ items , $ data );
101
+ } elseif ($ data instanceof ResourceIdentifierCollectionInterface) {
102
+ $ collection = $ this ->getCollection ( $ keyedItems , $ data );
126
103
127
104
$ item ->setRelation ($ method , $ collection );
128
105
}
@@ -132,72 +109,75 @@ function (JsonApItem $jsonApiItem) use ($items) {
132
109
}
133
110
134
111
/**
135
- * @param \Art4\JsonApiClient\ResourceItemInterface $jsonApiItem
112
+ * @param string $type
136
113
*
137
- * @return \Art4\JsonApiClient\Relationship[]
114
+ * @return \Swis\JsonApi\Client\Interfaces\ItemInterface
138
115
*/
139
- protected function getJsonApiDocumentRelationships ( JsonApItem $ jsonApiItem ): array
116
+ protected function getItemClass ( string $ type ): ItemInterface
140
117
{
141
- return $ jsonApiItem ->get ('relationships ' )->asArray (false );
118
+ if ($ this ->typeMapper ->hasMapping ($ type )) {
119
+ return $ this ->typeMapper ->getMapping ($ type );
120
+ }
121
+
122
+ return (new JenssegersItem ())->setType ($ type );
142
123
}
143
124
144
125
/**
145
- * @param \Swis\JsonApi\Client\Collection $included
146
- * @param \Art4\JsonApiClient\AccessInterface $accessor
126
+ * @param \Swis\JsonApi\Client\Collection $included
127
+ * @param \Art4\JsonApiClient\ResourceIdentifierInterface|\Art4\JsonApiClient\ResourceItemInterface $identifier
147
128
*
148
129
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface
149
130
*/
150
- protected function getIncludedItem (Collection $ included , AccessInterface $ accessor ): ItemInterface
131
+ protected function getItem (Collection $ included , $ identifier ): ItemInterface
151
132
{
152
- return $ included ->first (
153
- function ( ItemInterface $ item ) use ( $ accessor ) {
154
- return $ this -> accessorBelongsToItem ( $ accessor , $ item );
155
- },
156
- new NullItem ()
133
+ return $ included ->get (
134
+ $ this -> getElementKey ( $ identifier ),
135
+ function () {
136
+ return new NullItem ();
137
+ }
157
138
);
158
139
}
159
140
160
141
/**
161
- * @param \Art4\JsonApiClient\AccessInterface $accessor
162
- * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
142
+ * @param \Swis\JsonApi\Client\Collection $included
143
+ * @param \Art4\JsonApiClient\ResourceIdentifierCollectionInterface|\Art4\JsonApiClient\ResourceCollectionInterface $identifierCollection
163
144
*
164
- * @return bool
145
+ * @return \Swis\JsonApi\Client\Collection
165
146
*/
166
- protected function accessorBelongsToItem ( AccessInterface $ accessor , ItemInterface $ item ): bool
147
+ protected function getCollection ( Collection $ included , $ identifierCollection ): Collection
167
148
{
168
- return $ item ->getType () === $ accessor ->get ('type ' )
169
- && (string )$ item ->getId () === $ accessor ->get ('id ' );
149
+ $ items = new Collection ();
150
+
151
+ foreach ($ identifierCollection ->asArray () as $ identifier ) {
152
+ $ item = $ this ->getItem ($ included , $ identifier );
153
+
154
+ if ($ item instanceof NullItem) {
155
+ continue ;
156
+ }
157
+
158
+ $ items ->push ($ item );
159
+ }
160
+
161
+ return $ items ;
170
162
}
171
163
172
164
/**
173
- * @param \Swis\JsonApi\Client\Collection $included
174
- * @param \Art4\JsonApiClient\ResourceIdentifierCollection $collection
165
+ * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
175
166
*
176
- * @return \Swis\JsonApi\Client\Collection
167
+ * @return string
177
168
*/
178
- protected function getIncludedItems ( Collection $ included , IdentifierCollection $ collection ): Collection
169
+ protected function getItemKey ( ItemInterface $ item ): string
179
170
{
180
- return $ included ->filter (
181
- function (ItemInterface $ item ) use ($ collection ) {
182
- return $ this ->itemExistsInRelatedIdentifiers ($ collection ->asArray (false ), $ item );
183
- }
184
- )->values ();
171
+ return sprintf ('%s:%s ' , $ item ->getType (), $ item ->getId ());
185
172
}
186
173
187
174
/**
188
- * @param \Art4\JsonApiClient\ResourceIdentifier[] $relatedIdentifiers
189
- * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
175
+ * @param \Art4\JsonApiClient\ElementInterface $accessor
190
176
*
191
- * @return bool
177
+ * @return string
192
178
*/
193
- protected function itemExistsInRelatedIdentifiers ( array $ relatedIdentifiers , ItemInterface $ item ): bool
179
+ protected function getElementKey ( ElementInterface $ accessor ): string
194
180
{
195
- foreach ($ relatedIdentifiers as $ relatedIdentifier ) {
196
- if ($ this ->accessorBelongsToItem ($ relatedIdentifier , $ item )) {
197
- return true ;
198
- }
199
- }
200
-
201
- return false ;
181
+ return sprintf ('%s:%s ' , $ accessor ->get ('type ' ), $ accessor ->get ('id ' ));
202
182
}
203
183
}
0 commit comments