From fe38c9c3f5940602585d0cbc51b5dfded24e6cb0 Mon Sep 17 00:00:00 2001 From: tim Date: Sat, 24 Apr 2021 23:56:37 +0200 Subject: [PATCH] Add more object types --- src/Objects/ApiListTransformer.php | 2 + src/Objects/Category.php | 2 +- src/Objects/SingleArticle.php | 38 +++++-- src/Objects/SingleArticleDetails.php | 153 +++++++++++++++++++++++++++ src/PicnicClient.php | 7 ++ 5 files changed, 195 insertions(+), 7 deletions(-) create mode 100644 src/Objects/SingleArticleDetails.php diff --git a/src/Objects/ApiListTransformer.php b/src/Objects/ApiListTransformer.php index 2f5e6e6..ac6bc69 100644 --- a/src/Objects/ApiListTransformer.php +++ b/src/Objects/ApiListTransformer.php @@ -11,6 +11,7 @@ class ApiListTransformer return match ($type) { Category::getType() => Category::class, SingleArticle::getType() => SingleArticle::class, + SingleArticleDetails::getType() => SingleArticleDetails::class, Order::getType() => Order::class, OrderLine::getType() => OrderLine::class, OrderArticle::getType() => OrderArticle::class, @@ -38,6 +39,7 @@ class ApiListTransformer /** @var AbstractApiObject $name */ return $name::fromApi($item); } else { + return []; printf('Unknown object with type "%s"' . PHP_EOL, $item['type']); dd($item); } diff --git a/src/Objects/Category.php b/src/Objects/Category.php index fcb9868..24d0d0e 100644 --- a/src/Objects/Category.php +++ b/src/Objects/Category.php @@ -50,7 +50,7 @@ class Category extends AbstractApiObject /** * @return SingleArticle[] */ - public function getItems(): array + public function getArticles(): array { return $this->items; } diff --git a/src/Objects/SingleArticle.php b/src/Objects/SingleArticle.php index efefb37..43a758e 100644 --- a/src/Objects/SingleArticle.php +++ b/src/Objects/SingleArticle.php @@ -4,9 +4,11 @@ namespace Ardent\PicnicApi\Objects; +use Ardent\PicnicApi\Client; + class SingleArticle extends AbstractApiObject { - static public function getType(): string + static public function getType(): string { return 'SINGLE_ARTICLE'; } @@ -22,6 +24,8 @@ class SingleArticle extends AbstractApiObject private string $imageId, private int $maxCount, private string $unitQuantity, + private string $unitQuantitySub, + private array $decorators, ) { } @@ -31,11 +35,13 @@ class SingleArticle extends AbstractApiObject return new self( $apiObject['id'], $apiObject['name'], - $apiObject['price'], + $apiObject['price'] ?? 0, $apiObject['display_price'], $apiObject['image_id'], $apiObject['max_count'], $apiObject['unit_quantity'], + $apiObject['unit_quantity_sub'] ?? 'n/a', + $apiObject['decorators'] ); } @@ -58,17 +64,17 @@ class SingleArticle extends AbstractApiObject /** * @return int */ - public function getPrice(): int + public function getPrice(): float { - return $this->price; + return $this->price / 100.0; } /** * @return int */ - public function getDisplayPrice(): int + public function getDisplayPrice(): float { - return $this->displayPrice; + return $this->displayPrice / 100.0; } /** @@ -79,6 +85,11 @@ class SingleArticle extends AbstractApiObject return $this->imageId; } + public function getImageUrl(string $size = 'medium'): string + { + return Client::getImageUri($this->imageId, $size); + } + /** * @return int */ @@ -95,4 +106,19 @@ class SingleArticle extends AbstractApiObject return $this->unitQuantity; } + /** + * @return string + */ + public function getUnitQuantitySub(): string + { + return $this->unitQuantitySub; + } + + /** + * @return array + */ + public function getDecorators(): array + { + return $this->decorators; + } } \ No newline at end of file diff --git a/src/Objects/SingleArticleDetails.php b/src/Objects/SingleArticleDetails.php new file mode 100644 index 0000000..2e548c8 --- /dev/null +++ b/src/Objects/SingleArticleDetails.php @@ -0,0 +1,153 @@ +id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + public function getPrice(): float + { + return $this->price / 100.0; + } + + public function getDisplayPrice(): float + { + return $this->displayPrice / 100.0; + } + + public function getDeposit(): float + { + return $this->deposit / 100.0; + } + + /** + * @return string + */ + public function getImageId(): string + { + return $this->imageId; + } + + public function getImageUrl(string $size = 'medium'): string + { + return Client::getImageUri($this->imageId, $size); + } + + /** + * @return array + */ + public function getImageIds(): array + { + return $this->imageIds; + } + + /** + * @param string $size one of tiny/small/medium/large/extra-large + * + * @return string[] + */ + public function getImageUrls(string $size = 'medium'): array + { + return array_map(function ($id) use ($size) { + return Client::getImageUri($id, $size); + }, $this->imageIds); + } + + public function getMaxCount(): int + { + return $this->maxCount; + } + + public function getUnitQuantity(): string + { + return $this->unitQuantity; + } + + public function getUnitQuantitySub(): string + { + return $this->unitQuantitySub; + } + + public function getDecorators(): array + { + return $this->decorators; + } + + public function getNutritionalValues(): array + { + return $this->nutritionalValues; + } + + public function getIngredientsBlob(): string + { + return $this->ingredientsBlob; + } + + public function getAdditionalInfo(): string + { + return $this->additionalInfo; + } +} \ No newline at end of file diff --git a/src/PicnicClient.php b/src/PicnicClient.php index 3395001..3d3b36a 100644 --- a/src/PicnicClient.php +++ b/src/PicnicClient.php @@ -8,6 +8,8 @@ use Ardent\PicnicApi\Objects\ApiListTransformer; use Ardent\PicnicApi\Objects\Category; use Ardent\PicnicApi\Objects\Delivery; use Ardent\PicnicApi\Objects\Order; +use Ardent\PicnicApi\Objects\SingleArticle; +use Ardent\PicnicApi\Objects\SingleArticleDetails; class PicnicClient { @@ -28,6 +30,11 @@ class PicnicClient return ApiListTransformer::getObject($this->client->get('cart')); } + public function getProduct(string $productId): SingleArticleDetails + { + return ApiListTransformer::getObject($this->client->get(sprintf('product/%s', $productId))['product_details']); + } + /** * Returns all past and current deliveries of the user. *