Skip to content

Commit 050d916

Browse files
committed
Add iterate and getPage methods
1 parent a6e92ed commit 050d916

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

src/Client/Endpoint/ProductEndpoint.php

+37
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Setono\PeakWMS\Client\Endpoint;
66

77
use Setono\PeakWMS\DataTransferObject\Collection;
8+
use Setono\PeakWMS\DataTransferObject\PaginatedCollection;
89
use Setono\PeakWMS\DataTransferObject\Product\Product;
10+
use Setono\PeakWMS\Request\Query\Product\PageQuery;
911

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

2022
use DeletableEndpointTrait;
2123

24+
/**
25+
* @return PaginatedCollection<Product>
26+
*/
27+
public function getPage(PageQuery $query): PaginatedCollection
28+
{
29+
/** @var class-string<PaginatedCollection<Product>> $signature */
30+
$signature = sprintf('%s<%s>', PaginatedCollection::class, self::getDataClass());
31+
32+
return $this
33+
->mapperBuilder
34+
->mapper()
35+
->map(
36+
$signature,
37+
$this->createSource(
38+
$this->client->get($this->endpoint, $query),
39+
)->map(['data' => 'items']),
40+
);
41+
}
42+
2243
public function getByProductId(string $productId): Collection
2344
{
2445
/** @var class-string<Collection<Product>> $signature */
@@ -35,6 +56,22 @@ public function getByProductId(string $productId): Collection
3556
);
3657
}
3758

59+
/**
60+
* @return \Generator<array-key, Product>
61+
*/
62+
public function iterate(PageQuery $query = null): \Generator
63+
{
64+
$query ??= PageQuery::create();
65+
66+
do {
67+
$collection = $this->getPage($query);
68+
69+
yield from $collection;
70+
71+
$query->incrementPage();
72+
} while (!$collection->empty());
73+
}
74+
3875
protected static function getDataClass(): string
3976
{
4077
return Product::class;

src/DataTransferObject/Collection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @implements \IteratorAggregate<int, T>
1111
* @implements \ArrayAccess<int, T>
1212
*/
13-
final class Collection implements \IteratorAggregate, \Countable, \ArrayAccess
13+
class Collection implements \IteratorAggregate, \Countable, \ArrayAccess
1414
{
1515
/** @var list<T> */
1616
private array $items;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\PeakWMS\DataTransferObject;
6+
7+
/**
8+
* @template T of AbstractDataTransferObject
9+
*
10+
* @extends Collection<T>
11+
*/
12+
final class PaginatedCollection extends Collection
13+
{
14+
/**
15+
* @param list<T> $items
16+
*/
17+
public function __construct(
18+
array $items = [],
19+
public readonly int $page = 0,
20+
public readonly int $pageSize = 0,
21+
public readonly int $totalRecords = 0,
22+
) {
23+
parent::__construct($items);
24+
}
25+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\PeakWMS\Request\Query\Product;
6+
7+
use Setono\PeakWMS\Request\Query\Query;
8+
9+
final class PageQuery extends Query
10+
{
11+
/**
12+
* @param int $page The first page is 0, not 1
13+
* @param int $pageSize The maximum page size is 100
14+
*/
15+
public static function create(int $page = 0, int $pageSize = 100, \DateTimeInterface $updatedAfter = null): self
16+
{
17+
return new self(array_filter([
18+
'Page' => $page,
19+
'PageSize' => $pageSize,
20+
'updatedAfter' => $updatedAfter,
21+
], static fn ($value): bool => null !== $value));
22+
}
23+
24+
/**
25+
* @throws \LogicException If the page parameter is not set or is not an integer
26+
*/
27+
public function incrementPage(): void
28+
{
29+
if (!isset($this->parameters['Page']) || !is_int($this->parameters['Page'])) {
30+
throw new \LogicException('The page parameter must be set and be an integer');
31+
}
32+
33+
++$this->parameters['Page'];
34+
}
35+
}

0 commit comments

Comments
 (0)