Example #1
0
 /**
  *
  * Returns a JSON Array along with a cursor for pagination.
  *
  * @param $collection
  * @param $callback
  * @param CursorInterface $cursor
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function respondWithCursor($collection, $callback, CursorInterface $cursor)
 {
     $resource = new Collection($collection, $callback);
     $resource->setCursor($cursor);
     $rootScope = $this->fractal->createData($resource);
     return $this->respondWithArray($rootScope->toArray());
 }
 /**
  * @param $data
  * @param \League\Fractal\TransformerAbstract $transformer
  * @param \League\Fractal\Pagination\Cursor|int $cursor
  * @param string $resourceKey
  * @return \League\Fractal\Scope
  */
 public function cursorCollection($data, $transformer = null, $cursor = null, $resourceKey = null)
 {
     $transformer = $this->getTransformer($transformer);
     $resource = new Collection($data, $transformer, $resourceKey);
     $resource->setCursor($this->makeCursor($cursor, $data));
     return $this->manager->createData($resource);
 }
 /**
  * Create a new collection resource with the given tranformer.
  *
  * @param  array|object  $collection
  * @param  object  $transformer
  * @param  \League\Fractal\Pagination\CursorInterface  $cursor
  * @param  string  $key
  * @return \Illuminate\Http\Response
  */
 public function withCollection($collection, $transformer, FractalCursorInterface $cursor = null, $key = null)
 {
     $resource = new FractalCollection($collection, $transformer, $key);
     if (!is_null($cursor)) {
         $resource->setCursor($cursor);
     }
     return $this->build($resource);
 }
 public function collection($rootname, $items, TransformerAbstract $transformer, $options = array())
 {
     $resources = new Collection($items, $transformer, $rootname);
     if (isset($options['meta'])) {
         foreach ($options['meta'] as $metaKey => $metaItem) {
             $resources->setMetaValue($metaKey, $metaItem);
         }
     }
     if (array_key_exists('cursor', $options)) {
         $resources->setCursor($options['cursor']);
     }
     if (array_key_exists('callback', $options)) {
         call_user_func($options['callback'], $resources);
     }
     if ($items instanceof IlluminatePaginator) {
         $paginatorInterface = array_key_exists('paginatorInterface', $options) ? $options['paginatorInterface'] : null;
         $this->paginateCollection($resources, $items, $paginatorInterface);
     }
     return isset($options['raw']) ? $resources : $this->buildResponse($resources);
 }
Example #5
0
 /**
  * Response for collection of items
  *
  * @param $data
  * @param callable|\League\Fractal\TransformerAbstract $transformer
  * @param string $resourceKey
  * @param Cursor $cursor
  * @param array $meta
  * @return mixed
  */
 public function withCollection($data, $transformer, $resourceKey = null, Cursor $cursor = null, $meta = [])
 {
     $resource = new Collection($data, $transformer, $resourceKey);
     foreach ($meta as $metaKey => $metaValue) {
         $resource->setMetaValue($metaKey, $metaValue);
     }
     if (!is_null($cursor)) {
         $resource->setCursor($cursor);
     }
     $rootScope = $this->manager->createData($resource);
     return $this->withArray($rootScope->toArray());
 }
 /**
  * Respond with a given collection.
  *
  * @param $collection
  * @param int $skip
  * @param int $limit
  *
  * @return mixed
  */
 protected function respondWithCollection($collection, $skip = 0, $limit = 0)
 {
     $resource = new Collection($collection, $this->transformer, $this->resourceKeyPlural);
     if ($limit) {
         $cursor = new Cursor($skip, $skip + $limit, $collection->count());
         $resource->setCursor($cursor);
     }
     $rootScope = $this->prepareRootScope($resource);
     return $this->respond($rootScope->toArray());
 }
 public function testCursorOutput()
 {
     $manager = new Manager();
     $inputData = array(array('foo' => 'bar', 'baz' => 'ban'));
     $collection = new Collection($inputData, function (array $data) {
         return $data;
     });
     $cursor = new Cursor(0, 'ban', 'ban', 2);
     $collection->setCursor($cursor);
     $rootScope = $manager->createData($collection);
     $expectedOutput = array('meta' => array('cursor' => array('current' => 0, 'prev' => 'ban', 'next' => 'ban', 'count' => 2)), 'data' => $inputData);
     $this->assertEquals($expectedOutput, $rootScope->toArray());
 }
Example #8
0
 /**
  * @param $paginator
  * @param $callback
  * @return JsonResponse
  */
 protected function respondWithCursor($paginator, $callback)
 {
     $pagination = $paginator->getPaginate();
     $resource = new Collection($pagination->items, $callback);
     $cursor = new Cursor($pagination->current, $pagination->before, $pagination->next, $pagination->total_items);
     $resource->setCursor($cursor);
     $rootScope = $this->fractal->createData($resource);
     return $this->respondWithArray($rootScope->toArray());
 }