protected function createPagedCollection(LengthAwarePaginator $paginator, TransformerAbstract $transformer) { $data = $paginator->getCollection(); $collection = new Collection($data, $transformer); $collection->setPaginator(new IlluminatePaginatorAdapter($paginator)); return $this->fractal->createData($collection)->toArray(); }
public function paginatedCollection(Paginator $paginator, $transformer = null, $resourceKey = null) { $paginator->appends(\Request::query()); $resource = new Collection($paginator->getCollection(), $this->getTransformer($transformer), $resourceKey); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return $this->manager->createData($resource)->toArray(); }
public function index() { $forms = Form::paginate(); $resource = new Collection($forms->getCollection(), new FormTransformer(), 'forms'); $resource->setPaginator(new IlluminatePaginatorAdapter($forms)); return $this->fractal()->createData($resource)->toJson(); }
public function createResponseArray($data, $entity, Request $request) { $this->request = $request; $fractal = new Manager(); $fractal->parseIncludes($request->query->get('include', [])); $fractal->setSerializer(new ArraySerializer()); $transformer = $this->transformerFactory->get($entity); if ($data instanceof Pagerfanta) { $pager = $data; $pager->setMaxPerPage($request->query->get('limit', 10)); $pager->setCurrentPage($request->query->get('page', 1)); $results = $pager->getCurrentPageResults(); $resource = new Collection($results, $transformer); $resource->setPaginator(new PagerfantaPaginatorAdapter($pager, [$this, 'paginationRouter'])); } elseif ($data instanceof DoctrineQuery) { $ormAdapter = new DoctrineORMAdapter($data); $pager = new Pagerfanta($ormAdapter); $pager->setMaxPerPage($request->query->get('limit', 10)); $pager->setCurrentPage($request->query->get('page', 1)); $results = $pager->getCurrentPageResults(); $resource = new Collection($results, $transformer); $resource->setPaginator(new PagerfantaPaginatorAdapter($pager, [$this, 'paginationRouter'])); } elseif (is_array($data)) { $resource = new Collection($data, $transformer); } else { $resource = new Item($data, $transformer); } $data = $fractal->createData($resource)->toArray(); return $data; }
/** * @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); }
private function paginateCollection(Collection $collection, IlluminatePaginator $paginator, PaginatorInterface $adapter = null) { if (is_null($adapter)) { $adapter = new IlluminatePaginatorAdapter($paginator); } $collection->setPaginator($adapter); }
/** * * 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()); }
public function index() { $clinics = Clinic::paginate(); $resource = new Collection($clinics->getCollection(), new ClinicTransformer(), 'clinics'); $resource->setPaginator(new IlluminatePaginatorAdapter($clinics)); return $this->fractal()->createData($resource)->toJson(); }
public function index() { $users = User::paginate(); $resource = new Collection($users->getCollection(), new UserTransformer(), 'users'); $resource->setPaginator(new IlluminatePaginatorAdapter($users)); return $this->fractal()->createData($resource)->toJson(); }
public function respondWithPaginatedCollection(AbstractPaginator $paginator, $callback) { $resource = new Collection($paginator->getCollection(), $callback); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $rootScope = $this->fractal->createData($resource); return $rootScope; }
/** * Returns the requested elements as JSON * * @param callable|null $configFactory A function for generating the config * @param array|null $config The API endpoint configuration * * @throws Exception * @throws HttpException */ public function actionGetElements($configFactory = null, array $config = null) { if ($configFactory !== null) { $params = craft()->urlManager->getRouteParams(); $variables = isset($params['variables']) ? $params['variables'] : null; $config = $this->_callWithParams($configFactory, $variables); } // Merge in default config options $config = array_merge(['paginate' => true, 'pageParam' => 'page', 'elementsPerPage' => 100, 'first' => false, 'transformer' => 'Craft\\ElementApi_ElementTransformer'], craft()->config->get('defaults', 'elementapi'), $config); if ($config['pageParam'] == 'p') { throw new Exception('The pageParam setting cannot be set to "p" because that’s the parameter Craft uses to check the requested path.'); } if (!isset($config['elementType'])) { throw new Exception('Element API configs must specify the elementType.'); } /** @var ElementCriteriaModel $criteria */ $criteria = craft()->elements->getCriteria($config['elementType'], ['limit' => null]); if (!empty($config['criteria'])) { $criteria->setAttributes($config['criteria']); } // Load Fractal $pluginPath = craft()->path->getPluginsPath() . 'elementapi/'; require $pluginPath . 'vendor/autoload.php'; $fractal = new Manager(); $fractal->setSerializer(new ArraySerializer()); // Define the transformer if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract) { $transformer = $config['transformer']; } else { Craft::import('plugins.elementapi.ElementApi_ElementTransformer'); $transformer = Craft::createComponent($config['transformer']); } if ($config['first']) { $element = $criteria->first(); if (!$element) { throw new HttpException(404); } $resource = new Item($element, $transformer); } else { if ($config['paginate']) { // Create the paginator require $pluginPath . 'ElementApi_PaginatorAdapter.php'; $paginator = new ElementApi_PaginatorAdapter($config['elementsPerPage'], $criteria->total(), $config['pageParam']); // Fetch this page's elements $criteria->offset = $config['elementsPerPage'] * ($paginator->getCurrentPage() - 1); $criteria->limit = $config['elementsPerPage']; $elements = $criteria->find(); $paginator->setCount(count($elements)); $resource = new Collection($elements, $transformer); $resource->setPaginator($paginator); } else { $resource = new Collection($criteria, $transformer); } } JsonHelper::sendJsonHeaders(); echo $fractal->createData($resource)->toJson(); // End the request craft()->end(); }
public function index(Manager $fractal, TransactionTransformer $transactionTransformer) { $transactions = Transaction::orderBy('time', 'desc')->paginate(10); $collection = new Collection($transactions, $transactionTransformer); $collection->setPaginator(new IlluminatePaginatorAdapter($transactions)); $data = $fractal->createData($collection)->toArray(); return $this->respond($data); }
public function testGetTransformer() { $resource = new Collection($this->simpleCollection, function () { }); $this->assertTrue(is_callable($resource->getTransformer())); $resource = new Collection($this->simpleCollection, 'SomeClass'); $this->assertEquals($resource->getTransformer(), 'SomeClass'); }
protected function respondWithPaginate($paginator, $callback) { $collection = $paginator->getCollection(); $resource = new Collection($collection, $callback); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $rootScope = $this->fractal->createData($resource); return $this->respondWithArray($rootScope->toArray()); }
/** * Manage and finalize the data transformation. * * @param \League\Fractal\Resource\Item|\League\Fractal\Resource\Collection $data * @param int $code * @param array $meta * @return \Illuminate\Http\JsonResponse */ public function transform($data, $code = 200, $meta = []) { $data->setMeta($meta); $manager = new Manager(); $manager->setSerializer(new DataArraySerializer()); $response = $manager->createData($data)->toArray(); return response()->json($response, 200, [], JSON_UNESCAPED_SLASHES); }
/** * @return mixed */ public function paginate() { $paginator = $this->repository->paginate(); $bloco = $paginator->getCollection(); $resource = new Collection($bloco, new CategoriaTecidoTransformer()); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return $paginator; }
/** * @param Paginator $data * @param TransformerAbstract $transformer * @param array $headers * @return \Illuminate\Http\JsonResponse */ public function respondWithPaginator(LengthAwarePaginator $data, TransformerAbstract $transformer, $headers = []) { $manager = new Manager(); $manager->setSerializer(new ArraySerializer()); $resource = new Collection($data->getCollection(), $transformer); $resource->setPaginator(new IlluminatePaginatorAdapter($data)); $response = $manager->createData($resource)->toArray(); return $this->respond(['post' => $this->_request->all(), 'data' => $response['data'], 'meta' => $response['meta'], 'error' => ['global' => '']], $headers); }
/** * create fractal's collection * * @param Collection $collection * @param TransformerAbstract $transformer */ public function createPaginated($paginator, $transformer, $include = []) { $fractal = app(League\Fractal\Manager::class); $fractal->parseIncludes($include); $collection = $paginator->getCollection(); $resource = new Collection($collection, $transformer); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return $fractal->createData($resource); }
/** * @param $key * @param array $resources * @param PaginatorInterface $paginator * @param null|string|array $includes * @return array */ public function transformList($key, $resources, $includes = null, PaginatorInterface $paginator = null) { $transformer = $this->getTransformer($key); $resource = new Collection($resources, $transformer); if ($paginator !== null) { $resource->setPaginator($paginator); } return $this->convertResource($resource, $includes); }
/** * @param object[] $collection * @return \League\Fractal\Scope */ public function transformCollection($collection) { $item = reset($collection); $resource = new Collection($collection, $this->getTransformer(get_class($item)), $this->getResourceName(get_class($item))); if (null !== $this->paginator) { $resource->setPaginator(new IlluminatePaginatorAdapter($this->paginator)); } return $this->manager->createData($resource); }
/** * Respond with a paginator, and a transformer. * * @param LengthAwarePaginator $paginator * @param callable|\League\Fractal\TransformerAbstract $transformer * @param string $resourceKey * @param array $meta * @return ResponseFactory */ public function withPaginator(LengthAwarePaginator $paginator, $transformer, $resourceKey = null, $meta = []) { $resource = new Collection($paginator->items(), $transformer, $resourceKey); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); foreach ($meta as $metaKey => $metaValue) { $resource->setMetaValue($metaKey, $metaValue); } $rootScope = $this->manager->createData($resource); return $this->withArray($rootScope->toArray()); }
protected function createCollection($data, $transformer, $entityType, $paginator = false) { if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) { $entityType = null; } $resource = new Collection($data, $transformer, $entityType); if ($paginator) { $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); } return $this->manager->createData($resource)->toArray(); }
public function index(APIRequest $request) { $limit = $this->getItemsPerPage($request); $events = Event::query(); $events = $this->processDateFilters($request, $events); $this->processOrdering($request, $events); $events = $events->with(['location', 'tags'])->paginate($limit); $resource = new Collection($events->items(), new EventTransformer(), 'event'); $resource->setPaginator(new IlluminatePaginatorAdapter($events)); return $this->fractal->createData($resource)->toArray(); }
public function index(Manager $fractal, BlockTransformer $blockTransformer) { $blocks = Block::orderBy('time', 'desc')->paginate(10); foreach ($blocks as $block) { $block->miner_reward = $block->transactions[0]->outputs[0]->value; } $collection = new Collection($blocks, $blockTransformer); $collection->setPaginator(new IlluminatePaginatorAdapter($blocks)); $data = $fractal->createData($collection)->toArray(); return $this->respond($data); }
protected function respondWithPagination($item, $transformer) { if ($item == null) { return Response::json(null, $this->statusCode); } $paginator = $item; $resource = new Collection($item, $transformer); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $rootScope = $this->fractal->createData($resource); return $this->respondWithArray($rootScope->toArray()); }
public function index(APIRequest $request) { $limit = $this->getItemsPerPage($request); $locations = Location::query(); $locations = $this->processDateFilters($request, $locations); $this->processOrdering($request, $locations); $locations = $locations->paginate($limit); $resource = new Collection($locations->items(), new LocationTransformer(), 'location'); $resource->setPaginator(new IlluminatePaginatorAdapter($locations)); return $this->fractal->createData($resource)->toArray(); }
public function testSerializingCollectionResourceWithMeta() { $this->manager->parseIncludes('author'); $booksData = array(array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => '1997', '_author' => array('name' => 'Bob'))); $resource = new Collection($booksData, new GenericBookTransformer(), 'book'); $resource->setMetaValue('foo', 'bar'); $scope = new Scope($this->manager, $resource); $expected = array('book' => array(array('title' => 'Foo', 'year' => 1991), array('title' => 'Bar', 'year' => 1997)), 'linked' => array('author' => array(array('name' => 'Dave'), array('name' => 'Bob'))), 'meta' => array('foo' => 'bar')); $this->assertEquals($expected, $scope->toArray()); $expectedJson = '{"book":[{"title":"Foo","year":1991},{"title":"Bar","year":1997}],"linked":{"author":[{"name":"Dave"},{"name":"Bob"}]},"meta":{"foo":"bar"}}'; $this->assertEquals($expectedJson, $scope->toJson()); }
/** * Handle domain logic for an action. * * @param array $input * @return PayloadInterface */ public function __invoke(array $input) { //Authorize user to be able to view shifts $this->authorizeUser($input[AuthHandler::TOKEN_ATTRIBUTE]->getMetaData('entity'), 'view', 'shifts'); //Validate input $inputValidator = v::key('startDateTime', v::stringType())->key('endDateTime', v::stringType()); $inputValidator->assert($input); //Retrieve shifts between in time period $shifts = $this->shiftRepository->getShiftsBetween(Carbon::parse($input['startDateTime']), Carbon::parse($input['endDateTime'])); $this->collection->setData($shifts)->setTransformer($this->shiftTransformer); return $this->payload->withStatus(PayloadInterface::OK)->withOutput($this->fractal->parseIncludes(['manager', 'employee'])->createData($this->collection)->toArray()); }
/** * Handle domain logic for an action. * * @param array $input * @return PayloadInterface * @throws UserNotAuthorized */ public function __invoke(array $input) { //Make sure requested user matches auth user //todo: figure out if managers can access all employees' hours if ($input['id'] != $input[AuthHandler::TOKEN_ATTRIBUTE]->getMetaData('id')) { throw new UserNotAuthorized(); } //Get hours and transform to more readable collection $employee = $this->userRepository->getOneByIdOrFail($input['id']); $hours = $this->shiftRepository->getHoursCountGroupedByWeekFor($employee); $this->collection->setData($hours)->setTransformer($this->hoursTransformer); return $this->payload->withStatus(PayloadInterface::OK)->withOutput($this->fractal->createData($this->collection)->toArray()); }
public function transformPaginated(LengthAwarePaginator $paginator) { $collection = $paginator->getCollection(); try { $transformer = TransformerFactory::makeForCollection($collection); $resource = new Collection($collection, $transformer); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return $this->manager->createData($resource)->toArray(); } catch (\OutOfRangeException $e) { $emptyResource = new Collection([], []); $emptyResource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return $this->manager->createData($emptyResource)->toArray(); } }