Skip to content

Instantly share code, notes, and snippets.

@neandher
Created April 2, 2016 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neandher/8ad126bfd1d055a051b23a8c634f259d to your computer and use it in GitHub Desktop.
Save neandher/8ad126bfd1d055a051b23a8c634f259d to your computer and use it in GitHub Desktop.
KnpUniversity Symfony REST: Using @link with the PaginatedCollection
<?php
namespace AppBundle\Pagination;
use AppBundle\Annotation\Link;
use JMS\Serializer\Annotation as Serializer;
/**
* @Link(
* "self",
* url = "object.getUrl('self')"
* )
*
* @Link(
* "first",
* url = "object.getUrl('first')"
* )
*
* @Link(
* "last",
* url = "object.getUrl('last')"
* )
*
* @Link(
* "next",
* url = "object.getUrl('next')"
* )
*
* @Link(
* "previous",
* url = "object.getUrl('previous')"
* )
*
* @Serializer\ExclusionPolicy("all")
*/
class PaginatedCollection
{
/**
* @Serializer\Expose()
*
* @var
*/
private $items;
/**
* @Serializer\Expose()
*
* @var
*/
private $total;
/**
* @Serializer\Expose()
*
* @var
*/
private $count;
/**
* @var array
*/
private $_links = array();
/**
* PaginatedCollection constructor.
* @param $items
* @param $total
*/
public function __construct($items, $total)
{
$this->items = $items;
$this->total = $total;
$this->count = count($items);
}
public function addLink($ref, $url)
{
$this->_links[$ref] = $url;
}
public function getUrl($ref)
{
return isset($this->_links[$ref]) ? $this->_links[$ref] : '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment