Skip to content

Commit e7da99c

Browse files
committed
Added StockEndpoint::getByProductId
1 parent 23dcfd2 commit e7da99c

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

src/Client/ClientInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getLastResponse(): ?ResponseInterface;
3737
public function request(RequestInterface $request): ResponseInterface;
3838

3939
/**
40-
* @param Query|array<string, scalar|\Stringable|\DateTimeInterface> $query
40+
* @param Query|array<string, scalar|\Stringable|\DateTimeInterface|null> $query The query parameters. Parameters with null values are removed.
4141
*
4242
* @throws ClientExceptionInterface if an error happens while processing the request
4343
* @throws InternalServerErrorException if the server reports an internal server error

src/Client/Endpoint/StockEndpoint.php

+17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Setono\PeakWMS\Client\Endpoint;
66

7+
use Setono\PeakWMS\DataTransferObject\Collection;
78
use Setono\PeakWMS\DataTransferObject\PaginatedCollection;
89
use Setono\PeakWMS\DataTransferObject\Stock\Stock;
910
use Setono\PeakWMS\Request\Query\KeySetPageQuery;
11+
use Setono\PeakWMS\Request\Query\Query;
1012

1113
/**
1214
* @extends Endpoint<Stock>
@@ -34,6 +36,21 @@ public function getPage(KeySetPageQuery $query = null): PaginatedCollection
3436
);
3537
}
3638

39+
public function getByProductId(string $productId, string $variantId = null): Collection
40+
{
41+
/** @var class-string<Collection<Stock>> $signature */
42+
$signature = sprintf('%s<%s>', Collection::class, self::getDataClass());
43+
44+
return $this->mapperBuilder
45+
->mapper()
46+
->map(
47+
$signature,
48+
$this->createSource(
49+
$this->client->get(sprintf('%s/%s', $this->endpoint, $productId), new Query(['variantId' => $variantId])),
50+
),
51+
);
52+
}
53+
3754
/**
3855
* @return \Generator<Stock>
3956
*/

src/Client/Endpoint/StockEndpointInterface.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Setono\PeakWMS\Client\Endpoint;
66

7+
use Setono\PeakWMS\DataTransferObject\Collection;
78
use Setono\PeakWMS\DataTransferObject\PaginatedCollection;
89
use Setono\PeakWMS\DataTransferObject\Stock\Stock;
910
use Setono\PeakWMS\Request\Query\KeySetPageQuery;
@@ -18,6 +19,11 @@ interface StockEndpointInterface extends EndpointInterface
1819
*/
1920
public function getPage(KeySetPageQuery $query = null): PaginatedCollection;
2021

22+
/**
23+
* @return Collection<Stock>
24+
*/
25+
public function getByProductId(string $productId, string $variantId = null): Collection;
26+
2127
/**
2228
* @return iterable<Stock>
2329
*/

src/DataTransferObject/Collection.php

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public function filter(callable $callback): self
5050
return new self(array_values(array_filter($this->items, $callback)));
5151
}
5252

53+
/**
54+
* @param callable(T):numeric $callback
55+
*/
56+
public function sum(callable $callback): int|float
57+
{
58+
return array_sum(array_map($callback, $this->items));
59+
}
60+
5361
/**
5462
* @return list<T>
5563
*/

src/Request/Query/Query.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66

77
class Query implements \Stringable
88
{
9-
public function __construct(
10-
/**
11-
* @var array<string, scalar|\Stringable|\DateTimeInterface> $parameters
12-
*/
13-
protected array $parameters = [],
14-
) {
9+
/** @var array<string, scalar|\Stringable|\DateTimeInterface> */
10+
protected array $parameters;
11+
12+
/**
13+
* Parameters with null values are removed
14+
*
15+
* @param array<string, scalar|\Stringable|\DateTimeInterface|null> $parameters
16+
*/
17+
public function __construct(array $parameters = [])
18+
{
19+
$this->parameters = array_filter($parameters, static function ($element) {
20+
return null !== $element;
21+
});
1522
}
1623

1724
public function isEmpty(): bool
@@ -22,7 +29,7 @@ public function isEmpty(): bool
2229
public function toString(): string
2330
{
2431
return http_build_query(array_map(static function ($element) {
25-
return $element instanceof \DateTimeInterface ? $element->format(\DATE_ATOM) : $element;
32+
return $element instanceof \DateTimeInterface ? $element->format(\DATE_ATOM) : (string) $element;
2633
}, $this->parameters), '', '&', \PHP_QUERY_RFC3986);
2734
}
2835

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+
use PHPUnit\Framework\TestCase;
8+
use Setono\PeakWMS\DataTransferObject\Stock\Stock;
9+
10+
final class CollectionTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
*/
15+
public function it_sums(): void
16+
{
17+
$collection = new Collection([
18+
new Stock(quantity: 1),
19+
new Stock(quantity: 3),
20+
new Stock(quantity: 5),
21+
]);
22+
23+
self::assertSame(9, $collection->sum(fn (Stock $stock): int => (int) $stock->quantity));
24+
}
25+
}

0 commit comments

Comments
 (0)