Files
2025-08-02 16:30:27 +02:00

74 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\GraphQLBundle\ApiAdmin\Util\Content;
use KupShop\ContentBundle\Template\TemplateList;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\TemplateCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\Parameters;
use KupShop\GraphQLBundle\ApiAdmin\Types\Template\Input\TemplateFilterInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Template\Template;
use KupShop\GraphQLBundle\ApiShared\ApiUtil;
use KupShop\GraphQLBundle\ApiShared\Types\Enums\SortEnum;
use KupShop\KupShopBundle\Util\Entity\EntityUtil;
use Query\Operator;
class TemplateUtil
{
public function __construct(
protected readonly TemplateList $templateList,
protected readonly EntityUtil $entityUtil,
) {
}
public function getTemplates(?int $offset, int $limit, ?Parameters $sort, ?TemplateFilterInput $filter): TemplateCollection
{
$templateList = (clone $this->templateList);
if ($filter?->id) {
$templateList->andSpec(Operator::inIntArray($filter->id, 't.id'));
}
if ($filter?->categoryId) {
$templateList->andSpec(Operator::inIntArray($filter->categoryId, 't.id_category'));
}
if ($filter?->visible !== null) {
$templateList->andSpec(Operator::equals(['figure' => $filter->visible ? 'Y' : 'N']));
}
if ($sort) {
/** @var SortEnum $value */
foreach (array_filter($sort->getData()) as $field => $value) {
$templateList->orderBy("t.{$field}", $value->value);
}
}
$templates = new TemplateCollection(
ApiUtil::wrapItems(
$templateList->getTemplates($totalCount)->toArray(),
Template::class
)
);
return $templates
->setLimit($limit)
->setOffset($offset)
->setItemsTotalCount($totalCount);
}
public function getTemplate(int $id): ?Template
{
$templates = (clone $this->templateList)
->andSpec(Operator::equals(['t.id' => $id]))
->getTemplates();
if ($templates = $templates->toArray()) {
return new Template(reset($templates));
}
return null;
}
}