Skip to content

Commit d8e272d

Browse files
committed
First level objects now have self link too
1 parent 4fbdc18 commit d8e272d

File tree

4 files changed

+85
-81
lines changed

4 files changed

+85
-81
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ $transformer = new JsonTransformer($mapper);
122122

123123
//Output transformation
124124
$serializer = new HalSerializer($transformer);
125-
$serializer->setSelfUrl('http://example.com/posts/9');
126125
$serializer->setNextUrl('http://example.com/posts/10');
127126
$serializer->addMeta('author',[['name' => 'Nil Portugués Calderó', 'email' => 'contact@nilportugues.com']]);
128127

src/JsonTransformer.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,37 @@ protected function addArrayValueResourceToEmbedded(
385385
*/
386386
protected function setResponseLinks(array &$data)
387387
{
388-
if (!empty($data[Serializer::CLASS_IDENTIFIER_KEY])) {
388+
if (!empty($type = $data[Serializer::CLASS_IDENTIFIER_KEY])) {
389389
$data[self::LINKS_KEY] = \array_merge(
390390
CuriesHelper::buildCuries($this->curies),
391391
$this->addHrefToLinks($this->buildLinks()),
392392
(!empty($data[self::LINKS_KEY])) ? $data[self::LINKS_KEY] : [],
393393
$this->addHrefToLinks($this->getResponseAdditionalLinks($data, $data[Serializer::CLASS_IDENTIFIER_KEY]))
394394
);
395395

396+
/*
397+
* Adds the _links:self:href link to the response.
398+
*/
399+
if (!empty($type = $data[Serializer::CLASS_IDENTIFIER_KEY])) {
400+
list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues(
401+
$this->mappings,
402+
$data,
403+
$type
404+
);
405+
406+
$href = self::buildUrl(
407+
$this->mappings,
408+
$idProperties,
409+
$idValues,
410+
$this->mappings[$type]->getResourceUrl(),
411+
$type
412+
);
413+
414+
if ($href != $this->mappings[$type]->getResourceUrl()) {
415+
$data[self::LINKS_KEY][self::SELF_LINK][self::LINKS_HREF] = $href;
416+
}
417+
}
418+
396419
$data[self::LINKS_KEY] = \array_filter($data[self::LINKS_KEY]);
397420

398421
if (empty($data[self::LINKS_KEY])) {
@@ -440,6 +463,7 @@ protected function setResponseMeta(array &$response)
440463
protected static function buildUrl(array &$mappings, $idProperties, $idValues, $url, $type)
441464
{
442465
$outputUrl = \str_replace($idProperties, $idValues, $url);
466+
443467
if ($outputUrl !== $url) {
444468
return $outputUrl;
445469
}

tests/JsonTransformerTest.php

Lines changed: 51 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,24 @@ public function testItWillSerializeToHalJsonAnArrayOfObjects()
5454
"title": "post title 1",
5555
"body": "post body 1",
5656
"author_id": 4,
57-
"comments": []
57+
"comments": [],
58+
"_links":{
59+
"self":{
60+
"href":"/post/1"
61+
}
62+
}
5863
},
5964
{
6065
"post_id": 2,
6166
"title": "post title 2",
6267
"body": "post body 2",
6368
"author_id": 5,
64-
"comments": []
69+
"comments": [],
70+
"_links":{
71+
"self":{
72+
"href":"/post/2"
73+
}
74+
}
6575
}
6676
]
6777
}
@@ -278,7 +288,6 @@ public function testItWillSerializeToHalJsonAComplexObject()
278288
]
279289
);
280290
$transformer->addMeta('is_devel', true);
281-
$transformer->setSelfUrl('http://example.com/posts/9');
282291
$transformer->setFirstUrl('http://example.com/posts/1');
283292
$transformer->setNextUrl('http://example.com/posts/10');
284293

@@ -339,7 +348,12 @@ public function testItWillSerializeToHalJsonASimpleObject()
339348
"user_id": "User 25",
340349
"created_at": "2015-07-23T12:48:00+02:00"
341350
}
342-
]
351+
],
352+
"_links": {
353+
"self": {
354+
"href": "/post/1"
355+
}
356+
}
343357
}
344358
JSON;
345359

