89 lines
3.0 KiB
PHP
89 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\PreordersBundle\Dto;
|
|
|
|
use KupShop\ComponentsBundle\Entity\Thumbnail;
|
|
use KupShop\KupShopBundle\Util\Price\Price;
|
|
use KupShop\KupShopBundle\Util\StringUtil;
|
|
use KupShop\KupShopBundle\Wrapper\PriceWrapper;
|
|
|
|
/** @deprecated */
|
|
readonly class PreorderItem
|
|
{
|
|
public function __construct(
|
|
public int $productId,
|
|
public ?int $variationId,
|
|
|
|
public string $title,
|
|
public string $code,
|
|
public string $url,
|
|
public Thumbnail $thumbnail,
|
|
public Price $productPrice,
|
|
public Price $mocPrice,
|
|
public array $dynamicPrices,
|
|
) {
|
|
}
|
|
|
|
public static function fromProduct(\Product $product): self
|
|
{
|
|
assert(isset($product['dynamic_prices'], $product['moc_price']), 'Produkt z předobjednávky by měl mít předobjednávkové ceny :)');
|
|
|
|
return new self(
|
|
productId: $product->id,
|
|
variationId: null,
|
|
title: $product->title,
|
|
code: $product->code,
|
|
url: path('products', ['id' => $product->id, 'name' => StringUtil::slugify($product->title)]),
|
|
thumbnail: new Thumbnail(
|
|
id: (string) $product->image['id'],
|
|
title: $product->image['descr'],
|
|
version: (string) $product->image['date_update'],
|
|
),
|
|
productPrice: $product->getProductPrice(),
|
|
mocPrice: $product['moc_price'],
|
|
dynamicPrices: $product['dynamic_prices'],
|
|
);
|
|
}
|
|
|
|
public static function fromVariation(\Product $product, array $variation): self
|
|
{
|
|
assert(isset($variation['dynamic_prices'], $variation['moc_price']), 'Produkt z předobjednávky by měl mít předobjednávkové ceny :)');
|
|
|
|
$thumbnail = null;
|
|
if (!empty($variation['photos'])) {
|
|
$image = reset($variation['photos']);
|
|
|
|
$thumbnail = new Thumbnail(
|
|
id: (string) $image['id'],
|
|
title: $image['descr'],
|
|
version: (string) $image['date_update'],
|
|
);
|
|
} else {
|
|
$thumbnail = new Thumbnail(
|
|
id: (string) $product->image['id'],
|
|
title: $product->image['descr'],
|
|
version: (string) $product->image['date_update'],
|
|
);
|
|
}
|
|
|
|
return new self(
|
|
productId: $product->id,
|
|
variationId: $variation['id'],
|
|
title: $variation['title'],
|
|
code: $variation['code'] ?? $product->code,
|
|
url: path('products', ['id' => $product->id, 'name' => StringUtil::slugify($product->title)]).'#'.$variation['id'],
|
|
thumbnail: $thumbnail,
|
|
productPrice: PriceWrapper::unwrap($variation['productPrice']),
|
|
mocPrice: $variation['moc_price'],
|
|
dynamicPrices: $variation['dynamic_prices'],
|
|
);
|
|
}
|
|
|
|
public function getIndex(): string
|
|
{
|
|
return "{$this->productId}".($this->variationId ? "_{$this->variationId}" : '');
|
|
}
|
|
}
|