Skip to content

Commit

Permalink
Merge pull request #9 from saloonphp/feature/add-start-page-property
Browse files Browse the repository at this point in the history
Feature | Provide custom start page
  • Loading branch information
Sammyjo20 authored Oct 7, 2023
2 parents ebfc018 + 778e953 commit bfd4870
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ abstract class Paginator implements Iterator, Countable
*/
protected Request $request;

/**
* The start page when the paginator is rewound
*/
protected int $startPage = 1;

/**
* Internal Marker For The Current Page
*/
Expand Down Expand Up @@ -199,7 +204,7 @@ public function valid(): bool
*/
public function rewind(): void
{
$this->page = 1;
$this->page = $this->startPage;
$this->currentResponse = null;
$this->totalResults = 0;
$this->totalPages = null;
Expand Down Expand Up @@ -310,6 +315,18 @@ public function getPage(): int
return $this->page;
}

/**
* Set the start page of the paginator
*
* Used when the paginator is rewound
*/
public function setStartPage(int $startPage): static
{
$this->startPage = $startPage;

return $this;
}

/**
* Count the iterator
*/
Expand Down
51 changes: 51 additions & 0 deletions tests/Feature/StartPageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

use Saloon\PaginationPlugin\Tests\Fixtures\Connectors\PagedConnector;
use Saloon\PaginationPlugin\Tests\Fixtures\Requests\SuperheroPagedRequest;
use Saloon\PaginationPlugin\Tests\Fixtures\Connectors\CustomStartPagePagedConnector;

test('you can specify a start page on a paginator class', function () {
$connector = new CustomStartPagePagedConnector;
$request = new SuperheroPagedRequest();
$paginator = $connector->paginate($request);

$superheroes = [];
$iteratorCounter = 0;

foreach ($paginator as $item) {
$iteratorCounter++;
$superheroes = array_merge($superheroes, $item->json('data'));
}

expect($iteratorCounter)->toBe(2);
expect($paginator->getTotalResults())->toEqual(10);

$mapped = array_map(static fn (array $superhero) => $superhero['id'], $superheroes);

expect($mapped)->toEqual([11, 12, 13, 14, 15, 16, 17, 18, 19, 20,]);
});

test('you can specify a start page on a paginator instance', function () {
$connector = new PagedConnector;
$request = new SuperheroPagedRequest();
$paginator = $connector->paginate($request);

$superheroes = [];
$iteratorCounter = 0;

$paginator->setStartPage(3);

foreach ($paginator as $item) {
$iteratorCounter++;
$superheroes = array_merge($superheroes, $item->json('data'));
}

expect($iteratorCounter)->toBe(2);
expect($paginator->getTotalResults())->toEqual(10);

$mapped = array_map(static fn (array $superhero) => $superhero['id'], $superheroes);

expect($mapped)->toEqual([11, 12, 13, 14, 15, 16, 17, 18, 19, 20,]);
});
47 changes: 47 additions & 0 deletions tests/Fixtures/Connectors/CustomStartPagePagedConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Saloon\PaginationPlugin\Tests\Fixtures\Connectors;

use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\PaginationPlugin\PagedPaginator;
use Saloon\PaginationPlugin\Contracts\HasPagination;
use Saloon\PaginationPlugin\Contracts\HasRequestPagination;

class CustomStartPagePagedConnector extends TestConnector implements HasPagination
{
/**
* Paginate over each page
*/
public function paginate(Request $request): PagedPaginator
{
if ($request instanceof HasRequestPagination) {
return $request->paginate($this);
}

return new class(connector: $this, request: $request) extends PagedPaginator {
/**
* Set a custom start page
*/
protected int $startPage = 3;

/**
* Check if we are on the last page
*/
protected function isLastPage(Response $response): bool
{
return empty($response->json('next_page_url'));
}

/**
* Get the results from the page
*/
protected function getPageItems(Response $response, Request $request): array
{
return $response->json('data') ?? [];
}
};
}
}

0 comments on commit bfd4870

Please sign in to comment.