Skip to content

Commit 9b19988

Browse files
author
Nil Portugues Caldero
committed
WIP with some fixes
1 parent 5a2790b commit 9b19988

6 files changed

+41
-54
lines changed

phpunit.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"
99
convertWarningsToExceptions="true"
10-
stopOnError="false"
11-
stopOnFailure="false"
10+
stopOnError="true"
11+
stopOnFailure="true"
1212
stopOnIncomplete="false"
1313
stopOnSkipped="false"
1414
syntaxCheck="true"

src/Helpers/DataIncludedHelper.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,9 @@ public static function setResponseDataIncluded(array &$mappings, array $array, a
4343

4444
//Remove those resources that do not to appear in the getIncludedResources array.
4545
foreach ($inArrayValue as $position => $includableValue) {
46-
if (!empty($mappings[$parentType]) && $mappings[$parentType]->isFilteringIncludedResources()
47-
&& false === in_array(
48-
$includableValue[Serializer::CLASS_IDENTIFIER_KEY],
49-
$mappings[$parentType]->getIncludedResources(),
50-
true
51-
)
46+
if (!empty($mappings[$parentType])
47+
&& count($mappings[$parentType]->getIncludedResources()) > 0
48+
&& false === in_array($includableValue[Serializer::CLASS_IDENTIFIER_KEY], $mappings[$parentType]->getIncludedResources(), true)
5249
) {
5350
unset($inArrayValue[$position]);
5451
}

src/Helpers/DataLinksHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ protected static function relationshipLinksRelated($propertyName, array &$mappin
287287
*
288288
* @return mixed
289289
*/
290-
protected static function buildUrl(array &$mappings, $idProperties, $idValues, $url, $type)
290+
public static function buildUrl(array &$mappings, $idProperties, $idValues, $url, $type)
291291
{
292292
self::removeArraysFromKeyValueReplacement($idProperties, $idValues);
293293

src/JsonApiTransformer.php

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use NilPortugues\Api\JsonApi\Helpers\PropertyHelper;
99
use NilPortugues\Api\Transformer\Helpers\RecursiveDeleteHelper;
1010
use NilPortugues\Api\Transformer\Helpers\RecursiveFilterHelper;
11+
use NilPortugues\Api\Transformer\Helpers\RecursiveFormatterHelper;
1112
use NilPortugues\Api\Transformer\Helpers\RecursiveRenamerHelper;
1213
use NilPortugues\Api\Transformer\Transformer;
1314
use NilPortugues\Serializer\Serializer;
@@ -122,6 +123,7 @@ protected function setResponseLinks(array $value, array &$data)
122123
)
123124
);
124125

126+
125127
if (!empty($value[Serializer::CLASS_IDENTIFIER_KEY])) {
126128
$type = $value[Serializer::CLASS_IDENTIFIER_KEY];
127129

@@ -134,12 +136,37 @@ protected function setResponseLinks(array $value, array &$data)
134136
(!empty($urls)) ? $this->addHrefToLinks($this->getResponseAdditionalLinks($value, $type)) : []
135137
)
136138
);
139+
140+
141+
/*
142+
* Adds the _links:self:href link to the response.
143+
*/
144+
list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues(
145+
$this->mappings,
146+
$value,
147+
$type
148+
);
149+
$href = DataLinksHelper::buildUrl(
150+
$this->mappings,
151+
$idProperties,
152+
$idValues,
153+
$this->mappings[$type]->getResourceUrl(),
154+
$type
155+
);
156+
if ($href != $this->mappings[$type]->getResourceUrl()) {
157+
$data[self::LINKS_KEY][self::SELF_LINK][self::LINKS_HREF] = $href;
158+
}
159+
137160
}
138161
}
139162

163+
164+
165+
140166
if (empty($data[self::LINKS_KEY])) {
141167
unset($data[self::LINKS_KEY]);
142168
}
169+
143170
}
144171

