custom/plugins/MoorlProductAccessories/src/Core/Content/Accessory/AccessoryCollection.php line 87

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlProductAccessories\Core\Content\Accessory;
  3. use MoorlProductAccessories\Core\Content\Category\CategoryCollection;
  4. use MoorlProductAccessories\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  6. use Shopware\Core\Framework\Uuid\Uuid;
  7. /**
  8.  * @method void            add(AccessoryEntity $entity)
  9.  * @method void            set(string $key, AccessoryEntity $entity)
  10.  * @method AccessoryEntity[]    getIterator()
  11.  * @method AccessoryEntity[]    getElements()
  12.  * @method AccessoryEntity|null get(string $key)
  13.  * @method AccessoryEntity|null first()
  14.  * @method AccessoryEntity|null last()
  15.  */
  16. class AccessoryCollection extends EntityCollection
  17. {
  18.     protected function getExpectedClass(): string
  19.     {
  20.         return AccessoryEntity::class;
  21.     }
  22.     public function filterByCategoryId(?string $id null): self
  23.     {
  24.         return $this->filter(function (AccessoryEntity $entity) use ($id) {
  25.             return ($entity->getCategoryId() === $id && $entity->getAccessory()->getActive() === true);
  26.         });
  27.     }
  28.     public function hasDuplicate(AccessoryEntity $struct): bool
  29.     {
  30.         foreach ($this->getElements() as $entity) {
  31.             if ($entity->getId() === $struct->getId()) {
  32.                 continue;
  33.             }
  34.             if ($entity->getAccessoryId() === $struct->getAccessoryId() && $entity->getCategoryId() === $struct->getCategoryId()) {
  35.                 return true;
  36.             }
  37.         }
  38.         return false;
  39.     }
  40.     public function removeDuplicates(): void
  41.     {
  42.         foreach ($this->getElements() as $entity) {
  43.             if ($entity->getSwCategoryId() && $this->hasDuplicate($entity)) {
  44.                 $this->remove($entity->getId());
  45.             }
  46.         }
  47.     }
  48.     public function sortByName(): void
  49.     {
  50.         $this->sort(function (AccessoryEntity $aAccessoryEntity $b) {
  51.             return strnatcasecmp($a->getAccessory()->getTranslated()['name'], $b->getAccessory()->getTranslated()['name']);
  52.         });
  53.     }
  54.     public function getByAccessoryId(string $id): ?AccessoryEntity
  55.     {
  56.         return $this->filter(function (AccessoryEntity $entity) use ($id) {
  57.             return ($entity->getAccessoryId() === $id);
  58.         })->first();
  59.     }
  60.     public function getIdByAccessoryId(string $id): ?string
  61.     {
  62.         $entity $this->getByAccessoryId($id);
  63.         if (!$entity) {
  64.             return null;
  65.         }
  66.         return $entity->getId();
  67.     }
  68.     public function getCategories(): CategoryCollection
  69.     {
  70.         $this->removeDuplicates();
  71.         $this->sortByName();
  72.         $categories = new CategoryCollection();
  73.         foreach ($this->getElements() as $entity) {
  74.             if ($entity->getCategoryId() && !$categories->has($entity->getCategoryId())) {
  75.                 $categories->add($entity->getCategory());
  76.             }
  77.         }
  78.         foreach ($categories as $category) {
  79.             $category->setAccessories($this->filterByCategoryId($category->getId()));
  80.         }
  81.         if ($this->filterByCategoryId()->count() > 0) {
  82.             $category = new CategoryEntity();
  83.             $category->setSortOrder(999); // is Last
  84.             $category->setAccessories($this->filterByCategoryId());
  85.             $category->setType('multi');
  86.             $category->setId(Uuid::randomHex());
  87.             $categories->add($category);
  88.         }
  89.         $categories->sortByOrder();
  90.         return $categories;
  91.     }
  92. }