163 lines
4.2 KiB
PHP
163 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\FeedsBundle\Wrapper;
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use KupShop\FeedsBundle\FeedProductCollection;
|
|
use KupShop\FeedsBundle\FeedProductList;
|
|
use KupShop\FeedsBundle\Query\FeedProductQuery;
|
|
use Query\Product;
|
|
use Query\QueryBuilder;
|
|
|
|
class ReviewWrapper extends BaseWrapper
|
|
{
|
|
protected $object;
|
|
private array $feedRow;
|
|
|
|
protected bool $isFetched = false;
|
|
private ArrayCollection $objects;
|
|
|
|
private FeedProductCollection $collection;
|
|
|
|
public function setObjects($objects): self
|
|
{
|
|
$this->isFetched = false;
|
|
$this->objects = $objects;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function __construct(
|
|
public readonly ProductWrapper $productWrapper,
|
|
public FeedProductList $feedProductList,
|
|
) {
|
|
}
|
|
|
|
protected function setFeedRow($feedRow)
|
|
{
|
|
$this->feedRow = $feedRow;
|
|
}
|
|
|
|
public function field_name(): string
|
|
{
|
|
return $this->object->name ?? '';
|
|
}
|
|
|
|
/**
|
|
* Identifikátor hodnocení.
|
|
*/
|
|
public function field_id(): int
|
|
{
|
|
return $this->object->id;
|
|
}
|
|
|
|
/**
|
|
* Datum vytvoření recenze.
|
|
*/
|
|
public function field_date()
|
|
{
|
|
return $this->object->date ?? '';
|
|
}
|
|
|
|
/**
|
|
* Klady.
|
|
*/
|
|
public function field_pros(): string
|
|
{
|
|
return $this->object->pros ?? '';
|
|
}
|
|
|
|
/**
|
|
* Zápory.
|
|
*/
|
|
public function field_cons(): string
|
|
{
|
|
return $this->object->cons ?? '';
|
|
}
|
|
|
|
/**
|
|
* Text hodnocení.
|
|
*/
|
|
public function field_summary(): string
|
|
{
|
|
return $this->object->summary ?? '';
|
|
}
|
|
|
|
/**
|
|
* Hodnocení.
|
|
*/
|
|
public function field_rating(): ?float
|
|
{
|
|
return $this->object->rating;
|
|
}
|
|
|
|
/**
|
|
* Produkt.
|
|
*/
|
|
public function field_product(): ?ProductWrapper
|
|
{
|
|
if (!$this->isFetched) {
|
|
$products = [];
|
|
|
|
foreach ($this->objects as $v) {
|
|
$products[$v->id_product][] = $v?->id_product_variation;
|
|
}
|
|
|
|
// review might not have relation to variation despite product having variations
|
|
// fallback to product when no variation-review relation exists
|
|
$products = array_map(function ($value) {
|
|
if (is_array($value) && empty(array_filter($value))) {
|
|
return null;
|
|
}
|
|
|
|
return $value;
|
|
}, $products);
|
|
|
|
$feedID = $this->feedRow['id'] ?? 0;
|
|
$productList = (clone $this->feedProductList)
|
|
->andSpec(Product::productsAndVariationsIds($products))
|
|
->andSpec(function (QueryBuilder $qb) use ($feedID) {
|
|
FeedProductQuery::withFeedProducts($feedID)($qb);
|
|
|
|
// override WHERE condition from FeedProductQuery::withFeedProducts() to ignore products.show_in_feed value
|
|
return "COALESCE(fp.active, 1) IN (1, 'Y')";
|
|
})
|
|
->fetchProducers()
|
|
->fetchImages(1)
|
|
->getProducts();
|
|
|
|
$this->productWrapper->setProductCollection($productList);
|
|
$this->collection = $productList;
|
|
$this->isFetched = true;
|
|
}
|
|
|
|
$id = $this->object->id_product;
|
|
if ($this->object->id_product_variation) {
|
|
$id .= '/'.$this->object->id_product_variation;
|
|
}
|
|
|
|
$product = $this->collection->get((string) $id);
|
|
|
|
// fallback to product when no variation-review relation exists
|
|
if (is_null($product) && !empty($this->object->id_product) && empty($this->object->id_product_variation)) {
|
|
$productKey = $this->object->id_product.'/';
|
|
foreach ($this->collection->getKeys() as $key) {
|
|
if (str_starts_with((string) $key, $productKey)) {
|
|
$product = $this->collection->get($key);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $product === null ? null : $this->productWrapper->setObject($product);
|
|
}
|
|
|
|
/** Jedná se o překlad */
|
|
public function field_isTranslation(): bool
|
|
{
|
|
return !empty($this->object->translation_id);
|
|
}
|
|
}
|