145172
/**

tests/HelperMapping.php

+2-15
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,11 @@ public static function complex()
4545
'author' => [
4646
'related' => ['name' => 'http://example.com/posts/{postId}/author'],
4747
'self' => ['name' => 'http://example.com/posts/{postId}/relationships/author'],
48+
'comment' => ['name' => 'http://example.com/posts/{postId}/relationships/comments'],
4849
],
4950
],
5051
],
51-
[
52-
'class' => PostId::class,
53-
'alias' => '',
54-
'aliased_properties' => [],
55-
'hide_properties' => [],
56-
'id_properties' => [
57-
'postId',
58-
],
59-
'urls' => [
60-
'self' => ['name' => 'http://example.com/posts/{postId}'],
61-
'relationships' => [
62-
Comment::class => ['name' => 'http://example.com/posts/{postId}/relationships/comments'],
63-
],
64-
],
65-
],
52+
6653
[
6754
'class' => User::class,
6855
'alias' => '',

tests/JsonApiTransformerTest.php

+6-30
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,6 @@ public function testItWillSerializeToJsonApiAComplexObject()
159159
"self":{
160160
"href":"http://example.com/posts/9"
161161
},
162-
"first":{
163-
"href":"http://example.com/posts/1"
164-
},
165-
"next":{
166-
"href":"http://example.com/posts/10"
167-
},
168162
"comments":{
169163
"href":"http://example.com/posts/9/comments"
170164
}
@@ -193,14 +187,16 @@ public function testItWillSerializeToJsonApiAComplexObject()
193187
]
194188
);
195189
$transformer->addMeta('is_devel', true);
196-
$transformer->setSelfUrl('http://example.com/posts/9');
197-
$transformer->setFirstUrl('http://example.com/posts/1');
198-
$transformer->setNextUrl('http://example.com/posts/10');
190+
191+
print_r(\json_decode((new JsonApiSerializer($transformer))->serialize($post), true));
199192

200193
$this->assertEquals(
201194
\json_decode($expected, true),
202195
\json_decode((new JsonApiSerializer($transformer))->serialize($post), true)
203196
);
197+
198+
199+
die();
204200
}
205201

206202
/**
@@ -303,13 +299,11 @@ public function testItWillSerializeToJsonApiAComplexObjectAndFilterIncluded()
303299
]
304300
);
305301
$transformer->addMeta('is_devel', true);
306-
$transformer->setSelfUrl('http://example.com/posts/9');
307-
$transformer->setFirstUrl('http://example.com/posts/1');
308-
$transformer->setNextUrl('http://example.com/posts/10');
309302

310303
$included = new Included();
311304
$included->add('user.post');
312305

306+
313307
$this->assertEquals(
314308
\json_decode($expected, true),
315309
\json_decode((new JsonApiSerializer($transformer))->serialize($post, new Fields(), $included), true)
@@ -381,12 +375,6 @@ public function testItWillSerializeToJsonApiAComplexObjectAndFilterIncludedSpeci
381375
"self":{
382376
"href":"http://example.com/posts/9"
383377
},
384-
"first":{
385-
"href":"http://example.com/posts/1"
386-
},
387-
"next":{
388-
"href":"http://example.com/posts/10"
389-
},
390378
"comments":{
391379
"href":"http://example.com/posts/9/comments"
392380
}
@@ -415,9 +403,6 @@ public function testItWillSerializeToJsonApiAComplexObjectAndFilterIncludedSpeci
415403
]
416404
);
417405
$transformer->addMeta('is_devel', true);
418-
$transformer->setSelfUrl('http://example.com/posts/9');
419-
$transformer->setFirstUrl('http://example.com/posts/1');
420-
$transformer->setNextUrl('http://example.com/posts/10');
421406

422407
$included = new Included();
423408
$included->add('user');
@@ -457,12 +442,6 @@ public function testItWillSerializeToJsonApiAComplexObjectAndFilterFields()
457442
"self":{
458443
"href":"http://example.com/posts/9"
459444
},
460-
"first":{
461-
"href":"http://example.com/posts/1"
462-
},
463-
"next":{
464-
"href":"http://example.com/posts/10"
465-
},
466445
"comments":{
467446
"href":"http://example.com/posts/9/comments"
468447
}
@@ -491,9 +470,6 @@ public function testItWillSerializeToJsonApiAComplexObjectAndFilterFields()
491470
]
492471
);
493472
$transformer->addMeta('is_devel', true);
494-
$transformer->setSelfUrl('http://example.com/posts/9');
495-
$transformer->setFirstUrl('http://example.com/posts/1');
496-
$transformer->setNextUrl('http://example.com/posts/10');
497473

498474
$fields = new Fields();
499475
$fields->addField('post', 'title');

0 commit comments

Comments
 (0)