Files
kupshop/bundles/KupShop/PreordersBundle/Dto/UserPreorderItem.php
2025-08-02 16:30:27 +02:00

122 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\PreordersBundle\Dto;
use KupShop\ComponentsBundle\Contracts\OrderItemInterface;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\KupShopBundle\Arrayable;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\KupShopBundle\Util\StringUtil;
use KupShop\OrderingBundle\Util\Order\OrderItemInfo;
class UserPreorderItem implements OrderItemInterface, Arrayable, \ArrayAccess
{
public function __construct(
private array $item,
) {
}
public function getId(): int
{
return (int) $this->item['id'];
}
public function getProductId(): ?int
{
return (int) $this->item['id_product'];
}
public function getVariationId(): ?int
{
return $this->item['id_variation'];
}
public function getPieces(): int
{
return (int) $this->item['pieces'];
}
public function getThumbnail(): ?Thumbnail
{
if (empty($this->item['id_photo'])) {
return null;
}
return new Thumbnail(
(string) $this->item['id_photo'],
$this->item['descr_photo'] ?? '',
(string) (empty($this->item['id_photo_update']) ? '' : strtotime($this->item['id_photo_update'])),
);
}
public function getDescription(): ?string
{
return $this->item['title'];
}
public function getProductTitle(): ?string
{
return $this->item['product_title'];
}
public function getUrl(): ?string
{
return path('products', [
'id' => $this->getProductId(),
'name' => StringUtil::slugify($this->getProductTitle()),
]);
}
public function getTotalPrice(): Price
{
return new Price(
toDecimal($this->item['price_total']),
Contexts::get(CurrencyContext::class)->getOrDefault($this->item['currency']),
toDecimal(0),
);
}
public function getPiecePrice(): Price
{
return new Price(
toDecimal($this->item['piece_price']),
Contexts::get(CurrencyContext::class)->getOrDefault($this->item['currency']),
toDecimal(0),
);
}
public function getItemType(): string
{
return OrderItemInfo::TYPE_PRODUCT;
}
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]);
}
}