Advertisement
MyZik

Untitled

May 17th, 2020
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Manager;
  4.  
  5. use App\Entity\Category;
  6.  
  7. class ProductManager
  8. {
  9.     /**
  10.      * @var int
  11.      */
  12.     public $count;
  13.  
  14.     public function __construct()
  15.     {
  16.         $this->count = 0;
  17.     }
  18.  
  19.     /**
  20.      * @param Category|null $category
  21.      * @return int
  22.      */
  23.     public function getItemsCountFotCategory(?Category $category): int
  24.     {
  25.         foreach ($category->getChildren() as $child)
  26.         {
  27.             if ($category->getProducts() !== null) {
  28.                 $this->count += $category->getProducts()->count();
  29.             }
  30.  
  31.             if ($child->getChildren() !== null) {
  32.                 $this->getItemsCountFotCategory($child);
  33.             } else {
  34.                 continue;
  35.             }
  36.         }
  37.  
  38.         return $this->count;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement