Skip to content

Commit cc4f340

Browse files
committed
Parse pagination links in DocumentLink, if data attribute exists
refs #23
1 parent 366f468 commit cc4f340

File tree

3 files changed

+58
-40
lines changed

3 files changed

+58
-40
lines changed

src/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function __construct($object, FactoryManagerInterface $manager)
101101
{
102102
$this->container->set('links', $this->manager->getFactory()->make(
103103
'DocumentLink',
104-
[$object->links, $this->manager]
104+
[$object->links, $this->manager, $this]
105105
));
106106
}
107107

src/DocumentLink.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ final class DocumentLink implements DocumentLinkInterface
3232
*/
3333
protected $manager;
3434

35+
/**
36+
* @var AccessInterface
37+
*/
38+
protected $parent;
39+
3540
/**
3641
* @param object $object The link object
3742
*
3843
* @return self
3944
*
4045
* @throws ValidationException
4146
*/
42-
public function __construct($object, FactoryManagerInterface $manager)
47+
public function __construct($object, FactoryManagerInterface $manager, AccessInterface $parent)
4348
{
4449
if ( ! is_object($object) )
4550
{
@@ -48,6 +53,8 @@ public function __construct($object, FactoryManagerInterface $manager)
4853

4954
$this->manager = $manager;
5055

56+
$this->parent = $parent;
57+
5158
$this->container = new DataContainer();
5259

5360
$links = get_object_vars($object);
@@ -76,34 +83,36 @@ public function __construct($object, FactoryManagerInterface $manager)
7683
unset($links['related']);
7784
}
7885

79-
// Pagination links
80-
81-
if ( array_key_exists('first', $links) )
86+
// Pagination links, if data in parent attributes exists
87+
if ( $parent->has('data') )
8288
{
83-
$this->setPaginationLink('first', $links['first']);
89+
if ( array_key_exists('first', $links) )
90+
{
91+
$this->setPaginationLink('first', $links['first']);
8492

85-
unset($links['first']);
86-
}
93+
unset($links['first']);
94+
}
8795

88-
if ( array_key_exists('last', $links) )
89-
{
90-
$this->setPaginationLink('last', $links['last']);
96+
if ( array_key_exists('last', $links) )
97+
{
98+
$this->setPaginationLink('last', $links['last']);
9199

92-
unset($links['last']);
93-
}
100+
unset($links['last']);
101+
}
94102

95-
if ( array_key_exists('prev', $links) )
96-
{
97-
$this->setPaginationLink('prev', $links['prev']);
103+
if ( array_key_exists('prev', $links) )
104+
{
105+
$this->setPaginationLink('prev', $links['prev']);
98106

99-
unset($links['prev']);
100-
}
107+
unset($links['prev']);
108+
}
101109

102-
if ( array_key_exists('next', $links) )
103-
{
104-
$this->setPaginationLink('next', $links['next']);
110+
if ( array_key_exists('next', $links) )
111+
{
112+
$this->setPaginationLink('next', $links['next']);
105113

106-
unset($links['next']);
114+
unset($links['next']);
115+
}
107116
}
108117

109118
// custom links

tests/unit/DocumentLinkTest.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ class DocumentLinkTest extends \PHPUnit_Framework_TestCase
1515
public function setUp()
1616
{
1717
$this->manager = $this->buildManagerMock();
18+
19+
// Mock parent
20+
$this->parent = $this->getMockBuilder('Art4\JsonApiClient\AccessInterface')
21+
->getMock();
22+
23+
$this->parent->expects($this->any())
24+
->method('has')
25+
->with('data')
26+
->will($this->returnValue(true));
1827
}
1928

2029
/**
@@ -37,7 +46,7 @@ public function testOnlySelfRelatedPaginationPropertiesExists()
3746
$object->custom = 'http://example.org/custom';
3847
$object->meta = 'http://example.org/meta';
3948

40-
$link = new DocumentLink($object, $this->manager);
49+
$link = new DocumentLink($object, $this->manager, $this->parent);
4150

4251
$this->assertInstanceOf('Art4\JsonApiClient\DocumentLink', $link);
4352
$this->assertInstanceOf('Art4\JsonApiClient\AccessInterface', $link);
@@ -102,7 +111,7 @@ public function testCreateWithoutObjectThrowsException($input)
102111
'DocumentLink has to be an object, "' . gettype($input) . '" given.'
103112
);
104113

105-
$link = new DocumentLink($input, $this->manager);
114+
$link = new DocumentLink($input, $this->manager, $this->parent);
106115
}
107116

108117
/**
@@ -126,7 +135,7 @@ public function testSelfMustBeAString($input)
126135
'property "self" has to be a string, "' . gettype($input) . '" given.'
127136
);
128137

129-
$link = new DocumentLink($object, $this->manager);
138+
$link = new DocumentLink($object, $this->manager, $this->parent);
130139
}
131140

132141
/**
@@ -154,7 +163,7 @@ public function testRelatedMustBeAStringOrObject($input)
154163
// Input must be a string or object
155164
if ( gettype($input) === 'string' or gettype($input) === 'object' )
156165
{
157-
$link = new DocumentLink($object, $this->manager);
166+
$link = new DocumentLink($object, $this->manager, $this->parent);
158167

159168
$this->assertTrue($link->has('related'));
160169

@@ -166,7 +175,7 @@ public function testRelatedMustBeAStringOrObject($input)
166175
'property "related" has to be a string or object, "' . gettype($input) . '" given.'
167176
);
168177

169-
$link = new DocumentLink($object, $this->manager);
178+
$link = new DocumentLink($object, $this->manager, $this->parent);
170179
}
171180

172181
/**
@@ -183,7 +192,7 @@ public function testFirstCanBeAStringOrNull($input)
183192
// Input must be null or string
184193
if ( gettype($input) === 'string' )
185194
{
186-
$link = new DocumentLink($object, $this->manager);
195+
$link = new DocumentLink($object, $this->manager, $this->parent);
187196
$this->assertSame($link->getKeys(), array('self', 'first'));
188197

189198
$this->assertTrue($link->has('first'));
@@ -193,7 +202,7 @@ public function testFirstCanBeAStringOrNull($input)
193202
}
194203
elseif ( gettype($input) === 'NULL' )
195204
{
196-
$link = new DocumentLink($object, $this->manager);
205+
$link = new DocumentLink($object, $this->manager, $this->parent);
197206
$this->assertSame($link->getKeys(), array('self'));
198207

199208
$this->assertFalse($link->has('first'));
@@ -206,7 +215,7 @@ public function testFirstCanBeAStringOrNull($input)
206215
'property "first" has to be a string or null, "' . gettype($input) . '" given.'
207216
);
208217

209-
$link = new DocumentLink($object, $this->manager);
218+
$link = new DocumentLink($object, $this->manager, $this->parent);
210219
}
211220

212221
/**
@@ -223,7 +232,7 @@ public function testLastCanBeAStringOrNull($input)
223232
// Input must be null or string
224233
if ( gettype($input) === 'string' )
225234
{
226-
$link = new DocumentLink($object, $this->manager);
235+
$link = new DocumentLink($object, $this->manager, $this->parent);
227236
$this->assertSame($link->getKeys(), array('self', 'last'));
228237

229238
$this->assertTrue($link->has('last'));
@@ -233,7 +242,7 @@ public function testLastCanBeAStringOrNull($input)
233242
}
234243
elseif ( gettype($input) === 'NULL' )
235244
{
236-
$link = new DocumentLink($object, $this->manager);
245+
$link = new DocumentLink($object, $this->manager, $this->parent);
237246
$this->assertSame($link->getKeys(), array('self'));
238247

239248
$this->assertFalse($link->has('last'));
@@ -246,7 +255,7 @@ public function testLastCanBeAStringOrNull($input)
246255
'property "last" has to be a string or null, "' . gettype($input) . '" given.'
247256
);
248257

249-
$link = new DocumentLink($object, $this->manager);
258+
$link = new DocumentLink($object, $this->manager, $this->parent);
250259
}
251260

252261
/**
@@ -263,7 +272,7 @@ public function testPrevCanBeAStringOrNull($input)
263272
// Input must be null or string
264273
if ( gettype($input) === 'string' )
265274
{
266-
$link = new DocumentLink($object, $this->manager);
275+
$link = new DocumentLink($object, $this->manager, $this->parent);
267276
$this->assertSame($link->getKeys(), array('self', 'prev'));
268277

269278
$this->assertTrue($link->has('prev'));
@@ -273,7 +282,7 @@ public function testPrevCanBeAStringOrNull($input)
273282
}
274283
elseif ( gettype($input) === 'NULL' )
275284
{
276-
$link = new DocumentLink($object, $this->manager);
285+
$link = new DocumentLink($object, $this->manager, $this->parent);
277286
$this->assertSame($link->getKeys(), array('self'));
278287

279288
$this->assertFalse($link->has('prev'));
@@ -286,7 +295,7 @@ public function testPrevCanBeAStringOrNull($input)
286295
'property "prev" has to be a string or null, "' . gettype($input) . '" given.'
287296
);
288297

289-
$link = new DocumentLink($object, $this->manager);
298+
$link = new DocumentLink($object, $this->manager, $this->parent);
290299
}
291300

292301
/**
@@ -303,7 +312,7 @@ public function testNextCanBeAStringOrNull($input)
303312
// Input must be null or string
304313
if ( gettype($input) === 'string' )
305314
{
306-
$link = new DocumentLink($object, $this->manager);
315+
$link = new DocumentLink($object, $this->manager, $this->parent);
307316
$this->assertSame($link->getKeys(), array('self', 'next'));
308317

309318
$this->assertTrue($link->has('next'));
@@ -313,7 +322,7 @@ public function testNextCanBeAStringOrNull($input)
313322
}
314323
elseif ( gettype($input) === 'NULL' )
315324
{
316-
$link = new DocumentLink($object, $this->manager);
325+
$link = new DocumentLink($object, $this->manager, $this->parent);
317326
$this->assertSame($link->getKeys(), array('self'));
318327

319328
$this->assertFalse($link->has('next'));
@@ -326,7 +335,7 @@ public function testNextCanBeAStringOrNull($input)
326335
'property "next" has to be a string or null, "' . gettype($input) . '" given.'
327336
);
328337

329-
$link = new DocumentLink($object, $this->manager);
338+
$link = new DocumentLink($object, $this->manager, $this->parent);
330339
}
331340

332341
/**
@@ -337,7 +346,7 @@ public function testGetOnANonExistingKeyThrowsException()
337346
$object = new \stdClass();
338347
$object->self = 'http://example.org/self';
339348

340-
$link = new DocumentLink($object, $this->manager);
349+
$link = new DocumentLink($object, $this->manager, $this->parent);
341350

342351
$this->assertFalse($link->has('something'));
343352

0 commit comments

Comments
 (0)