Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 9 additions & 40 deletions src/Twig/Extension/CategoryFilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,8 @@

class CategoryFilterExtension extends AbstractExtension
{
/**
* @var CategoryLinkGenerator
*/
protected $categoryLinkGenerator;
protected CategoryLinkGenerator $categoryLinkGenerator;

/**
* CategoryFilterExtension constructor.
*
* @param CategoryLinkGenerator $categoryLinkGenerator
*/
public function __construct(CategoryLinkGenerator $categoryLinkGenerator)
{
$this->categoryLinkGenerator = $categoryLinkGenerator;
Expand All @@ -40,20 +32,15 @@ public function __construct(CategoryLinkGenerator $categoryLinkGenerator)
/**
* @return TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('app_category_filter_generate_link', [$this, 'generateLink']),
new TwigFunction('app_category_filter_prepare_data', [$this, 'prepareData']),
];
}

/**
* @param int $currentValue
* @param Category|null $rootCategory
* @return \stdClass
*/
public function prepareData($currentValue, Category $rootCategory = null)
public function prepareData(int $currentValue, ?Category $rootCategory = null): \stdClass
{
$data = new \stdClass();

Expand All @@ -68,48 +55,30 @@ public function prepareData($currentValue, Category $rootCategory = null)
return $data;
}

/**
* @param int $currentValue
*
* @return Category
*/
public function getCurrentCategory($currentValue)
public function getCurrentCategory(int $currentValue): Category
{
return Category::getById($currentValue ?? -1);
return Category::getById($currentValue);
}

/**
* @param Category|null $currentCategory
* @param Category|null $rootCategory
* @return array|\Pimcore\Model\DataObject[]
* @return Category[]
*/
public function getSubCategories(Category $currentCategory = null, $rootCategory = null)
public function getSubCategories(?Category $currentCategory = null, ?Category $rootCategory = null): array
{
$subCategories = [];

$parent = $currentCategory ?: $rootCategory;

if ($parent) {
$subCategories = array_filter($parent->getChildren(), function ($item) {
$subCategories = array_filter($parent->getChildren()->load(), static function ($item) {
return $item instanceof Category && $item->isPublished();
});

// foreach($currentCategory->getChildren() as $subCategory) {
// $subCategories[] = $subCategory;
// }
}

return $subCategories;
}

/**
* @param Category $category
* @param Category|null $rootCategory
* @param bool $reset
*
* @return string
*/
public function generateLink(Category $category, Category $rootCategory = null, $reset = false): string
public function generateLink(Category $category, ?Category $rootCategory = null, bool $reset = false): string
{
return $this->categoryLinkGenerator->generate($category, ['rootCategory' => $rootCategory], $reset);
}
Expand Down