Add getQuantityUnits and set/getUserFieldsForObject
This commit is contained in:
parent
22c4101729
commit
5a65036620
@ -7,6 +7,7 @@ namespace Ardent\GrocyApi;
|
|||||||
use Ardent\GrocyApi\Objects\AbstractApiObject;
|
use Ardent\GrocyApi\Objects\AbstractApiObject;
|
||||||
use Ardent\GrocyApi\Objects\ObjectTransformer;
|
use Ardent\GrocyApi\Objects\ObjectTransformer;
|
||||||
use Ardent\GrocyApi\Objects\ProductsObject;
|
use Ardent\GrocyApi\Objects\ProductsObject;
|
||||||
|
use Ardent\GrocyApi\Objects\QuantityUnitsObject;
|
||||||
|
|
||||||
class GrocyClient
|
class GrocyClient
|
||||||
{
|
{
|
||||||
@ -17,6 +18,66 @@ class GrocyClient
|
|||||||
$this->client = new Client($host, $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');
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return AbstractApiObject[]
|
* @return AbstractApiObject[]
|
||||||
*/
|
*/
|
||||||
@ -44,40 +105,4 @@ class GrocyClient
|
|||||||
ObjectTransformer::normalize($object)
|
ObjectTransformer::normalize($object)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @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'];
|
|
||||||
dd($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ProductsObject[]
|
|
||||||
*/
|
|
||||||
public function getProducts(): array
|
|
||||||
{
|
|
||||||
return $this->getObjects('products');
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -4,6 +4,8 @@
|
|||||||
namespace Ardent\GrocyApi\Objects;
|
namespace Ardent\GrocyApi\Objects;
|
||||||
|
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Annotation\Ignore;
|
||||||
|
|
||||||
abstract class AbstractApiObject
|
abstract class AbstractApiObject
|
||||||
{
|
{
|
||||||
private int $id;
|
private int $id;
|
||||||
@ -21,9 +23,12 @@ abstract class AbstractApiObject
|
|||||||
*
|
*
|
||||||
* @return ProductsObject
|
* @return ProductsObject
|
||||||
*/
|
*/
|
||||||
public function setId(int $id): ProductsObject
|
public function setId(int $id): AbstractApiObject
|
||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Ignore]
|
||||||
|
abstract public static function getEntity(): string;
|
||||||
}
|
}
|
@ -4,35 +4,27 @@
|
|||||||
namespace Ardent\GrocyApi\Objects;
|
namespace Ardent\GrocyApi\Objects;
|
||||||
|
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
||||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
|
||||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
|
||||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||||
use Symfony\Component\Serializer\Serializer;
|
use Symfony\Component\Serializer\Serializer;
|
||||||
|
|
||||||
class ObjectTransformer
|
class ObjectTransformer
|
||||||
{
|
{
|
||||||
public function SerializeObjectToJsonResponse($object, $context = []): JsonResponse
|
private static function objectList(): array
|
||||||
{
|
{
|
||||||
$defaultContext = [
|
return [
|
||||||
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
|
ProductsObject::getEntity() => ProductsObject::class,
|
||||||
return $object->getId();
|
QuantityUnitsObject::getEntity() => QuantityUnitsObject::class,
|
||||||
},
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$serializer = new Serializer([
|
|
||||||
new ObjectNormalizer(),
|
|
||||||
], [new JsonEncoder()]);
|
|
||||||
$json = $serializer->serialize($object, 'json', $context);
|
|
||||||
return new JsonResponse($json, 200, [], true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function denormalize($object, string $name): AbstractApiObject
|
public static function denormalize($object, string $name): AbstractApiObject
|
||||||
{
|
{
|
||||||
return (new ObjectNormalizer())->denormalize($object, sprintf('%s\\%sObject', __NAMESPACE__, ucfirst($name)));
|
// $name = (new CamelCaseToSnakeCaseNameConverter())->denormalize($name);
|
||||||
|
// sprintf('%s\\%sObject', __NAMESPACE__, ucfirst($name))
|
||||||
|
return (new ObjectNormalizer())->denormalize($object, self::objectList()[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function normalize(AbstractApiObject $object): array
|
public static function normalize(AbstractApiObject $object): array
|
||||||
|
@ -23,6 +23,11 @@ class ProductsObject extends AbstractApiObject
|
|||||||
#[Ignore]
|
#[Ignore]
|
||||||
private array $userfields;
|
private array $userfields;
|
||||||
|
|
||||||
|
public static function getEntity(): string
|
||||||
|
{
|
||||||
|
return 'products';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
97
src/Objects/QuantityUnitsObject.php
Normal file
97
src/Objects/QuantityUnitsObject.php
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ardent\GrocyApi\Objects;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Annotation\Ignore;
|
||||||
|
|
||||||
|
class QuantityUnitsObject extends AbstractApiObject
|
||||||
|
{
|
||||||
|
private string $name;
|
||||||
|
|
||||||
|
private ?string $description;
|
||||||
|
|
||||||
|
private string $row_created_timestamp;
|
||||||
|
|
||||||
|
private string $name_plural;
|
||||||
|
|
||||||
|
public static function getEntity(): string
|
||||||
|
{
|
||||||
|
return 'quantity_units';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName(): string
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return QuantityUnitsObject
|
||||||
|
*/
|
||||||
|
public function setName(string $name): QuantityUnitsObject
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getDescription(): ?string
|
||||||
|
{
|
||||||
|
return $this->description;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|null $description
|
||||||
|
*
|
||||||
|
* @return QuantityUnitsObject
|
||||||
|
*/
|
||||||
|
public function setDescription(?string $description): QuantityUnitsObject
|
||||||
|
{
|
||||||
|
$this->description = $description;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getRowCreatedTimestamp(): string
|
||||||
|
{
|
||||||
|
return $this->row_created_timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $row_created_timestamp
|
||||||
|
*
|
||||||
|
* @return QuantityUnitsObject
|
||||||
|
*/
|
||||||
|
public function setRowCreatedTimestamp(string $row_created_timestamp): QuantityUnitsObject
|
||||||
|
{
|
||||||
|
$this->row_created_timestamp = $row_created_timestamp;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNamePlural(): string
|
||||||
|
{
|
||||||
|
return $this->name_plural;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name_plural
|
||||||
|
*
|
||||||
|
* @return QuantityUnitsObject
|
||||||
|
*/
|
||||||
|
public function setNamePlural(string $name_plural): QuantityUnitsObject
|
||||||
|
{
|
||||||
|
$this->name_plural = $name_plural;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user