diff --git a/src/Client.php b/src/Client.php index 86b7a0b..a143527 100644 --- a/src/Client.php +++ b/src/Client.php @@ -48,6 +48,13 @@ class Client return json_decode($data->getBody()->getContents(), true); } + public function put(string $url, array $data) + { + $this->client->put($url, [ + RequestOptions::JSON => $data, + ]); + } + public function get(string $url, array $parameters = []): array { $data = $this->client->get($url, [ diff --git a/src/GrocyClient.php b/src/GrocyClient.php index 1ab794b..fcd2f0a 100644 --- a/src/GrocyClient.php +++ b/src/GrocyClient.php @@ -4,6 +4,10 @@ namespace Ardent\GrocyApi; +use Ardent\GrocyApi\Objects\AbstractApiObject; +use Ardent\GrocyApi\Objects\ObjectTransformer; +use Ardent\GrocyApi\Objects\ProductsObject; + class GrocyClient { private Client $client; @@ -13,23 +17,67 @@ class GrocyClient $this->client = new Client($host, $apikey); } - private function getObjects(string $object, array $query = [], $order = null, $limit = null, $offset = null) + /** + * @return AbstractApiObject[] + */ + private function getObjects(string $entity, array $query = [], string $order = null, int $limit = null, int $offset = null) { - return $this->client->get(sprintf('objects/%s', $object), compact('order', 'limit', 'offset')); + $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 $object, int $id) + private function getObject(string $entity, int $id): AbstractApiObject { - return $this->client->get(sprintf('%s/%s', $object, $id)); + $rawObject = $this->client->get(sprintf('objects/%s/%s', $entity, $id)); + return ObjectTransformer::denormalize($rawObject, $entity); } - public function getAllProducts() + private function setObject(string $entity, AbstractApiObject $object, int $id = null): void + { + $this->client->put( + sprintf('objects/%s/%s', $entity, $id), + 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) + public function getProduct(int $id): ProductsObject { - $this->getObject('products', $id); + 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 1edc6d0..c8a2c48 100644 --- a/src/Objects/AbstractApiObject.php +++ b/src/Objects/AbstractApiObject.php @@ -6,15 +6,24 @@ namespace Ardent\GrocyApi\Objects; abstract class AbstractApiObject { - abstract static public function getType(): string; -// -// static function validateObject(array $apiObject): ?self -// { -// if($apiObject['type'] === self::getType()) { -// return self::fromApi($apiObject); -// } -// return null; -// } + private int $id; - abstract static public function fromApi(array $apiObject): self; + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @param int $id + * + * @return ProductsObject + */ + public function setId(int $id): ProductsObject + { + $this->id = $id; + return $this; + } } \ No newline at end of file diff --git a/src/Objects/ApiListTransformer.php b/src/Objects/ApiListTransformer.php deleted file mode 100644 index 4fdc9bd..0000000 --- a/src/Objects/ApiListTransformer.php +++ /dev/null @@ -1,34 +0,0 @@ - function ($object, $format, $context) { + return $object->getId(); + }, + ]; + + $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))); + } + + public static function normalize(AbstractApiObject $object): array + { + $classMetaDataFactory = new ClassMetadataFactory(new AnnotationLoader()); + + $serializer = new Serializer([ + new ObjectNormalizer($classMetaDataFactory, new CamelCaseToSnakeCaseNameConverter()), + ]); + return $serializer->normalize($object); + } +} \ No newline at end of file diff --git a/src/Objects/ProductsObject.php b/src/Objects/ProductsObject.php new file mode 100644 index 0000000..db37ef8 --- /dev/null +++ b/src/Objects/ProductsObject.php @@ -0,0 +1,177 @@ +name; + } + + /** + * @param string $name + * + * @return ProductsObject + */ + public function setName(string $name): ProductsObject + { + $this->name = $name; + return $this; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @param string $description + * + * @return ProductsObject + */ + public function setDescription(string $description): ProductsObject + { + $this->description = $description; + return $this; + } + + /** + * @return int + */ + public function getMinStockAmount(): int + { + return $this->min_stock_amount; + } + + /** + * @param int $min_stock_amount + * + * @return ProductsObject + */ + public function setMinStockAmount(int $min_stock_amount): ProductsObject + { + $this->min_stock_amount = $min_stock_amount; + return $this; + } + + /** + * @return array + */ + public function getUserfields(): array + { + return $this->userfields; + } + + /** + * @param array $userfields + * + * @return ProductsObject + */ + public function setUserfields(array $userfields): ProductsObject + { + $this->userfields = $userfields; + return $this; + } + + /** + * @return int + */ + public function getLocationId(): int + { + return $this->location_id; + } + + /** + * @param int $location_id + * + * @return ProductsObject + */ + public function setLocationId(int $location_id): ProductsObject + { + $this->location_id = $location_id; + return $this; + } + + /** + * @return int + */ + public function getQuIdStock(): int + { + return $this->qu_id_stock; + } + + /** + * @param int $qu_id_stock + * + * @return ProductsObject + */ + public function setQuIdStock(int $qu_id_stock): ProductsObject + { + $this->qu_id_stock = $qu_id_stock; + return $this; + } + + /** + * @return int + */ + public function getQuIdPurchase(): int + { + return $this->qu_id_purchase; + } + + /** + * @param int $qu_id_purchase + * + * @return ProductsObject + */ + public function setQuIdPurchase(int $qu_id_purchase): ProductsObject + { + $this->qu_id_purchase = $qu_id_purchase; + return $this; + } + + /** + * @return int + */ + public function getQuFactorPurchaseToStock(): int + { + return $this->qu_factor_purchase_to_stock; + } + + /** + * @param int $qu_factor_purchase_to_stock + * + * @return ProductsObject + */ + public function setQuFactorPurchaseToStock(int $qu_factor_purchase_to_stock): ProductsObject + { + $this->qu_factor_purchase_to_stock = $qu_factor_purchase_to_stock; + return $this; + } +} \ No newline at end of file