Last active
August 17, 2018 23:06
Revisions
-
ipalaus revised this gist
Jan 23, 2014 . 3 changed files with 106 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ <?php namespace League\Fractal\Cursor; class Cursor implements CursorInterface { protected $current; protected $next; protected $count; public function __construct($current = null, $next = null, $count = null) { $this->current = $current; $this->next = $next; $this->count = $count; } public function getCurrent() { return $this->current; } public function setCurrent($current) { $this->current = $current; return $this; } public function getNext() { return $this->next; } public function setNext($next) { $this->next = $next; return $this; } public function getCount() { return $this->count; } public function setCount($count) { $this->count = $count; return $this; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ <?php namespace League\Fractal\Cursor; interface CursorInterface { public function getCurrent(); public function getNext(); public function getCount(); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ <?php namespace League\Fractal; use League\Fractal\Resource\Item; use League\Fractal\Resource\Collection; use League\Fractal\Resource\ResourceInterface; use League\Fractal\Pagination\PaginatorInterface; use League\Fractal\Cursor\CursorInterface; class Scope { public function toArray() { $output = array( 'data' => $this->runAppropriateTransformer() ); if ($this->availableEmbeds) { $output['embeds'] = $this->availableEmbeds; } if ($this->resource instanceof Collection) { $paginator = $this->resource->getPaginator(); if ($paginator !== null and $paginator instanceof PaginatorInterface) { $output['pagination'] = $this->outputPaginator($paginator); } /** * NEW CODE HERE */ $cursor = $this->resource->getCursor(); if ($cursor !== null and $cursor instanceof CursorInterface) { $output['cursor'] = $this->outputCursor($cursor); } } return $output; } } -
ipalaus revised this gist
Jan 23, 2014 . 2 changed files with 58 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ { "data": [ { "id": 6, "name": "Lennie Raynor PhD" }, { "id": 7, "name": "Duncan Raynor" }, { "id": 8, "name": "clockman" }, { "id": 9, "name": "Gerard Marks DDS" }, { "id": 10, "name": "josefina32" } ], "cursor": { "current": 5, "next": 10, "count": 5 } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ { "data": [ { "id": 1, "name": "Isern Palaus" }, { "id": 2, "name": "Lorem Ipsum" }, { "id": 3, "name": "Javi Martinez" }, { "id": 4, "name": "lowe.yasmin" }, { "id": 5, "name": "abshire.durward" } ], "cursor": { "current": 0, "next": 5, "count": 5 } } -
ipalaus created this gist
Jan 23, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ <?php use League\Fractal\Manager; use League\Fractal\Resource\Collection; use League\Fractal\Resource\Item; use League\Fractal\Cursor\CursorInterface; class ApiController extends Controller { public function __construct() { $this->fractal = new Manager; $this->fractal->setRequestedScopes(explode(',', Input::get('include'))); } protected function respondWithCursor($collection, $callback, CursorInterface $cursor) { $resource = new Collection($collection, $callback); $resource->setCursor($cursor); $rootScope = $this->fractal->createData($resource); return $this->respondWithArray($rootScope->toArray()); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ <?php use League\Fractal\Cursor\Cursor; class UsersController extends ApiController { public function index() { $current = Input::get('cursor', false); $users = new User; if ($current and is_numeric($current)) { $users = $users->where('id', '>', $current); } $users = $users->take(20)->get(); $cursor = new Cursor($current, $users->last()->id, $users->count()); return $this->respondWithCursor($users, new UserTransformer, $cursor); } }