Skip to content

Instantly share code, notes, and snippets.

@ipalaus
Last active August 17, 2018 23:06

Revisions

  1. ipalaus revised this gist Jan 23, 2014. 3 changed files with 106 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions Cursor.php
    Original 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;
    }

    }
    10 changes: 10 additions & 0 deletions CursorInterface.php
    Original 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();
    }
    44 changes: 44 additions & 0 deletions Scope.php
    Original 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;
    }

    }
  2. ipalaus revised this gist Jan 23, 2014. 2 changed files with 58 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions cursor_5.json
    Original 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
    }
    }
    29 changes: 29 additions & 0 deletions empty_cursor.json
    Original 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
    }
    }
  3. ipalaus created this gist Jan 23, 2014.
    28 changes: 28 additions & 0 deletions ApiController.php
    Original 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());
    }

    }
    25 changes: 25 additions & 0 deletions UsersController.php
    Original 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);
    }

    }