Make part of the api oo and create oo skeleton

This commit is contained in:
Tim 2021-04-16 01:23:55 +02:00
parent 8f88a863d1
commit 91d9a5a61f
9 changed files with 487 additions and 4 deletions

View File

@ -10,9 +10,9 @@
}
],
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.0"
"guzzlehttp/guzzle": "^6.0"
},
"autoload": {
"psr-4": {

View File

@ -0,0 +1,20 @@
<?php
namespace Ardent\PicnicApi\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;
// }
abstract static public function fromApi(array $apiObject): self;
}

View File

@ -0,0 +1,44 @@
<?php
namespace Ardent\PicnicApi\Objects;
class ApiListTransformer
{
private static function list(string $type): ?string
{
return match ($type) {
Category::getType() => Category::class,
SingleArticle::getType() => SingleArticle::class,
Order::getType() => Order::class,
OrderLine::getType() => OrderLine::class,
OrderArticle::getType() => OrderArticle::class,
default => null,
};
}
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 {
printf('Unknown object with type "%s"' . PHP_EOL, $item['type']);
dd($item);
}
}
}

57
src/Objects/Category.php Normal file
View File

@ -0,0 +1,57 @@
<?php
namespace Ardent\PicnicApi\Objects;
class Category extends AbstractApiObject
{
static public function getType(): string
{
return 'CATEGORY';
}
/**
* Category constructor.
*/
public function __construct(
private string $id,
private string $name,
private array $items,
)
{
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
$apiObject['name'],
ApiListTransformer::getObjects($apiObject['items']),
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return SingleArticle[]
*/
public function getItems(): array
{
return $this->items;
}
}

97
src/Objects/Order.php Normal file
View File

@ -0,0 +1,97 @@
<?php
namespace Ardent\PicnicApi\Objects;
class Order extends AbstractApiObject
{
/**
* Order constructor.
*/
public function __construct(
private string $id,
private array $items,
private array $deliverySlots,
private array $selectedSlot,
private int $totalCount,
private int $totalPrice,
private int $checkoutTotalPrice,
)
{
}
static public function getType(): string
{
return 'ORDER';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
ApiListTransformer::getObjects($apiObject['items']),
[],
[],
$apiObject['total_count'],
$apiObject['total_price'],
$apiObject['checkout_total_price'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return OrderLine[]
*/
public function getItems(): array
{
return $this->items;
}
/**
* @return array
*/
public function getDeliverySlots(): array
{
return $this->deliverySlots;
}
/**
* @return array
*/
public function getSelectedSlot(): array
{
return $this->selectedSlot;
}
/**
* @return int
*/
public function getTotalCount(): int
{
return $this->totalCount;
}
/**
* @return int
*/
public function getTotalPrice(): int
{
return $this->totalPrice;
}
/**
* @return int
*/
public function getCheckoutTotalPrice(): int
{
return $this->checkoutTotalPrice;
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace Ardent\PicnicApi\Objects;
class OrderArticle extends AbstractApiObject
{
/**
* OrderArticle constructor.
*/
public function __construct(
private string $id,
private string $name,
private array $imageIds,
private string $unitQuantity,
private int $price,
private int $maxCount,
)
{
}
static public function getType(): string
{
return 'ORDER_ARTICLE';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
$apiObject['name'],
$apiObject['image_ids'],
$apiObject['unit_quantity'],
$apiObject['price'],
$apiObject['max_count'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return string[]
*/
public function getImageIds(): array
{
return $this->imageIds;
}
/**
* @return string
*/
public function getUnitQuantity(): string
{
return $this->unitQuantity;
}
/**
* @return int
*/
public function getPrice(): int
{
return $this->price;
}
/**
* @return int
*/
public function getMaxCount(): int
{
return $this->maxCount;
}
}

67
src/Objects/OrderLine.php Normal file
View File

@ -0,0 +1,67 @@
<?php
namespace Ardent\PicnicApi\Objects;
class OrderLine extends AbstractApiObject
{
/**
* OrderLine constructor.
*/
public function __construct(
private string $id,
private array $items,
private int $displayPrice,
private int $price,
)
{
}
static public function getType(): string
{
return 'ORDER_LINE';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
ApiListTransformer::getObjects($apiObject['items']),
$apiObject['display_price'],
$apiObject['price'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return OrderArticle[]
*/
public function getItems(): array
{
return $this->items;
}
/**
* @return int
*/
public function getDisplayPrice(): int
{
return $this->displayPrice;
}
/**
* @return int
*/
public function getPrice(): int
{
return $this->price;
}
}

View File

@ -0,0 +1,98 @@
<?php
namespace Ardent\PicnicApi\Objects;
class SingleArticle extends AbstractApiObject
{
static public function getType(): string
{
return 'SINGLE_ARTICLE';
}
/**
* SingleArticle constructor.
*/
public function __construct(
private string $id,
private string $name,
private int $price,
private int $displayPrice,
private string $imageId,
private int $maxCount,
private string $unitQuantity,
)
{
}
static public function fromApi(array $apiObject): self
{
return new self(
$apiObject['id'],
$apiObject['name'],
$apiObject['price'],
$apiObject['display_price'],
$apiObject['image_id'],
$apiObject['max_count'],
$apiObject['unit_quantity'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return int
*/
public function getPrice(): int
{
return $this->price;
}
/**
* @return int
*/
public function getDisplayPrice(): int
{
return $this->displayPrice;
}
/**
* @return string
*/
public function getImageId(): string
{
return $this->imageId;
}
/**
* @return int
*/
public function getMaxCount(): int
{
return $this->maxCount;
}
/**
* @return string
*/
public function getUnitQuantity(): string
{
return $this->unitQuantity;
}
}

View File

@ -4,6 +4,7 @@
namespace Ardent\PicnicApi;
use Ardent\PicnicApi\Objects\ApiListTransformer;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
@ -24,12 +25,24 @@ class PicnicClient
public function getCart()
{
return $this->get('cart');
return ApiListTransformer::getObject($this->get('cart'));
}
/**
* Returns all past and current deliveries of the user.
*
* @param bool $summary Return a summary (less data).
* @param array $filters An array with the statusses of the deliveries. For example; ['COMPLETED'] will only get completed deliveries. Possible options include CURRENT, COMPLETED and CANCELLED.
*/
public function getDeliveries(bool $summary = false, array $filters = []): array
{
$summary = $summary ? '/summary' : '';
return $this->post('deliveries' . $summary, $filters);
}
public function search(string $term)
{
return $this->get(sprintf('search?search_term=%s', urlencode($term)));
return ApiListTransformer::getObjects($this->get(sprintf('search?search_term=%s', urlencode($term))));
}
private function post(string $url, array $data): array