Files
2025-08-02 16:30:27 +02:00

128 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\ComponentsBundle\Dto;
use KupShop\ComponentsBundle\Contracts\OrderItemInterface;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\KupShopBundle\Arrayable;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\KupShopBundle\Util\StringUtil;
class OrderItem implements OrderItemInterface, Arrayable, \ArrayAccess
{
private Price $totalPrice;
private Price $piecePrice;
public function __construct(
private array $item,
) {
}
public function getId(): int
{
return (int) $this->item['id'];
}
public function getProductId(): ?int
{
return $this->item['id_product'] ?? null;
}
public function getVariationId(): ?int
{
return $this->item['id_variation'] ?? null;
}
public function getPieces(): int
{
return (int) $this->item['pieces'];
}
public function getThumbnail(): ?Thumbnail
{
$product = $this->item['product'] ?? null;
if (!$product || empty($product->photoId)) {
return null;
}
return new Thumbnail(
(string) $product->photoId,
$product->photoDescr ?? '',
(string) (empty($product->photoDateUpdate) ? '' : strtotime($product->photoDateUpdate)),
);
}
public function getDescription(): ?string
{
return $this->item['descr'];
}
public function getProductTitle(): ?string
{
return $this->item['product']['title'] ?? null;
}
public function getUrl(): ?string
{
if (!$this->getProductId()) {
return null;
}
return path('products', [
'id' => $this->getProductId(),
'name' => StringUtil::slugify($this->getProductTitle() ?? $this->getDescription()),
]);
}
public function getTotalPrice(): Price
{
return $this->totalPrice ??= new Price(
$this->item['total_price']['value_without_vat'],
$this->item['total_price']['currency_object'],
$this->item['total_price']['vat'],
);
}
public function getPiecePrice(): Price
{
return $this->piecePrice ??= new Price(
$this->item['piece_price']['value_without_vat'],
$this->item['piece_price']['currency_object'],
$this->item['piece_price']['vat'],
);
}
public function getItemType(): string
{
return $this->item['item_type'];
}
public function toArray(): array
{
return $this->item;
}
public function offsetExists(mixed $offset): bool
{
return isset($this->item[$offset]);
}
public function offsetGet(mixed $offset): mixed
{
return $this->item[$offset];
}
public function offsetSet(mixed $offset, mixed $value): void
{
$this->item[$offset] = $value;
}
public function offsetUnset(mixed $offset): void
{
unset($this->item[$offset]);
}
}