HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Shop/Categories/CategoryService.php
<?php


namespace App\Komma\Shop\Categories;

use App\Komma\Shop\Categories\Models\Category;
use Illuminate\Database\Eloquent\Collection;

class CategoryService
{

    private $categories;

    public function __construct()
    {
        $this->categories = Category::where('active', 1)
            ->with('translation')
            ->orderBy('lft')
            ->get();
    }


    /**
     * Get a translated category
     *
     * @param  int  $id
     * @return Category
     */
    public function getCategory(int $id): Category
    {
        return $this->categories->find($id);
    }

    /**
     * Get all translated categories
     *
     * @return mixed
     */
    public function getCategories()
    {
        return $this->categories->where('lft', '!=', 1);
    }

    /**
     * Get the tree of the available categories
     *
     * @return Category
     */
    public function getCategoryTree():? Category
    {

        // First get the root category
        $rootCategories = $this->categories->where('lft', '=', 1)->first();
        if(empty($rootCategories)) return null;

        // Find the children of the tree category
        return $this->getChildrenOfCategory($rootCategories);
    }

    /**
     * Get all categories below the given one
     *
     * @param  Category  $category
     * @return mixed
     */
    public function getCategoriesBelow(Category $category)
    {
        return $this->categories->where('lft', '>', $category->lft)
            ->where('rgt', '<', $category->rgt);
    }

    /**
     * Get the translated children of the given category
     *
     * @param Category $category
     * @param int $depth
     * @return Category
     */
    private function getChildrenOfCategory(Category $category, int $depth = 1): Category
    {
        // Get the children (and children below)
        $collectedChildren = $this->categories->where('lft', '>', $category->lft)
            ->where('rgt', '<', $category->rgt);

        // Create collection for the translated children
        $directChildren = Collection::make();

        // Track the next child position so we only append the direct children
        $nextChildLftPosition = $category->lft +1;
        foreach ($collectedChildren as $child)
        {
            // Skip if not a direct child
            if($child->lft != $nextChildLftPosition) continue;

            // Here we swap non-translated child with the translated one from the translated Category collection
            $child = $this->categories->where('id', $child->id)->first();

            // If has sub children recall the getChildrenOfCategory
            if($child->rgt != $child->lft + 1){
                $this->getChildrenOfCategory($child,$depth + 1);
            }

            $child->depth = $depth + 1;

            // Append it the the translated children collection
            $directChildren->push($child);

            // Update the next child lft position
            $nextChildLftPosition = $child->rgt + 1;
        }

        // Bind the translated children on the category
        $category->children = $directChildren;
        $category->depth = $depth;

        return $category;
    }
}