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

130 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\GraphQLBundle\ApiAdmin\Util\Content;
use KupShop\ContentBundle\Page\FragmentPage;
use KupShop\ContentBundle\Page\PageList;
use KupShop\ContentBundle\Page\SystemPageList;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\PageCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\SystemPageCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\Page\Input\PageFilterInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Page\Page;
use KupShop\GraphQLBundle\ApiAdmin\Types\Parameters;
use KupShop\GraphQLBundle\ApiAdmin\Types\SystemPage\Input\SystemPageFilterInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\SystemPage\SystemPage;
use KupShop\GraphQLBundle\ApiShared\ApiUtil;
use KupShop\GraphQLBundle\ApiShared\Types\Enums\SortEnum;
use KupShop\KupShopBundle\Util\Entity\EntityUtil;
use Query\Operator;
class PageUtil
{
public function __construct(
protected readonly PageList $pageList,
protected readonly SystemPageList $systemPageList,
protected readonly EntityUtil $entityUtil,
) {
}
public function getPages(?int $offset, int $limit, ?Parameters $sort, ?PageFilterInput $filter): PageCollection
{
$pageList = (clone $this->pageList)->limit(ApiUtil::getLimit($limit), $offset);
if ($filter?->id) {
$pageList->andSpec(Operator::inIntArray($filter->id, 'p.id'));
}
if ($filter?->code) {
$pageList->andSpec(Operator::inStringArray($filter->code, 'p.code'));
}
if ($filter?->parent !== null) {
$pageList->andSpec(Operator::inIntArray($filter->parent, 'p.parent'));
}
if ($sort) {
/** @var SortEnum $value */
foreach (array_filter($sort->getData()) as $field => $value) {
$pageList->orderBy("p.{$field}", $value->value);
}
}
return (new PageCollection(ApiUtil::wrapItems(
$pageList->getPages($totalCount)->toArray(),
Page::class
)))->setItemsTotalCount($totalCount)->setOffset($offset)->setLimit($limit);
}
public function getSystemPages(?int $offset, int $limit, ?Parameters $sort, ?SystemPageFilterInput $filter): SystemPageCollection
{
$systemPageList = (clone $this->systemPageList)
->limit(ApiUtil::getLimit($limit), $offset);
if ($filter?->id) {
$systemPageList->andSpec(Operator::inIntArray($filter->id, 'p.id'));
}
if ($filter?->code) {
$systemPageList->andSpec(Operator::inStringArray($filter->code, 'p.code'));
}
if ($filter?->type !== null) {
$systemPageList->andSpec(Operator::inIntArray($filter->type, 'p.type'));
}
if ($sort) {
/** @var SortEnum $value */
foreach (array_filter($sort->getData()) as $field => $value) {
$systemPageList->orderBy("p.{$field}", $value->value);
}
}
return (new SystemPageCollection(ApiUtil::wrapItems(
$systemPageList->getPages($totalCount)->toArray(),
Page::class
)))->setItemsTotalCount($totalCount)->setOffset($offset)->setLimit($limit);
}
public function getFragments(): PageCollection
{
$pageList = (clone $this->systemPageList);
$pageList->andSpec(Operator::equals(['p.type' => FragmentPage::getType()]));
return new PageCollection(
ApiUtil::wrapItems(
$pageList->getPages($totalCount)->toArray(),
Page::class
)
);
}
public function getPage(int $id): ?Page
{
$pageList = (clone $this->pageList)
->andSpec(Operator::equals(['p.id' => $id]));
$pages = $pageList->getPages($totalCount)->toArray();
if (count($pages)) {
return new Page(reset($pages));
}
return null;
}
public function getSystemPage(int $id): ?SystemPage
{
$pageList = (clone $this->systemPageList)
->andSpec(Operator::equals(['p.id' => $id]));
$pages = $pageList->getPages($totalCount)->toArray();
if (count($pages)) {
return new SystemPage(reset($pages));
}
return null;
}
}