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 getUnitQuantitySub(): string;
public function getPrice(): float; public function getPrice(): float;
public function getMaxCount(): int; 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

@ -12,14 +12,14 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface
* OrderArticle constructor. * OrderArticle constructor.
*/ */
public function __construct( public function __construct(
private string $id, private string $id,
private string $name, private string $name,
private array $imageIds, private array $imageIds,
private string $unitQuantity, private string $unitQuantity,
private string $unitQuantitySub, private string $unitQuantitySub,
private int $price, private int $price,
private int $maxCount, 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 static public function fromApi(array $apiObject): AbstractApiObject
{ {
dump($apiObject);
return new self( return new self(
$apiObject['id'], $apiObject['id'],
$apiObject['name'], $apiObject['name'],
@ -39,7 +40,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface
$apiObject['unit_quantity_sub'] ?? 'n/a', $apiObject['unit_quantity_sub'] ?? 'n/a',
$apiObject['price'], $apiObject['price'],
$apiObject['max_count'], $apiObject['max_count'],
$apiObject['decorators'], new Decorators($apiObject['decorators']),
); );
} }
@ -95,7 +96,7 @@ class OrderArticle extends AbstractApiObject implements ArticleInterface
return $this->maxCount; return $this->maxCount;
} }
public function getDecorators(): array public function getDecorators(): Decorators
{ {
return $this->decorators; return $this->decorators;
} }

View File

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

View File

@ -17,15 +17,15 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface
* SingleArticle constructor. * SingleArticle constructor.
*/ */
public function __construct( public function __construct(
private string $id, private string $id,
private string $name, private string $name,
private int $price, private int $price,
private int $displayPrice, private int $displayPrice,
private string $imageId, private string $imageId,
private int $maxCount, private int $maxCount,
private string $unitQuantity, private string $unitQuantity,
private string $unitQuantitySub, private string $unitQuantitySub,
private array $decorators, private Decorators $decorators,
) )
{ {
} }
@ -41,7 +41,7 @@ class SingleArticle extends AbstractApiObject implements ArticleInterface
$apiObject['max_count'], $apiObject['max_count'],
$apiObject['unit_quantity'], $apiObject['unit_quantity'],
$apiObject['unit_quantity_sub'] ?? 'n/a', $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; return $this->unitQuantitySub;
} }
public function getDecorators(): array public function getDecorators(): Decorators
{ {
return $this->decorators; return $this->decorators;
} }

View File

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

View File

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