Beispiel #1
0
 /**
  * Returns a paginated result
  * 
  * @param Parameters $params
  * @return PayloadInterface
  */
 public function paginate(Parameters $params)
 {
     $sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
     $defaultSize = $sysPrefs->getPaginationSize();
     $page = $params->getPage('number');
     $size = $params->getPage('size', $defaultSize);
     $query = SubdivisionQuery::create();
     // sorting
     $sort = $params->getSort(Subdivision::getSerializer()->getSortFields());
     foreach ($sort as $field => $order) {
         $method = 'orderBy' . NameUtils::toStudlyCase($field);
         $query->{$method}($order);
     }
     // filtering
     $filter = $params->getFilter();
     if (!empty($filter)) {
         $this->applyFilter($query, $filter);
     }
     // paginate
     if ($size == -1) {
         $model = $query->findAll();
     } else {
         $model = $query->paginate($page, $size);
     }
     // run response
     return new Found(['model' => $model]);
 }
 /**
  * @param Request $request
  * @param Found $payload
  */
 public function found(Request $request, Found $payload)
 {
     $params = new Parameters($request->query->all());
     $serializer = RegionType::getSerializer();
     $resource = new Resource($payload->getModel(), $serializer);
     $resource = $resource->with($params->getInclude(['countries', 'subdivisions', 'area']));
     $resource = $resource->fields($params->getFields(['region-type' => RegionType::getSerializer()->getFields(), 'country' => Country::getSerializer()->getFields(), 'subdivision' => Subdivision::getSerializer()->getFields(), 'area' => RegionArea::getSerializer()->getFields()]));
     $document = new Document($resource);
     return new JsonResponse($document->toArray(), 200);
 }
 /**
  * @param Request $request
  * @param Found $payload
  */
 public function found(Request $request, Found $payload)
 {
     $params = new Parameters($request->query->all());
     $data = $payload->getModel();
     $serializer = Country::getSerializer();
     $resource = new Collection($data, $serializer);
     $resource = $resource->with($params->getInclude(['continent', 'currency', 'type', 'subtype', 'subordinates', 'country', 'subdivisions']));
     $resource = $resource->fields($params->getFields(['country' => Country::getSerializer()->getFields(), 'continent' => Continent::getSerializer()->getFields(), 'currency' => Currency::getSerializer()->getFields(), 'type' => RegionType::getSerializer()->getFields(), 'subtype' => RegionType::getSerializer()->getFields(), 'subordinate' => Country::getSerializer()->getFields(), 'subdivision' => Subdivision::getSerializer()->getFields()]));
     $document = new Document($resource);
     // meta
     if ($params->getPage('size') != -1) {
         $document->setMeta(['total' => $data->getNbResults(), 'first' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getFirstPage()]]), 'next' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getNextPage()]]), 'previous' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getPreviousPage()]]), 'last' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getLastPage()]])]);
     }
     // return response
     return new JsonResponse($document->toArray());
 }