Make class for decorators to make them easier to use

This commit is contained in:
Tim 2021-10-13 00:40:35 +02:00
parent 181879a529
commit 02f46d395d
7 changed files with 113 additions and 54 deletions

View File

@ -12,5 +12,5 @@ interface ArticleInterface
public function getUnitQuantitySub(): string;
public function getPrice(): float;
public function getMaxCount(): int;
public function getDecorators(): array;
public function getDecorators(): Decorators;
}

View File

@ -0,0 +1,57 @@
<?php
namespace Ardent\PicnicApi\Objects;
class Decorators
{
public const IMMUTABLE = 'IMMUTABLE';
public const QUANTITY = 'QUANTITY';
public const UNIT_QUANTITY = 'UNIT_QUANTITY';
public const FRESH_LABEL = 'FRESH_LABEL';
private array $decorators = [];
public function __construct(array $rawDecorators)
{
foreach ($rawDecorators as $rawDecorator) {
$this->decorators[$rawDecorator['type']] = $rawDecorator;
}
}
public function getDecorator(string $type): ?array
{
return $this->decorators[$type] ?? null;
}
public function hasDecorator(string $type): bool
{
return isset($this->decorators[$type]);
}
public function getQuantity(): string
{
$quantity = $this->getDecorator(self::QUANTITY);
return $quantity ? $quantity['quantity'] : '-';
}
public function getFreshLabel(): string
{
$label = $this->getDecorator(self::FRESH_LABEL);
if (!$label) {
return '-';
}
return match ($label['period']) {
'P2D' => 'minimal 2 days',
'P3D' => 'minimal 3 days',
'P4D' => 'minimal 4 days',
'P5D' => 'minimal 5 days',
'P7D' => 'minimal 7 days',
default => 'unknown period ' . $label['period'],
};
}
public function getRawDecorators(): array
{
return $this->decorators;
}
}

View File

@ -19,7 +19,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface
private string $unitQuantitySub,
private int $price,
private int $maxCount,
private array $decorators,
private Decorators $decorators,
)
{
}
@ -31,6 +31,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface
static public function fromApi(array $apiObject): AbstractApiObject
{
dump($apiObject);
return new self(
$apiObject['id'],
$apiObject['name'],
@ -39,7 +40,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface
$apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['price'],
$apiObject['max_count'],
$apiObject['decorators'],
new Decorators($apiObject['decorators']),
);
}
@ -95,7 +96,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface
return $this->maxCount;
}
public function getDecorators(): array
public function getDecorators(): Decorators
{
return $this->decorators;
}

View File

@ -34,9 +34,6 @@ class OrderLine extends AbstractApiObject
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
@ -50,19 +47,18 @@ class OrderLine extends AbstractApiObject
return $this->items;
}
/**
* @return int
*/
public function getDisplayPrice(): int
public function getDisplayPrice(): float
{
return $this->displayPrice;
return $this->displayPrice / 100.0;
}
/**
* @return int
*/
public function getPrice(): int
public function getPrice(): float
{
return $this->price;
return $this->price / 100.0;
}
public function getCount(): int
{
return count($this->items);
}
}

View File

@ -25,7 +25,7 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface
private int $maxCount,
private string $unitQuantity,
private string $unitQuantitySub,
private array $decorators,
private Decorators $decorators,
)
{
}
@ -41,7 +41,7 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface
$apiObject['max_count'],
$apiObject['unit_quantity'],
$apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['decorators']
new Decorators($apiObject['decorators']),
);
}
@ -90,7 +90,7 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface
return $this->unitQuantitySub;
}
public function getDecorators(): array
public function getDecorators(): Decorators
{
return $this->decorators;
}

View File

@ -24,7 +24,7 @@ class SingleArticleDetails extends AbstractApiObject implements ArticleInterface
private int $maxCount,
private string $unitQuantity,
private string $unitQuantitySub,
private array $decorators,
private Decorators $decorators,
private array $nutritionalValues,
private string $ingredientsBlob,
private string $additionalInfo,
@ -45,7 +45,7 @@ class SingleArticleDetails extends AbstractApiObject implements ArticleInterface
$apiObject['max_count'],
$apiObject['unit_quantity'],
$apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['decorators'],
new Decorators($apiObject['decorators']),
$apiObject['nutritional_values'],
$apiObject['ingredients_blob'] ?? '',
$apiObject['additional_info'],
@ -131,7 +131,7 @@ class SingleArticleDetails extends AbstractApiObject implements ArticleInterface
return $this->unitQuantitySub;
}
public function getDecorators(): array
public function getDecorators(): Decorators
{
return $this->decorators;
}

View File

@ -58,6 +58,11 @@ class PicnicClient
return ApiListTransformer::getObjects($data);
}
public function getDelivery(string $deliveryId): Delivery
{
return ApiListTransformer::getObject($this->client->get(sprintf('deliveries/%s', $deliveryId)));
}
/**
* @param string $term
*