Skip to content

Commit

Permalink
Fixing some issue with some versions of Eloquent... changing Collecti…
Browse files Browse the repository at this point in the history
…on behaviour and therefore breaking the lib
  • Loading branch information
nilportugues committed Jul 4, 2016
1 parent fad2f46 commit 27aa1e1
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/NilPortugues/Laravel5/JsonApi/Actions/ListResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
*/

namespace NilPortugues\Laravel5\JsonApi\Actions;
use Exception;
use Illuminate\Database\Eloquent\Collection;
use NilPortugues\Api\JsonApi\Http\PaginatedResource;
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag;
use NilPortugues\Api\JsonApi\Server\Errors\OufOfBoundsError;
use NilPortugues\Api\JsonApi\Server\Query\QueryObject;

/**
* Class ListResource.
Expand All @@ -30,4 +36,68 @@ public function getErrorResponse(\Exception $e)

return parent::getErrorResponse($e);
}


/**
* @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();

if ($results instanceof Collection) {
$results = json_encode(['data' => $results->toArray()]);
}


$paginatedResource = new PaginatedResource(
$results,
$this->page->number(),
$this->page->size(),
$totalAmount,
$links
);

$response = $this->response($paginatedResource);
} catch (Exception $e) {
$response = $this->getErrorResponse($e);
}

return $response;
}

}

0 comments on commit 27aa1e1

Please sign in to comment.