diff --git a/src/Client.php b/src/Client.php index a143527..3344115 100644 --- a/src/Client.php +++ b/src/Client.php @@ -39,7 +39,7 @@ class Client ); } - public function post(string $url, array $data): array + public function post(string $url, array $data): ?array { $data = $this->client->post($url, [ RequestOptions::JSON => $data, diff --git a/src/GrocyClient.php b/src/GrocyClient.php index 4ddfe6f..d191265 100644 --- a/src/GrocyClient.php +++ b/src/GrocyClient.php @@ -8,6 +8,7 @@ use Ardent\GrocyApi\Objects\AbstractApiObject; use Ardent\GrocyApi\Objects\ObjectTransformer; use Ardent\GrocyApi\Objects\ProductsObject; use Ardent\GrocyApi\Objects\QuantityUnitsObject; +use Ardent\GrocyApi\Objects\ShoppingListObject; class GrocyClient { @@ -46,6 +47,14 @@ class GrocyClient return $this->getObjects('quantity_units'); } + /** + * @return ShoppingListObject[] + */ + public function getShoppingListProducts(): array + { + return $this->getObjects('shopping_list'); + } + public function getProduct(int $id): ProductsObject { return $this->getObject('products', $id); @@ -78,6 +87,11 @@ class GrocyClient ]); } + public function getStockClient(): StockClient + { + return new StockClient($this->client); + } + /** * @return AbstractApiObject[] */ diff --git a/src/Objects/ObjectTransformer.php b/src/Objects/ObjectTransformer.php index 347f701..83fd616 100644 --- a/src/Objects/ObjectTransformer.php +++ b/src/Objects/ObjectTransformer.php @@ -12,19 +12,21 @@ use Symfony\Component\Serializer\Serializer; class ObjectTransformer { - private static function objectList(): array + private static function objectClass(string $name): string { - return [ + return match ($name) { ProductsObject::getEntity() => ProductsObject::class, QuantityUnitsObject::getEntity() => QuantityUnitsObject::class, - ]; + ShoppingListObject::getEntity() => ShoppingListObject::class, + default => null, + }; } public static function denormalize($object, string $name): AbstractApiObject { // $name = (new CamelCaseToSnakeCaseNameConverter())->denormalize($name); // sprintf('%s\\%sObject', __NAMESPACE__, ucfirst($name)) - return (new ObjectNormalizer())->denormalize($object, self::objectList()[$name]); + return (new ObjectNormalizer())->denormalize($object, self::objectClass($name)); } public static function normalize(AbstractApiObject $object): array diff --git a/src/Objects/ProductsObject.php b/src/Objects/ProductsObject.php index 3de2750..63b9616 100644 --- a/src/Objects/ProductsObject.php +++ b/src/Objects/ProductsObject.php @@ -93,6 +93,11 @@ class ProductsObject extends AbstractApiObject return $this->userfields; } + public function getPicnicId(): ?int + { + return $this->getUserfields()['picnic'] ?? null; + } + /** * @param array $userfields * diff --git a/src/Objects/ShoppingListObject.php b/src/Objects/ShoppingListObject.php new file mode 100644 index 0000000..e78c6d8 --- /dev/null +++ b/src/Objects/ShoppingListObject.php @@ -0,0 +1,158 @@ +product_id; + } + + /** + * @param int $product_id + * + * @return ShoppingListObject + */ + public function setProductId(int $product_id): ShoppingListObject + { + $this->product_id = $product_id; + return $this; + } + + /** + * @return string|null + */ + public function getNote(): ?string + { + return $this->note; + } + + /** + * @param string|null $note + * + * @return ShoppingListObject + */ + public function setNote(?string $note): ShoppingListObject + { + $this->note = $note; + return $this; + } + + /** + * @return int + */ + public function getAmount(): int + { + return $this->amount; + } + + /** + * @param int $amount + * + * @return ShoppingListObject + */ + public function setAmount(int $amount): ShoppingListObject + { + $this->amount = $amount; + return $this; + } + + /** + * @return string + */ + public function getRowCreatedTimestamp(): string + { + return $this->row_created_timestamp; + } + + /** + * @param string $row_created_timestamp + * + * @return ShoppingListObject + */ + public function setRowCreatedTimestamp(string $row_created_timestamp): ShoppingListObject + { + $this->row_created_timestamp = $row_created_timestamp; + return $this; + } + + /** + * @return int + */ + public function getShoppingListId(): int + { + return $this->shopping_list_id; + } + + /** + * @param int $shopping_list_id + * + * @return ShoppingListObject + */ + public function setShoppingListId(int $shopping_list_id): ShoppingListObject + { + $this->shopping_list_id = $shopping_list_id; + return $this; + } + + /** + * @return int + */ + public function getDone(): int + { + return $this->done; + } + + /** + * @param int $done + * + * @return ShoppingListObject + */ + public function setDone(int $done): ShoppingListObject + { + $this->done = $done; + return $this; + } + + /** + * @return int + */ + public function getQuId(): int + { + return $this->qu_id; + } + + /** + * @param int $qu_id + * + * @return ShoppingListObject + */ + public function setQuId(int $qu_id): ShoppingListObject + { + $this->qu_id = $qu_id; + return $this; + } +} \ No newline at end of file diff --git a/src/StockClient.php b/src/StockClient.php new file mode 100644 index 0000000..bf9c668 --- /dev/null +++ b/src/StockClient.php @@ -0,0 +1,48 @@ +client->post('stock/shoppinglist/add-missing-products', ['list_id' => $listId]); + } + + public function shoppingListClear(int $listId = 1): void + { + $this->client->post('stock/shoppinglist/clear', ['list_id' => $listId]); + } + + public function shoppingListAdd(int $productId, $amount, int $listId = 1, $note = ''): void + { + $this->client->post('stock/shoppinglist/clear', [ + 'product_id' => $productId, + 'list_id' => $listId, + 'product_amount' => $amount, + 'note' => $note, + ]); + } + + /** + * @return array array of StockLockEntry + */ + public function stockAdd(int $productId, int $amount, string $price = "0.00", string $type = 'purchase', ?string $bestBeforeDate = null): array + { + $data = [ + 'amount' => $amount, + 'transaction_type' => $type, + 'price' => $price, + ]; + if ($bestBeforeDate) { + $data['best_before_date'] = $bestBeforeDate; // "2019-01-19" + } + return $this->client->post(sprintf('stock/products/%s/add', $productId), $data); + } +} \ No newline at end of file