Skip to content

Commit 47f1d06

Browse files
committed
Merge pull request #27 from Art4/bugfix-26
self and pagination links can be objects closes #26, closes #27
2 parents 58be8fd + 4b67320 commit 47f1d06

File tree

4 files changed

+143
-14
lines changed

4 files changed

+143
-14
lines changed

src/DocumentLink.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public function __construct($object, FactoryManagerInterface $manager, AccessInt
6161

6262
if ( array_key_exists('self', $links) )
6363
{
64-
if ( ! is_string($links['self']) )
64+
if ( ! is_string($links['self']) and ! is_object($links['self']) )
6565
{
66-
throw new ValidationException('property "self" has to be a string, "' . gettype($links['self']) . '" given.');
66+
throw new ValidationException('property "self" has to be a string or object, "' . gettype($links['self']) . '" given.');
6767
}
6868

69-
$this->container->set('self', $links['self']);
69+
$this->setLink('self', $links['self']);
7070

7171
unset($links['self']);
7272
}
@@ -149,15 +149,19 @@ public function get($key)
149149
*/
150150
private function setPaginationLink($name, $value)
151151
{
152-
if ( ! is_string($value) and ! is_null($value) )
152+
if ( ! is_object($value) and ! is_string($value) and ! is_null($value) )
153153
{
154-
throw new ValidationException('property "' . $name . '" has to be a string or null, "' . gettype($value) . '" given.');
154+
throw new ValidationException('property "' . $name . '" has to be an object, a string or null, "' . gettype($value) . '" given.');
155155
}
156156

157-
if ( ! is_null($value) )
157+
if ( is_string($value) )
158158
{
159159
$this->container->set($name, strval($value));
160160
}
161+
elseif ( is_object($value) )
162+
{
163+
$this->setLink($name, $value);
164+
}
161165

162166
return $this;
163167
}

tests/files/08_object_links.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"data": [
3+
{
4+
"type": "party",
5+
"id": "1",
6+
"attributes": {
7+
"legal_name": "Littel, Hickle and Wunsch",
8+
"legal_name_fetched": "2015-08-20 07:50:39",
9+
"nationality": "SE",
10+
"national_identifier": "5562404176",
11+
"created_at": "2016-04-13 13:39:25",
12+
"updated_at": "2016-04-13 13:39:25",
13+
"deleted_at": null
14+
},
15+
"links": {
16+
"self": {
17+
"href": "http://parties.api/v1/parties/1"
18+
}
19+
}
20+
}
21+
],
22+
"included": [],
23+
"links": {
24+
"self": {
25+
"href": "?page[number]=1&page[size]=10"
26+
},
27+
"first": {
28+
"href": "?page[number]=1&page[size]=10"
29+
},
30+
"next": {
31+
"href": "?page[number]=2&page[size]=10"
32+
},
33+
"last": {
34+
"href": "?page[number]=11&page[size]=10"
35+
}
36+
},
37+
"meta": {
38+
"page": {
39+
"total": 106,
40+
"last": 11,
41+
"number": 1,
42+
"size": 10
43+
}
44+
},
45+
"jsonapi": {
46+
"version": "1.0"
47+
}
48+
}

tests/integration/ParsingTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,41 @@ public function testParseRelationshipExample()
425425
// Test full array
426426
$this->assertEquals(json_decode($string, true), $document->asArray(true));
427427
}
428+
429+
/**
430+
* @test
431+
*/
432+
public function testParseObjectLinksExample()
433+
{
434+
$string = $this->getJsonString('08_object_links.json');
435+
$document = Helper::parse($string);
436+
437+
$this->assertInstanceOf('Art4\JsonApiClient\Document', $document);
438+
$this->assertTrue($document->has('links'));
439+
440+
$this->assertInstanceOf('Art4\JsonApiClient\DocumentLinkInterface', $document->get('links'));
441+
$this->assertTrue($document->has('links.self'));
442+
$this->assertTrue($document->has('links.first'));
443+
$this->assertTrue($document->has('links.next'));
444+
$this->assertTrue($document->has('links.last'));
445+
446+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $document->get('links.self'));
447+
$this->assertTrue($document->has('links.self.href'));
448+
$this->assertSame('?page[number]=1&page[size]=10', $document->get('links.self.href'));
449+
450+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $document->get('links.first'));
451+
$this->assertTrue($document->has('links.first.href'));
452+
$this->assertSame('?page[number]=1&page[size]=10', $document->get('links.first.href'));
453+
454+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $document->get('links.next'));
455+
$this->assertTrue($document->has('links.next.href'));
456+
$this->assertSame('?page[number]=2&page[size]=10', $document->get('links.next.href'));
457+
458+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $document->get('links.last'));
459+
$this->assertTrue($document->has('links.last.href'));
460+
$this->assertSame('?page[number]=11&page[size]=10', $document->get('links.last.href'));
461+
462+
// Test full array
463+
$this->assertEquals(json_decode($string, true), $document->asArray(true));
464+
}
428465
}

