Skip to content

Commit 8b19908

Browse files
committed
Merge branch 'cursor-pagination-profile' into 2.1.0
2 parents 5fdddb0 + 8809bb3 commit 8b19908

9 files changed

Lines changed: 846 additions & 10 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use alsvanzelf\jsonapi\CollectionDocument;
4+
use alsvanzelf\jsonapi\objects\ResourceObject;
5+
use alsvanzelf\jsonapi\profiles\CursorPaginationProfile;
6+
7+
require 'bootstrap_examples.php';
8+
9+
/**
10+
* use the cursor pagination profile as extension to the document
11+
*/
12+
13+
$profile = new CursorPaginationProfile();
14+
15+
$user1 = new ResourceObject('user', 1);
16+
$user2 = new ResourceObject('user', 2);
17+
$user42 = new ResourceObject('user', 42);
18+
19+
$profile->setCursor($user1, 'ford');
20+
$profile->setCursor($user2, 'arthur');
21+
$profile->setCursor($user42, 'zaphod');
22+
23+
$document = CollectionDocument::fromResources($user1, $user2, $user42);
24+
$document->applyProfile($profile);
25+
26+
$profile->setCount($document, $exactTotal=3, $bestGuessTotal=10);
27+
$profile->setLinksFirstPage($document, $currentUrl='/users?sort=42&page[size]=10', $lastCursor='zaphod');
28+
29+
/**
30+
* get the json
31+
*/
32+
33+
$options = [
34+
'prettyPrint' => true,
35+
];
36+
echo '<pre>'.$document->toJson($options);

examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ <h3>Misc</h3>
4949
<li><a href="meta_only.php">Meta-only use-cases</a></li>
5050
<li><a href="status_only.php">Status-only</a></li>
5151
<li><a href="example_profile.php">Example profile</a></li>
52+
<li><a href="cursor_pagination_profile.php">Cursor pagination profile</a></li>
5253
<li><a href="output.php">Different ways to output</a></li>
5354
</ul>
5455

src/CollectionDocument.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use alsvanzelf\jsonapi\DataDocument;
66
use alsvanzelf\jsonapi\Document;
77
use alsvanzelf\jsonapi\exceptions\InputException;
8+
use alsvanzelf\jsonapi\interfaces\PaginableInterface;
89
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface;
910
use alsvanzelf\jsonapi\interfaces\ResourceContainerInterface;
1011
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
@@ -15,7 +16,7 @@
1516
* this document is a set of Resources
1617
* this document should be used if there could be multiple, also if only one or even none is returned
1718
*/
18-
class CollectionDocument extends DataDocument implements ResourceContainerInterface {
19+
class CollectionDocument extends DataDocument implements PaginableInterface, ResourceContainerInterface {
1920
/** @var ResourceInterface[] */
2021
protected $resources = [];
2122
/** @var array */
@@ -63,10 +64,7 @@ public function add($type, $id, array $attributes=[]) {
6364
}
6465

6566
/**
66-
* @param string $previousHref optional
67-
* @param string $nextHref optional
68-
* @param string $firstHref optional
69-
* @param string $lastHref optional
67+
* @inheritDoc
7068
*/
7169
public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) {
7270
if ($previousHref !== null) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace alsvanzelf\jsonapi\interfaces;
4+
5+
interface PaginableInterface {
6+
/**
7+
* @param string $previousHref optional
8+
* @param string $nextHref optional
9+
* @param string $firstHref optional
10+
* @param string $lastHref optional
11+
*/
12+
public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null);
13+
}

src/objects/RelationshipObject.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
use alsvanzelf\jsonapi\helpers\AtMemberManager;
88
use alsvanzelf\jsonapi\helpers\LinksManager;
99
use alsvanzelf\jsonapi\interfaces\ObjectInterface;
10+
use alsvanzelf\jsonapi\interfaces\PaginableInterface;
1011
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface;
1112
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
1213
use alsvanzelf\jsonapi\objects\LinksObject;
1314
use alsvanzelf\jsonapi\objects\MetaObject;
1415
use alsvanzelf\jsonapi\objects\ResourceObject;
1516

16-
class RelationshipObject implements ObjectInterface, RecursiveResourceContainerInterface {
17+
class RelationshipObject implements ObjectInterface, PaginableInterface, RecursiveResourceContainerInterface {
1718
use AtMemberManager, LinksManager;
1819

1920
const TO_ONE = 'one';
@@ -143,10 +144,7 @@ public function setRelatedLink($href, array $meta=[]) {
143144
}
144145

145146
/**
146-
* @param string $previousHref optional
147-
* @param string $nextHref optional
148-
* @param string $firstHref optional
149-
* @param string $lastHref optional
147+
* @inheritDoc
150148
*
151149
* @throws InputException if used on a to-one relationship
152150
*/

0 commit comments

Comments
 (0)