/** * @param string|int $id * @param string $className * @param callable $callable * * @return \Symfony\Component\HttpFoundation\Response */ public function get($id, $className, callable $callable) { try { QueryObject::assert($this->serializer, $this->fields, $this->included, new Sorting(), $this->errorBag, $className); $data = $callable(); if (empty($data)) { $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); } $response = $this->response($this->serializer->serialize($data, $this->fields, $this->included)); } catch (Exception $e) { $response = $this->getErrorResponse($e); } return $response; }
/** * @param callable $totalAmountCallable * @param callable $resultsCallable * @param string $route * @param string $className * * @return \Symfony\Component\HttpFoundation\Response */ public function get(callable $totalAmountCallable, callable $resultsCallable, $route, $className) { try { QueryObject::assert($this->serializer, $this->fields, $this->included, $this->sorting, $this->errorBag, $className); $totalAmount = $totalAmountCallable(); if ($totalAmount > 0 && $this->page->size() > 0 && $this->page->number() > ceil($totalAmount / $this->page->size())) { return $this->resourceNotFound(new ErrorBag([new OufOfBoundsError($this->page->number(), $this->page->size())])); } $links = $this->pagePaginationLinks($route, $this->page->number(), $this->page->size(), $totalAmount, $this->fields, $this->sorting, $this->included, $this->filters); $results = $resultsCallable(); $paginatedResource = new PaginatedResource($this->serializer->serialize($results), $this->page->number(), $this->page->size(), $totalAmount, $links); $response = $this->response($paginatedResource); } catch (Exception $e) { $response = $this->getErrorResponse($e); } return $response; }
public function testItCanAssertAndThrowExceptionForInvalidSortParams() { $fields = new Fields(); $included = new Included(); $sorting = new Sorting(); $errorBag = new ErrorBag(); $hasError = false; try { $sorting->addField('superhero', 'ascending'); QueryObject::assert($this->serializer, $fields, $included, $sorting, $errorBag, Post::class); } catch (QueryException $e) { $hasError = true; } $this->assertTrue($hasError); }