Skip to content

Commit

Permalink
Add iterate and getPage methods
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 27, 2024
1 parent a6e92ed commit 050d916
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/Client/Endpoint/ProductEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Setono\PeakWMS\Client\Endpoint;

use Setono\PeakWMS\DataTransferObject\Collection;
use Setono\PeakWMS\DataTransferObject\PaginatedCollection;
use Setono\PeakWMS\DataTransferObject\Product\Product;
use Setono\PeakWMS\Request\Query\Product\PageQuery;

/**
* @extends Endpoint<Product>
Expand All @@ -19,6 +21,25 @@ final class ProductEndpoint extends Endpoint implements ProductEndpointInterface

use DeletableEndpointTrait;

/**
* @return PaginatedCollection<Product>
*/
public function getPage(PageQuery $query): PaginatedCollection
{
/** @var class-string<PaginatedCollection<Product>> $signature */
$signature = sprintf('%s<%s>', PaginatedCollection::class, self::getDataClass());

return $this
->mapperBuilder
->mapper()
->map(
$signature,
$this->createSource(
$this->client->get($this->endpoint, $query),
)->map(['data' => 'items']),
);
}

public function getByProductId(string $productId): Collection
{
/** @var class-string<Collection<Product>> $signature */
Expand All @@ -35,6 +56,22 @@ public function getByProductId(string $productId): Collection
);
}

/**
* @return \Generator<array-key, Product>
*/
public function iterate(PageQuery $query = null): \Generator
{
$query ??= PageQuery::create();

do {
$collection = $this->getPage($query);

yield from $collection;

$query->incrementPage();
} while (!$collection->empty());
}

protected static function getDataClass(): string
{
return Product::class;
Expand Down
2 changes: 1 addition & 1 deletion src/DataTransferObject/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @implements \IteratorAggregate<int, T>
* @implements \ArrayAccess<int, T>
*/
final class Collection implements \IteratorAggregate, \Countable, \ArrayAccess
class Collection implements \IteratorAggregate, \Countable, \ArrayAccess
{
/** @var list<T> */
private array $items;
Expand Down
25 changes: 25 additions & 0 deletions src/DataTransferObject/PaginatedCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\DataTransferObject;

/**
* @template T of AbstractDataTransferObject
*
* @extends Collection<T>
*/
final class PaginatedCollection extends Collection
{
/**
* @param list<T> $items
*/
public function __construct(
array $items = [],
public readonly int $page = 0,
public readonly int $pageSize = 0,
public readonly int $totalRecords = 0,
) {
parent::__construct($items);
}
}
35 changes: 35 additions & 0 deletions src/Request/Query/Product/PageQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Request\Query\Product;

use Setono\PeakWMS\Request\Query\Query;

final class PageQuery extends Query
{
/**
* @param int $page The first page is 0, not 1
* @param int $pageSize The maximum page size is 100
*/
public static function create(int $page = 0, int $pageSize = 100, \DateTimeInterface $updatedAfter = null): self
{
return new self(array_filter([
'Page' => $page,
'PageSize' => $pageSize,
'updatedAfter' => $updatedAfter,
], static fn ($value): bool => null !== $value));
}

/**
* @throws \LogicException If the page parameter is not set or is not an integer
*/
public function incrementPage(): void
{
if (!isset($this->parameters['Page']) || !is_int($this->parameters['Page'])) {
throw new \LogicException('The page parameter must be set and be an integer');
}

++$this->parameters['Page'];
}
}

0 comments on commit 050d916

Please sign in to comment.