Cleanup old objects and add serializer

This commit is contained in:
Tim 2021-10-03 20:44:09 +02:00
parent c823696937
commit 44bd43128b
10 changed files with 3 additions and 714 deletions

View File

@ -12,7 +12,8 @@
"require": {
"php": "^8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.0"
"guzzlehttp/guzzle": "^6.0",
"symfony/serializer": "^5.0"
},
"autoload": {
"psr-4": {

View File

@ -15,7 +15,7 @@ class GrocyClient
private function getObjects(string $object, array $query = [], $order = null, $limit = null, $offset = null)
{
return $this->client->get($object, compact('order', 'limit', 'offset'));
return $this->client->get(sprintf('objects/%s', $object), compact('order', 'limit', 'offset'));
}
private function getObject(string $object, int $id)

View File

@ -6,19 +6,6 @@ namespace Ardent\GrocyApi\Objects;
class ApiListTransformer
{
private static function list(string $type): ?string
{
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,
Delivery::getType() => Delivery::class,
default => null,
};
}
public static function getObjects(array $items): array
{

View File

@ -1,57 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
class Category extends AbstractApiObject
{
static public function getType(): string
{
return 'CATEGORY';
}
/**
* Category constructor.
*/
public function __construct(
private string $id,
private string $name,
private array $items,
)
{
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
$apiObject['name'],
ApiListTransformer::getObjects($apiObject['items']),
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return SingleArticle[]
*/
public function getArticles(): array
{
return $this->items;
}
}

View File

@ -1,107 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
class Delivery extends AbstractApiObject
{
/**
* Delivery constructor.
*/
public function __construct(
private string $id,
private string $deliveryId,
private string $creationTime,
private array $slot,
private array $eta2,
private string $status,
private array $deliveryTime,
private array $orders,
)
{
}
static public function getType(): string
{
return 'DELIVERY';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
$apiObject['delivery_id'],
$apiObject['creation_time'],
$apiObject['slot'],
$apiObject['eta2'],
$apiObject['status'],
$apiObject['delivery_time'],
ApiListTransformer::getObjects($apiObject['orders']),
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getDeliveryId(): string
{
return $this->deliveryId;
}
/**
* @return string
*/
public function getCreationTime(): string
{
return $this->creationTime;
}
/**
* @return array
*/
public function getSlot(): array
{
return $this->slot;
}
/**
* @return array
*/
public function getEta2(): array
{
return $this->eta2;
}
/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
/**
* @return array
*/
public function getDeliveryTime(): array
{
return $this->deliveryTime;
}
/**
* @return Order[]
*/
public function getOrders(): array
{
return $this->orders;
}
}

View File

@ -1,97 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
class Order extends AbstractApiObject
{
/**
* Order constructor.
*/
public function __construct(
private string $id,
private array $items,
private array $deliverySlots,
private array $selectedSlot,
private int $totalCount,
private int $totalPrice,
private int $checkoutTotalPrice,
)
{
}
static public function getType(): string
{
return 'ORDER';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
ApiListTransformer::getObjects($apiObject['items']),
$apiObject['delivery_slots'] ?? [],
$apiObject['selected_slot'] ?? [],
$apiObject['total_count'] ?? -1,
$apiObject['total_price'],
$apiObject['checkout_total_price'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->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;
}
}

View File

@ -1,93 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
use Ardent\GrocyApi\Client;
class OrderArticle extends AbstractApiObject
{
/**
* OrderArticle constructor.
*/
public function __construct(
private string $id,
private string $name,
private array $imageIds,
private string $unitQuantity,
private int $price,
private int $maxCount,
)
{
}
static public function getType(): string
{
return 'ORDER_ARTICLE';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
$apiObject['name'],
$apiObject['image_ids'],
$apiObject['unit_quantity'],
$apiObject['price'],
$apiObject['max_count'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @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);
}
/**
* @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;
}
}

View File

@ -1,68 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
class OrderLine extends AbstractApiObject
{
/**
* OrderLine constructor.
*/
public function __construct(
private string $id,
private array $items,
private int $displayPrice,
private int $price,
)
{
}
static public function getType(): string
{
return 'ORDER_LINE';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
dump($apiObject);
return new self(
$apiObject['id'],
ApiListTransformer::getObjects($apiObject['items']),
$apiObject['display_price'],
$apiObject['price'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->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;
}
}

View File

@ -1,124 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
use Ardent\GrocyApi\Client;
class SingleArticle extends AbstractApiObject
{
static public function getType(): string
{
return 'SINGLE_ARTICLE';
}
/**
* 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,
)
{
}
static public function fromApi(array $apiObject): self
{
return new self(
$apiObject['id'],
$apiObject['name'],
$apiObject['price'] ?? 0,
$apiObject['display_price'],
$apiObject['image_id'],
$apiObject['max_count'],
$apiObject['unit_quantity'],
$apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['decorators']
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return int
*/
public function getPrice(): float
{
return $this->price / 100.0;
}
/**
* @return int
*/
public function getDisplayPrice(): float
{
return $this->displayPrice / 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 int
*/
public function getMaxCount(): int
{
return $this->maxCount;
}
/**
* @return string
*/
public function getUnitQuantity(): string
{
return $this->unitQuantity;
}
/**
* @return string
*/
public function getUnitQuantitySub(): string
{
return $this->unitQuantitySub;
}
/**
* @return array
*/
public function getDecorators(): array
{
return $this->decorators;
}
}

View File

@ -1,153 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
use Ardent\GrocyApi\Client;
class SingleArticleDetails extends AbstractApiObject
{
static public function getType(): string
{
return 'SINGLE_ARTICLE_DETAILS';
}
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,
)
{
}
static public function fromApi(array $apiObject): self
{
return new self(
$apiObject['id'],
$apiObject['name'],
$apiObject['price'] ?? 0,
$apiObject['display_price'],
$apiObject['deposit'] ?? 0,
$apiObject['image_id'],
$apiObject['image_ids'],
$apiObject['max_count'],
$apiObject['unit_quantity'],
$apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['decorators'],
$apiObject['nutritional_values'],
$apiObject['ingredients_blob'],
$apiObject['additional_info'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->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;
}
}