From 02f46d395d3f8c7b11467127e47a1f0e21a11d78 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 13 Oct 2021 00:40:35 +0200 Subject: [PATCH] Make class for decorators to make them easier to use --- src/Objects/ArticleInterface.php | 2 +- src/Objects/Decorators.php | 57 ++++++++++++++++++++++++++++ src/Objects/OrderArticle.php | 21 +++++----- src/Objects/OrderLine.php | 28 ++++++-------- src/Objects/SingleArticle.php | 22 +++++------ src/Objects/SingleArticleDetails.php | 32 ++++++++-------- src/PicnicClient.php | 5 +++ 7 files changed, 113 insertions(+), 54 deletions(-) create mode 100644 src/Objects/Decorators.php diff --git a/src/Objects/ArticleInterface.php b/src/Objects/ArticleInterface.php index e386a78..345375f 100644 --- a/src/Objects/ArticleInterface.php +++ b/src/Objects/ArticleInterface.php @@ -12,5 +12,5 @@ interface ArticleInterface public function getUnitQuantitySub(): string; public function getPrice(): float; public function getMaxCount(): int; - public function getDecorators(): array; + public function getDecorators(): Decorators; } \ No newline at end of file diff --git a/src/Objects/Decorators.php b/src/Objects/Decorators.php new file mode 100644 index 0000000..2337907 --- /dev/null +++ b/src/Objects/Decorators.php @@ -0,0 +1,57 @@ +decorators[$rawDecorator['type']] = $rawDecorator; + } + } + + public function getDecorator(string $type): ?array + { + return $this->decorators[$type] ?? null; + } + + public function hasDecorator(string $type): bool + { + return isset($this->decorators[$type]); + } + + public function getQuantity(): string + { + $quantity = $this->getDecorator(self::QUANTITY); + return $quantity ? $quantity['quantity'] : '-'; + } + + public function getFreshLabel(): string + { + $label = $this->getDecorator(self::FRESH_LABEL); + if (!$label) { + return '-'; + } + return match ($label['period']) { + 'P2D' => 'minimal 2 days', + 'P3D' => 'minimal 3 days', + 'P4D' => 'minimal 4 days', + 'P5D' => 'minimal 5 days', + 'P7D' => 'minimal 7 days', + default => 'unknown period ' . $label['period'], + }; + } + + public function getRawDecorators(): array + { + return $this->decorators; + } +} \ No newline at end of file diff --git a/src/Objects/OrderArticle.php b/src/Objects/OrderArticle.php index 908ccce..fe99978 100644 --- a/src/Objects/OrderArticle.php +++ b/src/Objects/OrderArticle.php @@ -12,14 +12,14 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface * OrderArticle constructor. */ public function __construct( - private string $id, - private string $name, - private array $imageIds, - private string $unitQuantity, - private string $unitQuantitySub, - private int $price, - private int $maxCount, - private array $decorators, + private string $id, + private string $name, + private array $imageIds, + private string $unitQuantity, + private string $unitQuantitySub, + private int $price, + private int $maxCount, + private Decorators $decorators, ) { } @@ -31,6 +31,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface static public function fromApi(array $apiObject): AbstractApiObject { + dump($apiObject); return new self( $apiObject['id'], $apiObject['name'], @@ -39,7 +40,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface $apiObject['unit_quantity_sub'] ?? 'n/a', $apiObject['price'], $apiObject['max_count'], - $apiObject['decorators'], + new Decorators($apiObject['decorators']), ); } @@ -95,7 +96,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface return $this->maxCount; } - public function getDecorators(): array + public function getDecorators(): Decorators { return $this->decorators; } diff --git a/src/Objects/OrderLine.php b/src/Objects/OrderLine.php index 02ba7c0..1883642 100644 --- a/src/Objects/OrderLine.php +++ b/src/Objects/OrderLine.php @@ -11,9 +11,9 @@ class OrderLine extends AbstractApiObject */ public function __construct( private string $id, - private array $items, - private int $displayPrice, - private int $price, + private array $items, + private int $displayPrice, + private int $price, ) { } @@ -34,9 +34,6 @@ class OrderLine extends AbstractApiObject ); } - /** - * @return string - */ public function getId(): string { return $this->id; @@ -50,19 +47,18 @@ class OrderLine extends AbstractApiObject return $this->items; } - /** - * @return int - */ - public function getDisplayPrice(): int + public function getDisplayPrice(): float { - return $this->displayPrice; + return $this->displayPrice / 100.0; } - /** - * @return int - */ - public function getPrice(): int + public function getPrice(): float { - return $this->price; + return $this->price / 100.0; + } + + public function getCount(): int + { + return count($this->items); } } \ No newline at end of file diff --git a/src/Objects/SingleArticle.php b/src/Objects/SingleArticle.php index 8991f4a..be9e6a0 100644 --- a/src/Objects/SingleArticle.php +++ b/src/Objects/SingleArticle.php @@ -17,15 +17,15 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface * SingleArticle constructor. */ public function __construct( - private string $id, - private string $name, - private int $price, - private int $displayPrice, - private string $imageId, - private int $maxCount, - private string $unitQuantity, - private string $unitQuantitySub, - private array $decorators, + private string $id, + private string $name, + private int $price, + private int $displayPrice, + private string $imageId, + private int $maxCount, + private string $unitQuantity, + private string $unitQuantitySub, + private Decorators $decorators, ) { } @@ -41,7 +41,7 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface $apiObject['max_count'], $apiObject['unit_quantity'], $apiObject['unit_quantity_sub'] ?? 'n/a', - $apiObject['decorators'] + new Decorators($apiObject['decorators']), ); } @@ -90,7 +90,7 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface return $this->unitQuantitySub; } - public function getDecorators(): array + public function getDecorators(): Decorators { return $this->decorators; } diff --git a/src/Objects/SingleArticleDetails.php b/src/Objects/SingleArticleDetails.php index 1178838..d0f0521 100644 --- a/src/Objects/SingleArticleDetails.php +++ b/src/Objects/SingleArticleDetails.php @@ -14,20 +14,20 @@ class SingleArticleDetails extends AbstractApiObject implements ArticleInterface } public function __construct( - private string $id, - private string $name, - private int $price, - private int $displayPrice, - private int $deposit, - private string $imageId, - private array $imageIds, - private int $maxCount, - private string $unitQuantity, - private string $unitQuantitySub, - private array $decorators, - private array $nutritionalValues, - private string $ingredientsBlob, - private string $additionalInfo, + private string $id, + private string $name, + private int $price, + private int $displayPrice, + private int $deposit, + private string $imageId, + private array $imageIds, + private int $maxCount, + private string $unitQuantity, + private string $unitQuantitySub, + private Decorators $decorators, + private array $nutritionalValues, + private string $ingredientsBlob, + private string $additionalInfo, ) { } @@ -45,7 +45,7 @@ class SingleArticleDetails extends AbstractApiObject implements ArticleInterface $apiObject['max_count'], $apiObject['unit_quantity'], $apiObject['unit_quantity_sub'] ?? 'n/a', - $apiObject['decorators'], + new Decorators($apiObject['decorators']), $apiObject['nutritional_values'], $apiObject['ingredients_blob'] ?? '', $apiObject['additional_info'], @@ -131,7 +131,7 @@ class SingleArticleDetails extends AbstractApiObject implements ArticleInterface return $this->unitQuantitySub; } - public function getDecorators(): array + public function getDecorators(): Decorators { return $this->decorators; } diff --git a/src/PicnicClient.php b/src/PicnicClient.php index 65f2cfc..c747eb4 100644 --- a/src/PicnicClient.php +++ b/src/PicnicClient.php @@ -58,6 +58,11 @@ class PicnicClient return ApiListTransformer::getObjects($data); } + public function getDelivery(string $deliveryId): Delivery + { + return ApiListTransformer::getObject($this->client->get(sprintf('deliveries/%s', $deliveryId))); + } + /** * @param string $term *