Skip to content

Commit

Permalink
Merge branch 'cursor-pagination-profile' into 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lode committed Mar 3, 2019
2 parents 5fdddb0 + 8809bb3 commit 8b19908
Show file tree
Hide file tree
Showing 9 changed files with 846 additions and 10 deletions.
36 changes: 36 additions & 0 deletions examples/cursor_pagination_profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;
use alsvanzelf\jsonapi\profiles\CursorPaginationProfile;

require 'bootstrap_examples.php';

/**
* use the cursor pagination profile as extension to the document
*/

$profile = new CursorPaginationProfile();

$user1 = new ResourceObject('user', 1);
$user2 = new ResourceObject('user', 2);
$user42 = new ResourceObject('user', 42);

$profile->setCursor($user1, 'ford');
$profile->setCursor($user2, 'arthur');
$profile->setCursor($user42, 'zaphod');

$document = CollectionDocument::fromResources($user1, $user2, $user42);
$document->applyProfile($profile);

$profile->setCount($document, $exactTotal=3, $bestGuessTotal=10);
$profile->setLinksFirstPage($document, $currentUrl='/users?sort=42&page[size]=10', $lastCursor='zaphod');

/**
* get the json
*/

$options = [
'prettyPrint' => true,
];
echo '<pre>'.$document->toJson($options);
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ <h3>Misc</h3>
<li><a href="meta_only.php">Meta-only use-cases</a></li>
<li><a href="status_only.php">Status-only</a></li>
<li><a href="example_profile.php">Example profile</a></li>
<li><a href="cursor_pagination_profile.php">Cursor pagination profile</a></li>
<li><a href="output.php">Different ways to output</a></li>
</ul>

Expand Down
8 changes: 3 additions & 5 deletions src/CollectionDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use alsvanzelf\jsonapi\DataDocument;
use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\exceptions\InputException;
use alsvanzelf\jsonapi\interfaces\PaginableInterface;
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface;
use alsvanzelf\jsonapi\interfaces\ResourceContainerInterface;
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
Expand All @@ -15,7 +16,7 @@
* this document is a set of Resources
* this document should be used if there could be multiple, also if only one or even none is returned
*/
class CollectionDocument extends DataDocument implements ResourceContainerInterface {
class CollectionDocument extends DataDocument implements PaginableInterface, ResourceContainerInterface {
/** @var ResourceInterface[] */
protected $resources = [];
/** @var array */
Expand Down Expand Up @@ -63,10 +64,7 @@ public function add($type, $id, array $attributes=[]) {
}

/**
* @param string $previousHref optional
* @param string $nextHref optional
* @param string $firstHref optional
* @param string $lastHref optional
* @inheritDoc
*/
public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) {
if ($previousHref !== null) {
Expand Down
13 changes: 13 additions & 0 deletions src/interfaces/PaginableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace alsvanzelf\jsonapi\interfaces;

interface PaginableInterface {
/**
* @param string $previousHref optional
* @param string $nextHref optional
* @param string $firstHref optional
* @param string $lastHref optional
*/
public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null);
}
8 changes: 3 additions & 5 deletions src/objects/RelationshipObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use alsvanzelf\jsonapi\helpers\AtMemberManager;
use alsvanzelf\jsonapi\helpers\LinksManager;
use alsvanzelf\jsonapi\interfaces\ObjectInterface;
use alsvanzelf\jsonapi\interfaces\PaginableInterface;
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface;
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
use alsvanzelf\jsonapi\objects\LinksObject;
use alsvanzelf\jsonapi\objects\MetaObject;
use alsvanzelf\jsonapi\objects\ResourceObject;

class RelationshipObject implements ObjectInterface, RecursiveResourceContainerInterface {
class RelationshipObject implements ObjectInterface, PaginableInterface, RecursiveResourceContainerInterface {
use AtMemberManager, LinksManager;

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

/**
* @param string $previousHref optional
* @param string $nextHref optional
* @param string $firstHref optional
* @param string $lastHref optional
* @inheritDoc
*
* @throws InputException if used on a to-one relationship
*/
Expand Down
Loading

0 comments on commit 8b19908

Please sign in to comment.