src/Category/Cache/CacheCategoryCollection.php line 43

Open in your IDE?
  1. <?php
  2. namespace Klaravik\Category\Cache;
  3. use Klaravik\Category\Cache\Model\CacheCategory;
  4. use Klaravik\Common\Collections\ArrayCollection;
  5. use Klaravik\Exception\CategoryCacheException;
  6. final class CacheCategoryCollection extends ArrayCollection
  7. {
  8.     /** @var CacheCategory[] */
  9.     protected $elements;
  10.     /**
  11.      * @param int $level
  12.      *
  13.      * @return static
  14.      */
  15.     public function getByLevel($level)
  16.     {
  17.         return new static(array_filter($this->elements, function($cacheCategory) use($level) {
  18.             return $cacheCategory->getLevel() === $level;
  19.         }));
  20.     }
  21.     /**
  22.      * Get top level of categories
  23.      *
  24.      * @return $this
  25.      */
  26.     public function getTopLevel()
  27.     {
  28.         return $this->getByLevel(0);
  29.     }
  30.     /**
  31.      * @param int $categoryId
  32.      *
  33.      * @return CacheCategory
  34.      *
  35.      * @throws CategoryCacheException
  36.      */
  37.     public function findById($categoryId)
  38.     {
  39.         foreach ($this->elements as $cacheCategory) {
  40.             if ($cacheCategory->getId() === $categoryId) {
  41.                 return $cacheCategory;
  42.             }
  43.         }
  44.         throw new CategoryCacheException('Missing category for id '$categoryId);
  45.     }
  46.     /**
  47.      * Filter out collection on showOnlyInParent
  48.      *
  49.      * @param bool $value
  50.      *
  51.      * @return CacheCategoryCollection
  52.      */
  53.     public function showOnlyInParent($value)
  54.     {
  55.         return new static(array_filter($this->elements, function(CacheCategory $cacheCategory) use ($value) {
  56.             return $cacheCategory->isShowOnlyInParent() === $value;
  57.         }));
  58.     }
  59.     /**
  60.      * Filter out categories that has products in them
  61.      *
  62.      * @return $this
  63.      */
  64.     public function hasProductCount()
  65.     {
  66.         return new static(array_filter($this->elements, function(CacheCategory $cacheCategory) {
  67.             return $cacheCategory->getNumberOfProducts() > 0;
  68.         }));
  69.     }
  70.     /**
  71.      * Flatten the collection
  72.      *
  73.      * @return array|mixed
  74.      */
  75.     public function flatten()
  76.     {
  77.         $return = array();
  78.         foreach ($this->elements as $cacheCategory) {
  79.             $return[] = $cacheCategory;
  80.             if ($cacheCategory->getChildren()->count()) {
  81.                 $return array_merge($return$cacheCategory->getChildren()->flatten());
  82.             }
  83.         }
  84.         return $return;
  85.     }
  86.     /**
  87.      * @return CacheCategory[]
  88.      */
  89.     public function toArray()
  90.     {
  91.         return $this->elements;
  92.     }
  93.     /**
  94.      * Sort collection by order
  95.      *
  96.      * @return CacheCategoryCollection
  97.      *
  98.      * @throws \Exception
  99.      */
  100.     public function sortByOrder()
  101.     {
  102.         $iterator $this->getIterator();
  103.         $iterator->uasort(function($a$b) {
  104.             return ($a->getOrder() < $b->getOrder()) ? -1;
  105.         });
  106.         return new self(iterator_to_array($iterator));
  107.     }
  108.     /**
  109.      * @param null|int $selectedCategoryId
  110.      *
  111.      * @return int
  112.      */
  113.     public function totalNumberOfProducts($selectedCategoryId null)
  114.     {
  115.         try {
  116.             return $this->findById($selectedCategoryId)->getNumberOfProducts();
  117.         } catch (CategoryCacheException $e) {
  118.         }
  119.         return array_reduce($this->getTopLevel()->elements, function($countCacheCategory $cacheCategory) {
  120.             return $count $cacheCategory->getNumberOfProducts();
  121.         }, 0);
  122.     }
  123.     /**
  124.      * @param string $urlName
  125.      *
  126.      * @return CacheCategory|null
  127.      */
  128.     public function findOneByUrlName(string $urlName): ?CacheCategory
  129.     {
  130.         foreach ($this->elements as $category) {
  131.             if ($category->getUrl() === $urlName) {
  132.                 return $category;
  133.             }
  134.         }
  135.         return null;
  136.     }
  137. }