Esempio n. 1
0
    public function testSerialization()
    {
        $collection = new Collection();
        $collection->total = 20;
        $collection->page = 2;
        $collection->limit = 10;
        $collection->addLink(AtomLink::create('self', 'http://fsc.com/collection?page=2'));
        $collection->addLink(AtomLink::create('next', 'http://fsc.com/collection?page=3'));
        $collection->addLink(AtomLink::create('previous', 'http://fsc.com/collection?page=1'));
        $collection->addLink(AtomLink::create('first', 'http://fsc.com/collection?page=1'));
        $collection->addLink(AtomLink::create('last', 'http://fsc.com/collection?page=5'));
        $collection->addLink(AtomLink::create('new', 'http://fsc.com/collection/new'));
        $collection->results = array('foo', 'bar');
        $this->assertSerializedXmlEquals('<collection total="20" page="2" limit="10">
  <link rel="self" href="http://fsc.com/collection?page=2"/>
  <link rel="next" href="http://fsc.com/collection?page=3"/>
  <link rel="previous" href="http://fsc.com/collection?page=1"/>
  <link rel="first" href="http://fsc.com/collection?page=1"/>
  <link rel="last" href="http://fsc.com/collection?page=5"/>
  <link rel="new" href="http://fsc.com/collection/new"/>
  <entry><![CDATA[foo]]></entry>
  <entry><![CDATA[bar]]></entry>
</collection>', $collection);
    }
Esempio n. 2
0
 protected function configureCollectionRepresentation(CollectionRepresentation $collectionRepresentation, Pagerfanta $pager, $entity = null, $collectionRel = null)
 {
     // Properties
     $collectionRepresentation->total = $pager->getNbResults();
     $collectionRepresentation->page = $pager->getCurrentPage();
     $collectionRepresentation->limit = $pager->getMaxPerPage();
     // Links between pages
     $createRoute = function ($page, $limit) use($entity, $collectionRel) {
         $parameters = array('search' => array('page' => $page, 'limit' => $limit));
         return null !== $entity && null !== $collectionRel ? $this->getUrlGenerator()->generateEntityCollectionUrl($entity, $collectionRel, $parameters) : $this->getUrlGenerator()->generateCollectionUrl($parameters);
     };
     $collectionRepresentation->addLink($this->atomLinkFactory->create('self', $createRoute($pager->getCurrentPage(), $pager->getMaxPerPage())));
     if ($pager->hasNextPage()) {
         $collectionRepresentation->addLink($this->atomLinkFactory->create('next', $createRoute($pager->getNextPage(), $pager->getMaxPerPage())));
     }
     if ($pager->hasPreviousPage()) {
         $collectionRepresentation->addLink($this->atomLinkFactory->create('previous', $createRoute($pager->getPreviousPage(), $pager->getMaxPerPage())));
     }
     $collectionRepresentation->addLink($this->atomLinkFactory->create('first', $createRoute(1, $pager->getMaxPerPage())));
     $collectionRepresentation->addLink($this->atomLinkFactory->create('last', $createRoute($pager->getNbPages(), $pager->getMaxPerPage())));
 }