@@ -401,7 +415,12 @@ public function testItWillRenamePropertiesFromClass()
401415
"user_id": "User 25",
402416
"created_at": "2015-07-23T12:48:00+02:00"
403417
}
404-
]
418+
],
419+
"_links": {
420+
"self": {
421+
"href": "/post/1"
422+
}
423+
}
405424
}
406425
JSON;
407426

@@ -461,7 +480,12 @@ public function testItWillHidePropertiesFromClass()
461480
"user_id": "User 25",
462481
"created_at": "2015-07-23T12:48:00+02:00"
463482
}
464-
]
483+
],
484+
"_links": {
485+
"self": {
486+
"href": "/post/1"
487+
}
488+
}
465489
}
466490
JSON;
467491

@@ -520,7 +544,12 @@ public function testTypeValueIsChangedByClassAlias()
520544
"user_id": "User 25",
521545
"created_at": "2015-07-23T12:48:00+02:00"
522546
}
523-
]
547+
],
548+
"_links": {
549+
"self": {
550+
"href": "/post/1"
551+
}
552+
}
524553
}
525554
JSON;
526555

@@ -579,7 +608,12 @@ public function testItIfFilteringOutKeys()
579608
"user_id": "User 25",
580609
"created_at": "2015-07-23T12:48:00+02:00"
581610
}
582-
]
611+
],
612+
"_links":{
613+
"self":{
614+
"href":"/post/1"
615+
}
616+
}
583617
}
584618
JSON;
585619

@@ -605,7 +639,7 @@ private function createSimplePost()
605639
return $post;
606640
}
607641

608-
public function testItCanBuildUrlsFromClassNameOrClassAlias()
642+
public function testItCanBuildSelfUrls()
609643
{
610644
$mappings = [
611645
[
@@ -623,15 +657,10 @@ public function testItCanBuildUrlsFromClassNameOrClassAlias()
623657
'postId',
624658
],
625659
'urls' => [
626-
// Mandatory
627-
'self' => 'http://example.com/posts/{Message}',
628-
// Optional
629-
'comments' => 'http://example.com/posts/{Message}/comments',
660+
'self' => 'http://example.com/posts/{postId}',
630661
],
631662
// (Optional) Used by HAL+JSON
632663
'curies' => [
633-
'name' => 'example',
634-
'href' => 'http://example.com/docs/rels/{rel}',
635664
],
636665
],
637666
[
@@ -643,14 +672,10 @@ public function testItCanBuildUrlsFromClassNameOrClassAlias()
643672
'userId',
644673
],
645674
'urls' => [
646-
'self' => 'http://example.com/users/{User}',
647-
'friends' => 'http://example.com/users/{User}/friends',
648-
'comments' => 'http://example.com/users/{User}/comments',
675+
'self' => 'http://example.com/users/{userId}',
649676
],
650677
// (Optional) Used by HAL+JSON
651678
'curies' => [
652-
'name' => 'example',
653-
'href' => 'http://example.com/docs/rels/{rel}',
654679
],
655680
],
656681
[
@@ -662,12 +687,10 @@ public function testItCanBuildUrlsFromClassNameOrClassAlias()
662687
'commentId',
663688
],
664689
'urls' => [
665-
'self' => 'http://example.com/comments/{Comment}',
690+
'self' => 'http://example.com/comments/{commentId}',
666691
],
667692
// (Optional) Used by HAL+JSON
668693
'curies' => [
669-
'name' => 'example',
670-
'href' => 'http://example.com/docs/rels/{rel}',
671694
],
672695
],
673696
];
@@ -686,12 +709,6 @@ public function testItCanBuildUrlsFromClassNameOrClassAlias()
686709
"_links":{
687710
"self":{
688711
"href":"http://example.com/users/1"
689-
},
690-
"example:friends":{
691-
"href":"http://example.com/users/1/friends"
692-
},
693-
"example:comments":{
694-
"href":"http://example.com/users/1/comments"
695712
}
696713
}
697714
},
@@ -710,19 +727,13 @@ public function testItCanBuildUrlsFromClassNameOrClassAlias()
710727
"_links":{
711728
"self":{
712729
"href":"http://example.com/users/2"
713-
},
714-
"example:friends":{
715-
"href":"http://example.com/users/2/friends"
716-
},
717-
"example:comments":{
718-
"href":"http://example.com/users/2/comments"
719730
}
720731
}
721732
}
722733
},
723734
"_links":{
724-
"example:user":{
725-
"href":"http://example.com/users/2"
735+
"user":{
736+
"href":"http://example.com/users/2"
726737
},
727738
"self":{
728739
"href":"http://example.com/comments/1000"
@@ -732,35 +743,12 @@ public function testItCanBuildUrlsFromClassNameOrClassAlias()
732743
]
733744
},
734745
"_links":{
735-
"curies":[
736-
{
737-
"name":"example",
738-
"href":"http://example.com/docs/rels/{rel}",
739-
"templated":true
740-
}
741-
],
746+
"author":{
747+
"href":"http://example.com/users/1"
748+
},
742749
"self":{
743750
"href":"http://example.com/posts/9"
744-
},
745-
"first":{
746-
"href":"http://example.com/posts/1"
747-
},
748-
"next":{
749-
"href":"http://example.com/posts/10"
750-
},
751-
"example:author":{
752-
"href":"http://example.com/users/1"
753-
},
754-
"example:comments":{
755-
"href":"http://example.com/posts/9/comments"
756751
}
757-
},
758-
"_meta":{
759-
"author":{
760-
"name":"Nil Portugués Calderó",
761-
"email":"contact@nilportugues.com"
762-
},
763-
"is_devel":true
764752
}
765753
}
766754
JSON;
@@ -786,18 +774,6 @@ public function testItCanBuildUrlsFromClassNameOrClassAlias()
786774
);
787775

