From 5a65036620e9caf732e574d97051a2fb968436c0 Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 4 Oct 2021 02:40:45 +0200 Subject: [PATCH] Add getQuantityUnits and set/getUserFieldsForObject --- src/GrocyClient.php | 97 ++++++++++++++++++----------- src/Objects/AbstractApiObject.php | 7 ++- src/Objects/ObjectTransformer.php | 22 +++---- src/Objects/ProductsObject.php | 5 ++ src/Objects/QuantityUnitsObject.php | 97 +++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 52 deletions(-) create mode 100644 src/Objects/QuantityUnitsObject.php diff --git a/src/GrocyClient.php b/src/GrocyClient.php index fcd2f0a..4ddfe6f 100644 --- a/src/GrocyClient.php +++ b/src/GrocyClient.php @@ -7,6 +7,7 @@ namespace Ardent\GrocyApi; use Ardent\GrocyApi\Objects\AbstractApiObject; use Ardent\GrocyApi\Objects\ObjectTransformer; use Ardent\GrocyApi\Objects\ProductsObject; +use Ardent\GrocyApi\Objects\QuantityUnitsObject; class GrocyClient { @@ -17,6 +18,66 @@ class GrocyClient $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[] */ @@ -44,40 +105,4 @@ class GrocyClient 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); - } } \ No newline at end of file diff --git a/src/Objects/AbstractApiObject.php b/src/Objects/AbstractApiObject.php index c8a2c48..7f0d5f3 100644 --- a/src/Objects/AbstractApiObject.php +++ b/src/Objects/AbstractApiObject.php @@ -4,6 +4,8 @@ namespace Ardent\GrocyApi\Objects; +use Symfony\Component\Serializer\Annotation\Ignore; + abstract class AbstractApiObject { private int $id; @@ -21,9 +23,12 @@ abstract class AbstractApiObject * * @return ProductsObject */ - public function setId(int $id): ProductsObject + public function setId(int $id): AbstractApiObject { $this->id = $id; return $this; } + + #[Ignore] + abstract public static function getEntity(): string; } \ No newline at end of file diff --git a/src/Objects/ObjectTransformer.php b/src/Objects/ObjectTransformer.php index 79563fe..347f701 100644 --- a/src/Objects/ObjectTransformer.php +++ b/src/Objects/ObjectTransformer.php @@ -4,35 +4,27 @@ 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\Loader\AnnotationLoader; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; -use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; class ObjectTransformer { - public function SerializeObjectToJsonResponse($object, $context = []): JsonResponse + private static function objectList(): array { - $defaultContext = [ - AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) { - return $object->getId(); - }, + return [ + ProductsObject::getEntity() => ProductsObject::class, + 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 { - 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 diff --git a/src/Objects/ProductsObject.php b/src/Objects/ProductsObject.php index db37ef8..3de2750 100644 --- a/src/Objects/ProductsObject.php +++ b/src/Objects/ProductsObject.php @@ -23,6 +23,11 @@ class ProductsObject extends AbstractApiObject #[Ignore] private array $userfields; + public static function getEntity(): string + { + return 'products'; + } + /** * @return string */ diff --git a/src/Objects/QuantityUnitsObject.php b/src/Objects/QuantityUnitsObject.php new file mode 100644 index 0000000..09683b9 --- /dev/null +++ b/src/Objects/QuantityUnitsObject.php @@ -0,0 +1,97 @@ +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; + } +} \ No newline at end of file