From 91d9a5a61f94e9ed6ad45df3ab07f2fe248bbd3c Mon Sep 17 00:00:00 2001 From: tim Date: Fri, 16 Apr 2021 01:23:55 +0200 Subject: [PATCH] Make part of the api oo and create oo skeleton --- composer.json | 4 +- src/Objects/AbstractApiObject.php | 20 ++++++ src/Objects/ApiListTransformer.php | 44 ++++++++++++++ src/Objects/Category.php | 57 +++++++++++++++++ src/Objects/Order.php | 97 +++++++++++++++++++++++++++++ src/Objects/OrderArticle.php | 87 ++++++++++++++++++++++++++ src/Objects/OrderLine.php | 67 ++++++++++++++++++++ src/Objects/SingleArticle.php | 98 ++++++++++++++++++++++++++++++ src/PicnicClient.php | 17 +++++- 9 files changed, 487 insertions(+), 4 deletions(-) create mode 100644 src/Objects/AbstractApiObject.php create mode 100644 src/Objects/ApiListTransformer.php create mode 100644 src/Objects/Category.php create mode 100644 src/Objects/Order.php create mode 100644 src/Objects/OrderArticle.php create mode 100644 src/Objects/OrderLine.php create mode 100644 src/Objects/SingleArticle.php diff --git a/composer.json b/composer.json index 06edc27..32c323f 100644 --- a/composer.json +++ b/composer.json @@ -10,9 +10,9 @@ } ], "require": { - "php": "^7.4|^8.0", + "php": "^8.0", "ext-json": "*", - "guzzlehttp/guzzle": "^7.0" + "guzzlehttp/guzzle": "^6.0" }, "autoload": { "psr-4": { diff --git a/src/Objects/AbstractApiObject.php b/src/Objects/AbstractApiObject.php new file mode 100644 index 0000000..c803b6e --- /dev/null +++ b/src/Objects/AbstractApiObject.php @@ -0,0 +1,20 @@ + Category::class, + SingleArticle::getType() => SingleArticle::class, + Order::getType() => Order::class, + OrderLine::getType() => OrderLine::class, + OrderArticle::getType() => OrderArticle::class, + default => null, + }; + } + + public static function getObjects(array $items): array + { + $objects = []; + foreach ($items as $item) { + $object = self::getObject($item); + if ($object) { + $objects[] = $object; + } + } + return $objects; + } + + public static function getObject(mixed $item): mixed + { + $name = self::list($item['type']); + if ($name) { + /** @var AbstractApiObject $name */ + return $name::fromApi($item); + } else { + printf('Unknown object with type "%s"' . PHP_EOL, $item['type']); + dd($item); + } + } +} \ No newline at end of file diff --git a/src/Objects/Category.php b/src/Objects/Category.php new file mode 100644 index 0000000..fcb9868 --- /dev/null +++ b/src/Objects/Category.php @@ -0,0 +1,57 @@ +id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return SingleArticle[] + */ + public function getItems(): array + { + return $this->items; + } +} \ No newline at end of file diff --git a/src/Objects/Order.php b/src/Objects/Order.php new file mode 100644 index 0000000..7c29b90 --- /dev/null +++ b/src/Objects/Order.php @@ -0,0 +1,97 @@ +id; + } + + /** + * @return OrderLine[] + */ + public function getItems(): array + { + return $this->items; + } + + /** + * @return array + */ + public function getDeliverySlots(): array + { + return $this->deliverySlots; + } + + /** + * @return array + */ + public function getSelectedSlot(): array + { + return $this->selectedSlot; + } + + /** + * @return int + */ + public function getTotalCount(): int + { + return $this->totalCount; + } + + /** + * @return int + */ + public function getTotalPrice(): int + { + return $this->totalPrice; + } + + /** + * @return int + */ + public function getCheckoutTotalPrice(): int + { + return $this->checkoutTotalPrice; + } +} \ No newline at end of file diff --git a/src/Objects/OrderArticle.php b/src/Objects/OrderArticle.php new file mode 100644 index 0000000..d0c891c --- /dev/null +++ b/src/Objects/OrderArticle.php @@ -0,0 +1,87 @@ +id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return string[] + */ + public function getImageIds(): array + { + return $this->imageIds; + } + + /** + * @return string + */ + public function getUnitQuantity(): string + { + return $this->unitQuantity; + } + + /** + * @return int + */ + public function getPrice(): int + { + return $this->price; + } + + /** + * @return int + */ + public function getMaxCount(): int + { + return $this->maxCount; + } +} \ No newline at end of file diff --git a/src/Objects/OrderLine.php b/src/Objects/OrderLine.php new file mode 100644 index 0000000..07909ba --- /dev/null +++ b/src/Objects/OrderLine.php @@ -0,0 +1,67 @@ +id; + } + + /** + * @return OrderArticle[] + */ + public function getItems(): array + { + return $this->items; + } + + /** + * @return int + */ + public function getDisplayPrice(): int + { + return $this->displayPrice; + } + + /** + * @return int + */ + public function getPrice(): int + { + return $this->price; + } +} \ No newline at end of file diff --git a/src/Objects/SingleArticle.php b/src/Objects/SingleArticle.php new file mode 100644 index 0000000..efefb37 --- /dev/null +++ b/src/Objects/SingleArticle.php @@ -0,0 +1,98 @@ +id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return int + */ + public function getPrice(): int + { + return $this->price; + } + + /** + * @return int + */ + public function getDisplayPrice(): int + { + return $this->displayPrice; + } + + /** + * @return string + */ + public function getImageId(): string + { + return $this->imageId; + } + + /** + * @return int + */ + public function getMaxCount(): int + { + return $this->maxCount; + } + + /** + * @return string + */ + public function getUnitQuantity(): string + { + return $this->unitQuantity; + } + +} \ No newline at end of file diff --git a/src/PicnicClient.php b/src/PicnicClient.php index 3f93319..fc241c2 100644 --- a/src/PicnicClient.php +++ b/src/PicnicClient.php @@ -4,6 +4,7 @@ namespace Ardent\PicnicApi; +use Ardent\PicnicApi\Objects\ApiListTransformer; use GuzzleHttp\Client; use GuzzleHttp\RequestOptions; @@ -24,12 +25,24 @@ class PicnicClient public function getCart() { - return $this->get('cart'); + return ApiListTransformer::getObject($this->get('cart')); + } + + /** + * Returns all past and current deliveries of the user. + * + * @param bool $summary Return a summary (less data). + * @param array $filters An array with the statusses of the deliveries. For example; ['COMPLETED'] will only get completed deliveries. Possible options include CURRENT, COMPLETED and CANCELLED. + */ + public function getDeliveries(bool $summary = false, array $filters = []): array + { + $summary = $summary ? '/summary' : ''; + return $this->post('deliveries' . $summary, $filters); } public function search(string $term) { - return $this->get(sprintf('search?search_term=%s', urlencode($term))); + return ApiListTransformer::getObjects($this->get(sprintf('search?search_term=%s', urlencode($term)))); } private function post(string $url, array $data): array