Skip to content

Commit

Permalink
Added StockEndpoint::getByProductId
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Dec 4, 2024
1 parent 23dcfd2 commit e7da99c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getLastResponse(): ?ResponseInterface;
public function request(RequestInterface $request): ResponseInterface;

/**
* @param Query|array<string, scalar|\Stringable|\DateTimeInterface> $query
* @param Query|array<string, scalar|\Stringable|\DateTimeInterface|null> $query The query parameters. Parameters with null values are removed.
*
* @throws ClientExceptionInterface if an error happens while processing the request
* @throws InternalServerErrorException if the server reports an internal server error
Expand Down
17 changes: 17 additions & 0 deletions src/Client/Endpoint/StockEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Setono\PeakWMS\Client\Endpoint;

use Setono\PeakWMS\DataTransferObject\Collection;
use Setono\PeakWMS\DataTransferObject\PaginatedCollection;
use Setono\PeakWMS\DataTransferObject\Stock\Stock;
use Setono\PeakWMS\Request\Query\KeySetPageQuery;
use Setono\PeakWMS\Request\Query\Query;

/**
* @extends Endpoint<Stock>
Expand Down Expand Up @@ -34,6 +36,21 @@ public function getPage(KeySetPageQuery $query = null): PaginatedCollection
);
}

public function getByProductId(string $productId, string $variantId = null): Collection
{
/** @var class-string<Collection<Stock>> $signature */
$signature = sprintf('%s<%s>', Collection::class, self::getDataClass());

return $this->mapperBuilder
->mapper()
->map(
$signature,
$this->createSource(
$this->client->get(sprintf('%s/%s', $this->endpoint, $productId), new Query(['variantId' => $variantId])),
),
);
}

/**
* @return \Generator<Stock>
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Client/Endpoint/StockEndpointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Setono\PeakWMS\Client\Endpoint;

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

/**
* @return Collection<Stock>
*/
public function getByProductId(string $productId, string $variantId = null): Collection;

/**
* @return iterable<Stock>
*/
Expand Down
8 changes: 8 additions & 0 deletions src/DataTransferObject/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function filter(callable $callback): self
return new self(array_values(array_filter($this->items, $callback)));
}

/**
* @param callable(T):numeric $callback
*/
public function sum(callable $callback): int|float
{
return array_sum(array_map($callback, $this->items));
}

/**
* @return list<T>
*/
Expand Down
21 changes: 14 additions & 7 deletions src/Request/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@

class Query implements \Stringable
{
public function __construct(
/**
* @var array<string, scalar|\Stringable|\DateTimeInterface> $parameters
*/
protected array $parameters = [],
) {
/** @var array<string, scalar|\Stringable|\DateTimeInterface> */
protected array $parameters;

/**
* Parameters with null values are removed
*
* @param array<string, scalar|\Stringable|\DateTimeInterface|null> $parameters
*/
public function __construct(array $parameters = [])
{
$this->parameters = array_filter($parameters, static function ($element) {
return null !== $element;
});
}

public function isEmpty(): bool
Expand All @@ -22,7 +29,7 @@ public function isEmpty(): bool
public function toString(): string
{
return http_build_query(array_map(static function ($element) {
return $element instanceof \DateTimeInterface ? $element->format(\DATE_ATOM) : $element;
return $element instanceof \DateTimeInterface ? $element->format(\DATE_ATOM) : (string) $element;
}, $this->parameters), '', '&', \PHP_QUERY_RFC3986);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/DataTransferObject/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\DataTransferObject;

use PHPUnit\Framework\TestCase;
use Setono\PeakWMS\DataTransferObject\Stock\Stock;

final class CollectionTest extends TestCase
{
/**
* @test
*/
public function it_sums(): void
{
$collection = new Collection([
new Stock(quantity: 1),
new Stock(quantity: 3),
new Stock(quantity: 5),
]);

self::assertSame(9, $collection->sum(fn (Stock $stock): int => (int) $stock->quantity));
}
}

0 comments on commit e7da99c

Please sign in to comment.