Fully working version of working with products

This commit is contained in:
Tim 2021-10-04 00:37:36 +02:00
parent 44bd43128b
commit 22c4101729
6 changed files with 305 additions and 51 deletions

View File

@ -48,6 +48,13 @@ class Client
return json_decode($data->getBody()->getContents(), true); 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 public function get(string $url, array $parameters = []): array
{ {
$data = $this->client->get($url, [ $data = $this->client->get($url, [

View File

@ -4,6 +4,10 @@
namespace Ardent\GrocyApi; namespace Ardent\GrocyApi;
use Ardent\GrocyApi\Objects\AbstractApiObject;
use Ardent\GrocyApi\Objects\ObjectTransformer;
use Ardent\GrocyApi\Objects\ProductsObject;
class GrocyClient class GrocyClient
{ {
private Client $client; private Client $client;
@ -13,23 +17,67 @@ class GrocyClient
$this->client = new Client($host, $apikey); $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'); 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);
} }
} }

View File

@ -6,15 +6,24 @@ namespace Ardent\GrocyApi\Objects;
abstract class AbstractApiObject abstract class AbstractApiObject
{ {
abstract static public function getType(): string; private int $id;
//
// static function validateObject(array $apiObject): ?self
// {
// if($apiObject['type'] === self::getType()) {
// return self::fromApi($apiObject);
// }
// return null;
// }
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;
}
} }

View File

@ -1,34 +0,0 @@
<?php
namespace Ardent\GrocyApi\Objects;
class ApiListTransformer
{
public static function getObjects(array $items): array
{
$objects = [];
foreach ($items as $item) {
$object = self::getObject($item);
if ($object) {
$objects[] = $object;
}
}
return $objects;
}
public static function getObject(mixed $item): mixed
{
$name = self::list($item['type']);
if ($name) {
/** @var AbstractApiObject $name */
return $name::fromApi($item);
} else {
return [];
printf('Unknown object with type "%s"' . PHP_EOL, $item['type']);
dd($item);
}
}
}

View File

@ -0,0 +1,47 @@
<?php
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
{
$defaultContext = [
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => 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);
}
}

View File

@ -0,0 +1,177 @@
<?php
namespace Ardent\GrocyApi\Objects;
use Symfony\Component\Serializer\Annotation\Ignore;
class ProductsObject extends AbstractApiObject
{
private string $name;
private string $description;
private int $min_stock_amount = 0;
private int $location_id;
private int $qu_id_stock;
private int $qu_id_purchase;
private int $qu_factor_purchase_to_stock;
#[Ignore]
private array $userfields;
/**
* @return string
*/
public function getName(): string
{
return $this->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;
}
}