Beispiel #1
0
 /**
  * Get data from request and create Pagination and PartialResponse events.
  * @param  GetResponseEvent $event Event object with request
  */
 public function onRequest(GetResponseEvent $event)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
         return;
     }
     $request = $event->getRequest();
     $pagination = new Pagination();
     $partialResponse = new PartialResponse();
     if ($request->query->has('page')) {
         $pagination->setPage($request->query->get('page'));
     }
     if ($request->query->has('sort')) {
         $pagination->setSort($request->query->get('sort'));
     }
     if ($request->query->has('items_per_page')) {
         $pagination->setItemsPerPage($request->query->get('items_per_page'));
     }
     if ($request->query->has('fields')) {
         $partialResponse->setFields($request->query->get('fields'));
     }
     $this->paginatorService->setPagination($pagination);
     $this->paginatorService->setPartialResponse($partialResponse);
     /**
      * Append all used parameters from get and post
      */
     $this->paginatorService->setUsedRouteParams(array_merge($request->query->all(), $request->request->all()));
 }
 /**
  * Paginate data
  *
  * @param mixed $data   Data to paginate
  * @param array $params Parameters for Paginator
  *
  * @return array         Paginated data
  */
 public function paginate($data, $params = array())
 {
     $paginator = $this->paginator->paginate($data, $this->pagination->getPage(), $this->pagination->getItemsPerPage(), $params);
     $items['items'] = $paginator->getItems();
     /**
      * Set pagination object only when need
      */
     if ($this->paginationData['itemsPerPage'] < $this->paginationData['itemsCount']) {
         $items['pagination'] = $this->paginationData;
     }
     return $items;
 }
Beispiel #3
0
 /**
  * Paginate data
  * @param  mixed $data   Data to paginate
  * @param  array  $params Prameters for Paginator
  * @return array         Paginated data
  */
 public function paginate($data, $params = array())
 {
     $paginator = $this->paginator->paginate($data, $this->pagination->getPage(), $this->pagination->getItemsPerPage(), $params);
     $items['items'] = $paginator->getItems();
     if (count($items['items']) == 0) {
         throw new NotFoundHttpException('Results was not found.');
     }
     /**
      * Set pagination object only when need
      */
     if ($this->paginationData['itemsPerPage'] < $this->paginationData['itemsCount']) {
         $items['pagination'] = $this->paginationData;
     }
     return $items;
 }