788776
$transformer = new JsonTransformer($mapper);
789-
$transformer->setMeta(
790-
[
791-
'author' => [
792-
'name' => 'Nil Portugués Calderó',
793-
'email' => 'contact@nilportugues.com',
794-
],
795-
]
796-
);
797-
$transformer->addMeta('is_devel', true);
798-
$transformer->setSelfUrl('http://example.com/posts/9');
799-
$transformer->setFirstUrl('http://example.com/posts/1');
800-
$transformer->setNextUrl('http://example.com/posts/10');
801777

802778
$this->assertEquals(
803779
\json_decode($expected, true),

tests/XmlTransformerTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,25 @@ public function testItWillSerializeToHalXmlAnArrayOfObjects()
5050
<resource>
5151
<total><![CDATA[2]]></total>
5252
<embedded>
53-
<resource>
53+
<resource href="/post/1">
5454
<post_id><![CDATA[1]]></post_id>
5555
<title><![CDATA[post title 1]]></title>
5656
<body><![CDATA[post body 1]]></body>
5757
<author_id><![CDATA[4]]></author_id>
5858
<comments/>
59+
<links>
60+
<link rel="self" href="/post/1"/>
61+
</links>
5962
</resource>
60-
<resource>
63+
<resource href="/post/2">
6164
<post_id><![CDATA[2]]></post_id>
6265
<title><![CDATA[post title 2]]></title>
6366
<body><![CDATA[post body 2]]></body>
6467
<author_id><![CDATA[5]]></author_id>
6568
<comments/>
69+
<links>
70+
<link rel="self" href="/post/2"/>
71+
</links>
6672
</resource>
6773
</embedded>
6874
</resource>
@@ -222,11 +228,11 @@ public function testItWillSerializeToHalXmlAComplexObject()
222228
<templated><![CDATA[true]]></templated>
223229
</link>
224230
</curies>
225-
<link rel="self" href="http://example.com/posts/9"/>
226231
<link rel="first" href="http://example.com/posts/1"/>
227232
<link rel="next" href="http://example.com/posts/10"/>
228233
<link rel="example:author" href="http://example.com/users/1"/>
229234
<link rel="example:comments" href="http://example.com/posts/9/comments"/>
235+
<link rel="self" href="http://example.com/posts/9"/>
230236
</links>
231237
<meta>
232238
<author>
@@ -268,7 +274,6 @@ public function testItWillSerializeToHalXmlAComplexObject()
268274
]
269275
);
270276
$transformer->addMeta('is_devel', true);
271-
$transformer->setSelfUrl('http://example.com/posts/9');
272277
$transformer->setFirstUrl('http://example.com/posts/1');
273278
$transformer->setNextUrl('http://example.com/posts/10');
274279

0 commit comments

Comments
 (0)