Add more object types

This commit is contained in:
Tim 2021-04-24 23:56:37 +02:00
parent f45ebf24f1
commit fe38c9c3f5
5 changed files with 195 additions and 7 deletions

View File

@ -11,6 +11,7 @@ class ApiListTransformer
return match ($type) { return match ($type) {
Category::getType() => Category::class, Category::getType() => Category::class,
SingleArticle::getType() => SingleArticle::class, SingleArticle::getType() => SingleArticle::class,
SingleArticleDetails::getType() => SingleArticleDetails::class,
Order::getType() => Order::class, Order::getType() => Order::class,
OrderLine::getType() => OrderLine::class, OrderLine::getType() => OrderLine::class,
OrderArticle::getType() => OrderArticle::class, OrderArticle::getType() => OrderArticle::class,
@ -38,6 +39,7 @@ class ApiListTransformer
/** @var AbstractApiObject $name */ /** @var AbstractApiObject $name */
return $name::fromApi($item); return $name::fromApi($item);
} else { } else {
return [];
printf('Unknown object with type "%s"' . PHP_EOL, $item['type']); printf('Unknown object with type "%s"' . PHP_EOL, $item['type']);
dd($item); dd($item);
} }

View File

@ -50,7 +50,7 @@ class Category extends AbstractApiObject
/** /**
* @return SingleArticle[] * @return SingleArticle[]
*/ */
public function getItems(): array public function getArticles(): array
{ {
return $this->items; return $this->items;
} }

View File

@ -4,6 +4,8 @@
namespace Ardent\PicnicApi\Objects; namespace Ardent\PicnicApi\Objects;
use Ardent\PicnicApi\Client;
class SingleArticle extends AbstractApiObject class SingleArticle extends AbstractApiObject
{ {
static public function getType(): string static public function getType(): string
@ -22,6 +24,8 @@ class SingleArticle extends AbstractApiObject
private string $imageId, private string $imageId,
private int $maxCount, private int $maxCount,
private string $unitQuantity, private string $unitQuantity,
private string $unitQuantitySub,
private array $decorators,
) )
{ {
} }
@ -31,11 +35,13 @@ class SingleArticle extends AbstractApiObject
return new self( return new self(
$apiObject['id'], $apiObject['id'],
$apiObject['name'], $apiObject['name'],
$apiObject['price'], $apiObject['price'] ?? 0,
$apiObject['display_price'], $apiObject['display_price'],
$apiObject['image_id'], $apiObject['image_id'],
$apiObject['max_count'], $apiObject['max_count'],
$apiObject['unit_quantity'], $apiObject['unit_quantity'],
$apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['decorators']
); );
} }
@ -58,17 +64,17 @@ class SingleArticle extends AbstractApiObject
/** /**
* @return int * @return int
*/ */
public function getPrice(): int public function getPrice(): float
{ {
return $this->price; return $this->price / 100.0;
} }
/** /**
* @return int * @return int
*/ */
public function getDisplayPrice(): int public function getDisplayPrice(): float
{ {
return $this->displayPrice; return $this->displayPrice / 100.0;
} }
/** /**
@ -79,6 +85,11 @@ class SingleArticle extends AbstractApiObject
return $this->imageId; return $this->imageId;
} }
public function getImageUrl(string $size = 'medium'): string
{
return Client::getImageUri($this->imageId, $size);
}
/** /**
* @return int * @return int
*/ */
@ -95,4 +106,19 @@ class SingleArticle extends AbstractApiObject
return $this->unitQuantity; return $this->unitQuantity;
} }
/**
* @return string
*/
public function getUnitQuantitySub(): string
{
return $this->unitQuantitySub;
}
/**
* @return array
*/
public function getDecorators(): array
{
return $this->decorators;
}
} }

View File

@ -0,0 +1,153 @@
<?php
namespace Ardent\PicnicApi\Objects;
use Ardent\PicnicApi\Client;
class SingleArticleDetails extends AbstractApiObject
{
static public function getType(): string
{
return 'SINGLE_ARTICLE_DETAILS';
}
public function __construct(
private string $id,
private string $name,
private int $price,
private int $displayPrice,
private int $deposit,
private string $imageId,
private array $imageIds,
private int $maxCount,
private string $unitQuantity,
private string $unitQuantitySub,
private array $decorators,
private array $nutritionalValues,
private string $ingredientsBlob,
private string $additionalInfo,
)
{
}
static public function fromApi(array $apiObject): self
{
return new self(
$apiObject['id'],
$apiObject['name'],
$apiObject['price'] ?? 0,
$apiObject['display_price'],
$apiObject['deposit'] ?? 0,
$apiObject['image_id'],
$apiObject['image_ids'],
$apiObject['max_count'],
$apiObject['unit_quantity'],
$apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['decorators'],
$apiObject['nutritional_values'],
$apiObject['ingredients_blob'],
$apiObject['additional_info'],
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
public function getPrice(): float
{
return $this->price / 100.0;
}
public function getDisplayPrice(): float
{
return $this->displayPrice / 100.0;
}
public function getDeposit(): float
{
return $this->deposit / 100.0;
}
/**
* @return string
*/
public function getImageId(): string
{
return $this->imageId;
}
public function getImageUrl(string $size = 'medium'): string
{
return Client::getImageUri($this->imageId, $size);
}
/**
* @return array
*/
public function getImageIds(): array
{
return $this->imageIds;
}
/**
* @param string $size one of tiny/small/medium/large/extra-large
*
* @return string[]
*/
public function getImageUrls(string $size = 'medium'): array
{
return array_map(function ($id) use ($size) {
return Client::getImageUri($id, $size);
}, $this->imageIds);
}
public function getMaxCount(): int
{
return $this->maxCount;
}
public function getUnitQuantity(): string
{
return $this->unitQuantity;
}
public function getUnitQuantitySub(): string
{
return $this->unitQuantitySub;
}
public function getDecorators(): array
{
return $this->decorators;
}
public function getNutritionalValues(): array
{
return $this->nutritionalValues;
}
public function getIngredientsBlob(): string
{
return $this->ingredientsBlob;
}
public function getAdditionalInfo(): string
{
return $this->additionalInfo;
}
}

View File

@ -8,6 +8,8 @@ use Ardent\PicnicApi\Objects\ApiListTransformer;
use Ardent\PicnicApi\Objects\Category; use Ardent\PicnicApi\Objects\Category;
use Ardent\PicnicApi\Objects\Delivery; use Ardent\PicnicApi\Objects\Delivery;
use Ardent\PicnicApi\Objects\Order; use Ardent\PicnicApi\Objects\Order;
use Ardent\PicnicApi\Objects\SingleArticle;
use Ardent\PicnicApi\Objects\SingleArticleDetails;
class PicnicClient class PicnicClient
{ {
@ -28,6 +30,11 @@ class PicnicClient
return ApiListTransformer::getObject($this->client->get('cart')); return ApiListTransformer::getObject($this->client->get('cart'));
} }
public function getProduct(string $productId): SingleArticleDetails
{
return ApiListTransformer::getObject($this->client->get(sprintf('product/%s', $productId))['product_details']);
}
/** /**
* Returns all past and current deliveries of the user. * Returns all past and current deliveries of the user.
* *