<?php
namespace Boldr\Shop\ShopBundle\Presenter\Category;
use Boldr\Shop\ShopBundle\Entity\Category;
class CategoryPresenter
{
private iterable $categoryViewProcessors;
private array $categoryViews = [];
public function __construct(iterable $categoryViewProcessors)
{
$this->categoryViewProcessors = $categoryViewProcessors;
}
public function createCategoryView(Category $category, string $locale): CategoryView
{
$cacheKey = $category->getId().'_'.$locale;
if (isset($this->categoryViews[$cacheKey]))
return $this->categoryViews[$cacheKey];
$categoryTranslation = $category->getTranslations()->get($locale);
$categoryView = new CategoryView(
$category->getId(),
$categoryTranslation->getName()
);
$view = array_reduce(
iterator_to_array($this->categoryViewProcessors),
static fn($categoryView, $categoryViewProcessor) => $categoryViewProcessor->processCategoryView($categoryView, $category, $locale),
$categoryView
);
$this->categoryViews[$cacheKey] = $view;
return $view;
}
}