122 lines
3.3 KiB
PHP
122 lines
3.3 KiB
PHP
<?php
|
|
|
|
|
|
namespace Ardent\GrocyApi;
|
|
|
|
|
|
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
|
|
{
|
|
private Client $client;
|
|
|
|
public function __construct(string $host, string $apikey)
|
|
{
|
|
$this->client = new Client($host, $apikey);
|
|
}
|
|
|
|
/**
|
|
* @return int the id of the created object
|
|
*/
|
|
private function addObject(string $entity, AbstractApiObject $object): int
|
|
{
|
|
$data = $this->client->post(
|
|
sprintf('objects/%s', $entity),
|
|
ObjectTransformer::normalize($object)
|
|
);
|
|
return $data['created_object_id'];
|
|
}
|
|
|
|
/**
|
|
* @return ProductsObject[]
|
|
*/
|
|
public function getProducts(): array
|
|
{
|
|
return $this->getObjects('products');
|
|
}
|
|
|
|
/**
|
|
* @return QuantityUnitsObject[]
|
|
*/
|
|
public function getQuantityUnits(): array
|
|
{
|
|
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);
|
|
}
|
|
|
|
public function setProduct(ProductsObject $product, int $id): void
|
|
{
|
|
$this->setObject('products', $product, $id);
|
|
}
|
|
|
|
public function addProduct(ProductsObject $product): int
|
|
{
|
|
return $this->addObject('products', $product);
|
|
}
|
|
|
|
public function getUserFieldsForObject(AbstractApiObject $object): array
|
|
{
|
|
return $this->client->get(sprintf('userfields/%s/%s', $object::getEntity(), $object->getId()));
|
|
}
|
|
|
|
public function setUserFieldsForObject(AbstractApiObject $object, array $userfields)
|
|
{
|
|
$this->client->put(sprintf('userfields/%s/%s', $object::getEntity(), $object->getId()), $userfields);
|
|
}
|
|
|
|
public function setProductPicnicId(int $productId, int $picnicId)
|
|
{
|
|
$this->setUserFieldsForObject((new ProductsObject())->setId($productId), [
|
|
'picnic' => $picnicId,
|
|
]);
|
|
}
|
|
|
|
public function getStockClient(): StockClient
|
|
{
|
|
return new StockClient($this->client);
|
|
}
|
|
|
|
/**
|
|
* @return AbstractApiObject[]
|
|
*/
|
|
private function getObjects(string $entity, array $query = [], string $order = null, int $limit = null, int $offset = null)
|
|
{
|
|
$array = $this->client->get(sprintf('objects/%s', $entity), compact('order', 'limit', 'offset'));
|
|
$objects = [];
|
|
foreach ($array as $rawObject) {
|
|
$objects[] = ObjectTransformer::denormalize($rawObject, $entity);
|
|
}
|
|
|
|
return $objects;
|
|
}
|
|
|
|
private function getObject(string $entity, int $id): AbstractApiObject
|
|
{
|
|
$rawObject = $this->client->get(sprintf('objects/%s/%s', $entity, $id));
|
|
return ObjectTransformer::denormalize($rawObject, $entity);
|
|
}
|
|
|
|
private function setObject(string $entity, AbstractApiObject $object, int $id = null): void
|
|
{
|
|
$this->client->put(
|
|
sprintf('objects/%s/%s', $entity, $id),
|
|
ObjectTransformer::normalize($object)
|
|
);
|
|
}
|
|
} |