Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fixed a bug when the collection is empty and you use the 'all' parame…
…ter.
  • Loading branch information
dtulp committed May 9, 2018
commit 41d9f7e59ef7167f6eda0263a254eb94bc0f2dce
33 changes: 33 additions & 0 deletions src/Paginators/EmptyPaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Swis\JsonApi\Server\Paginators;

use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Collection;

class EmptyPaginator extends AbstractPaginator
{
protected $total;

public function __construct($items = [], $total = 0)
{
$this->items = $items instanceof Collection ? $items : Collection::make($items);
$this->total = 0;
}

public function toArray()
{
return [
'data' => $this->items->toArray(),
'from' => $this->firstItem(),
'path' => $this->path,
'to' => $this->lastItem(),
'total' => $this->total(),
];
}

public function total()
{
return $this->total;
}
}
4 changes: 4 additions & 0 deletions src/Repositories/BaseApiRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Pagination\LengthAwarePaginator;
use Swis\JsonApi\Server\Exceptions\NotFoundException;
use Swis\JsonApi\Server\Paginators\EmptyPaginator;
use Swis\JsonApi\Server\Traits\HandlesRelationships;

abstract class BaseApiRepository implements RepositoryInterface
Expand Down Expand Up @@ -47,6 +48,9 @@ public function paginate($parameters = [])
if (array_key_exists('all', $this->parameters)) {
$collection = $this->query->get();
$total = count($collection);
if (0 == $total) {
return new EmptyPaginator();
}

return new LengthAwarePaginator($collection, $total, $total);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/EmptyPaginatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Swis\JsonApi\Server\Paginators;

use Tests\TestCase;

class EmptyPaginatorTest extends TestCase
{
/** @var EmptyPaginator */
private $emptyPaginator;

protected function setUp()
{
$this->emptyPaginator = new EmptyPaginator();
}

/** @test */
public function test_total()
{
$this->assertEquals(0, $this->emptyPaginator->total());
}

/** @test */
public function test_to_array()
{
$this->assertEquals([], $this->emptyPaginator->toArray()['data']);
$this->assertEquals(0, $this->emptyPaginator->toArray()['total']);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Swis\JsonApi\Server\Exceptions\NotFoundException;
use Swis\JsonApi\Server\Paginators\EmptyPaginator;
use Tests\TestCase;
use Tests\TestClasses\TestModel;
use Tests\TestClasses\TestRepository;
Expand Down Expand Up @@ -118,6 +119,13 @@ public function paginate_with_all_attribute()
$this->assertEquals(1, $this->testRepository->paginate($parameters = ['all' => true])->total());
}

/** @test */
public function paginate_with_all_attribute_empty_collection()
{
$result = $this->testRepositoryWithRelationships->paginate(['all' => true]);
$this->assertInstanceOf(EmptyPaginator::class, $result);
}

/** @test */
public function set_columns()
{
Expand Down