tests/unit/DocumentLinkTest.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ public function testCreateWithoutObjectOrStringAttributeThrowsException($input)
144144
*
145145
* self: the link that generated the current response document.
146146
*/
147-
public function testSelfMustBeAString($input)
147+
public function testSelfMustBeAStringOrObject($input)
148148
{
149149
// Input must be a string
150-
if ( gettype($input) === 'string' )
150+
if ( gettype($input) === 'string' or gettype($input) === 'object' )
151151
{
152152
return;
153153
}
@@ -157,7 +157,7 @@ public function testSelfMustBeAString($input)
157157

158158
$this->setExpectedException(
159159
'Art4\JsonApiClient\Exception\ValidationException',
160-
'property "self" has to be a string, "' . gettype($input) . '" given.'
160+
'property "self" has to be a string or object, "' . gettype($input) . '" given.'
161161
);
162162

163163
$link = new DocumentLink($object, $this->manager, $this->parent);
@@ -208,7 +208,7 @@ public function testRelatedMustBeAStringOrObject($input)
208208
*
209209
* Keys MUST either be omitted or have a null value to indicate that a particular link is unavailable.
210210
*/
211-
public function testFirstCanBeAStringOrNull($input)
211+
public function testFirstCanBeAnObjectOrStringOrNull($input)
212212
{
213213
$object = new \stdClass();
214214
$object->self = 'https://example.org/self';
@@ -234,10 +234,20 @@ public function testFirstCanBeAStringOrNull($input)
234234

235235
return;
236236
}
237+
elseif ( gettype($input) === 'object' )
238+
{
239+
$link = new DocumentLink($object, $this->manager, $this->parent);
240+
$this->assertSame($link->getKeys(), array('self', 'first'));
241+
242+
$this->assertTrue($link->has('first'));
243+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $link->get('first'));
244+
245+
return;
246+
}
237247

238248
$this->setExpectedException(
239249
'Art4\JsonApiClient\Exception\ValidationException',
240-
'property "first" has to be a string or null, "' . gettype($input) . '" given.'
250+
'property "first" has to be an object, a string or null, "' . gettype($input) . '" given.'
241251
);
242252

243253
$link = new DocumentLink($object, $this->manager, $this->parent);
@@ -274,10 +284,20 @@ public function testLastCanBeAStringOrNull($input)
274284

275285
return;
276286
}
287+
elseif ( gettype($input) === 'object' )
288+
{
289+
$link = new DocumentLink($object, $this->manager, $this->parent);
290+
$this->assertSame($link->getKeys(), array('self', 'last'));
291+
292+
$this->assertTrue($link->has('last'));
293+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $link->get('last'));
294+
295+
return;
296+
}
277297

278298
$this->setExpectedException(
279299
'Art4\JsonApiClient\Exception\ValidationException',
280-
'property "last" has to be a string or null, "' . gettype($input) . '" given.'
300+
'property "last" has to be an object, a string or null, "' . gettype($input) . '" given.'
281301
);
282302

283303
$link = new DocumentLink($object, $this->manager, $this->parent);
@@ -314,10 +334,20 @@ public function testPrevCanBeAStringOrNull($input)
314334

315335
return;
316336
}
337+
elseif ( gettype($input) === 'object' )
338+
{
339+
$link = new DocumentLink($object, $this->manager, $this->parent);
340+
$this->assertSame($link->getKeys(), array('self', 'prev'));
341+
342+
$this->assertTrue($link->has('prev'));
343+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $link->get('prev'));
344+
345+
return;
346+
}
317347

318348
$this->setExpectedException(
319349
'Art4\JsonApiClient\Exception\ValidationException',
320-
'property "prev" has to be a string or null, "' . gettype($input) . '" given.'
350+
'property "prev" has to be an object, a string or null, "' . gettype($input) . '" given.'
321351
);
322352

323353
$link = new DocumentLink($object, $this->manager, $this->parent);
@@ -354,10 +384,20 @@ public function testNextCanBeAStringOrNull($input)
354384

355385
return;
356386
}
387+
elseif ( gettype($input) === 'object' )
388+
{
389+
$link = new DocumentLink($object, $this->manager, $this->parent);
390+
$this->assertSame($link->getKeys(), array('self', 'next'));
391+
392+
$this->assertTrue($link->has('next'));
393+
$this->assertInstanceOf('Art4\JsonApiClient\LinkInterface', $link->get('next'));
394+
395+
return;
396+
}
357397

358398
$this->setExpectedException(
359399
'Art4\JsonApiClient\Exception\ValidationException',
360-
'property "next" has to be a string or null, "' . gettype($input) . '" given.'
400+
'property "next" has to be an object, a string or null, "' . gettype($input) . '" given.'
361401
);
362402

363403
$link = new DocumentLink($object, $this->manager, $this->parent);

0 commit comments

Comments
 (0)