vendor/shopware/core/Content/Category/Tree/Tree.php line 8

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\Tree;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Framework\Struct\Struct;
  5. class Tree extends Struct
  6. {
  7.     /**
  8.      * @var TreeItem[]
  9.      */
  10.     protected $tree;
  11.     /**
  12.      * @var CategoryEntity|null
  13.      */
  14.     protected $active;
  15.     public function __construct(?CategoryEntity $active, array $tree)
  16.     {
  17.         $this->tree $tree;
  18.         $this->active $active;
  19.     }
  20.     public function isSelected(CategoryEntity $category): bool
  21.     {
  22.         if ($category->getId() === $this->active->getId()) {
  23.             return true;
  24.         }
  25.         $ids explode('|'$this->active->getPath());
  26.         return \in_array($category->getId(), $idstrue);
  27.     }
  28.     public function getTree(): array
  29.     {
  30.         return $this->tree;
  31.     }
  32.     public function setTree(array $tree): void
  33.     {
  34.         $this->tree $tree;
  35.     }
  36.     public function getActive(): ?CategoryEntity
  37.     {
  38.         return $this->active;
  39.     }
  40.     public function setActive(?CategoryEntity $active): void
  41.     {
  42.         $this->active $active;
  43.     }
  44.     public function getChildren(string $categoryId): ?Tree
  45.     {
  46.         $match $this->find($categoryId$this->tree);
  47.         if ($match) {
  48.             return new Tree($match->getCategory(), $match->getChildren());
  49.         }
  50.         // active id is not part of $this->tree? active id is root or used as first level
  51.         if ($this->active && $this->active->getId() === $categoryId) {
  52.             return $this;
  53.         }
  54.         return null;
  55.     }
  56.     public function getApiAlias(): string
  57.     {
  58.         return 'category_tree';
  59.     }
  60.     /**
  61.      * @param TreeItem[] $tree
  62.      */
  63.     private function find(string $categoryId, array $tree): ?TreeItem
  64.     {
  65.         if (isset($tree[$categoryId])) {
  66.             return $tree[$categoryId];
  67.         }
  68.         foreach ($tree as $item) {
  69.             $nested $this->find($categoryId$item->getChildren());
  70.             if ($nested) {
  71.                 return $nested;
  72.             }
  73.         }
  74.         return null;
  75.     }
  76. }