<?php
namespace App\Manager;
use App\Entity\Category;
use App\Util\StringUtil;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class BlogManager
{
protected ParameterBagInterface $parameterBag;
protected EntityManagerInterface $entityManager;
public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $entityManager)
{
$this->parameterBag = $parameterBag;
$this->entityManager = $entityManager;
}
public function getCategories(): array
{
$categoryRepository = $this->entityManager->getRepository(Category::class);
return $this->parameterBag->get('site_blog_show_empty_categories')
? $categoryRepository->findAllCategoriesWithBlogPosts()
: $categoryRepository->findCategoriesWithBlogPosts();
}
public function getSelectedFilters(?string $filters): array
{
$selectedFilters = explode('/', $filters);
// If route is blogs/ the trailing slash adds an empty string as a filter to the array, array_filter to extract.
$selectedFilters = array_filter($selectedFilters);
return array_map(static fn ($filter) => StringUtil::xssCleanup($filter), $selectedFilters);
}
public function getSelectedCategoryTitle(array $categories, array $selectedFilters): ?string
{
$selectedCategoryTitle = null;
/** @var Category $category */
foreach ($categories as $category) {
if (!$selectedCategoryTitle && \in_array($category->getSlug(), $selectedFilters, true)) {
$selectedCategoryTitle = $category->getCategory();
}
}
return $selectedCategoryTitle;
}
}