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/farmfun.komma.pro/app/Komma/Kms/Menu/Item.php
<?php

declare(strict_types=1);

namespace App\Komma\Kms\Menu;

/**
 * Class Item
 *
 * Represents a menu item
 */
class Item
{
    /** @var string */
    protected $name = '';

    /** @var bool */
    protected $isSeparator = false;

    /** @var bool */
    protected $isActive = false;

    /** @var Item */
    protected $parent;

    /** @var Item[] */
    protected $subItems = [];

    /** @var string */
    protected $url = '#';

    protected $sortOrder = -1;

    /**
     * @var string[]
     */
    private $slugs = [];

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return Item
     */
    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return bool
     */
    public function isSeparator(): bool
    {
        return $this->isSeparator;
    }

    /**
     * @param bool $isSeparator
     * @return Item
     */
    public function setIsSeparator(bool $isSeparator): self
    {
        $this->isSeparator = $isSeparator;

        return $this;
    }

    /**
     * @return bool
     */
    public function isActive(): bool
    {
        return $this->isActive;
    }

    /**
     * @param bool $isActive
     * @return Item
     */
    public function setIsActive(bool $isActive): self
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * @return Item[]
     */
    public function getSubItems(): array
    {
        return $this->subItems;
    }

    /**
     * @param Item[] $subItems
     * @return Item
     */
    public function setSubItems(array $subItems): self
    {
        $this->subItems = [];
        foreach ($subItems as $subItem) {
            $this->addSubItem($subItem, $this);
        }

        return $this;
    }

    /**
     * @param Item $subItem
     * @return $this
     */
    public function addSubItem(self $subItem)
    {
        $subItem->parent = $this;
        $this->subItems[] = $subItem;

        return $this;
    }

    public function hasSubItems(): bool
    {
        return count($this->subItems) > 0;
    }

    /**
     * @return string
     */
    public function getUrl(): string
    {
        return $this->url;
    }

    /**
     * @param string $url
     * @return Item
     */
    public function setUrl(string $url): self
    {
        $this->url = $url;

        return $this;
    }

    /**
     * @param string $string
     * @return Item
     */
    public function setSlug(string $string)
    {
        $this->slugs = [$string];

        return $this;
    }

    /**
     * @param array $slugs
     * @return Item
     */
    public function setSlugs(array $slugs)
    {
        foreach ($slugs as $slug) {
            $this->addSlug($slug);
        }

        return $this;
    }

    /**
     * @param string $slug
     */
    public function addSlug(string $slug)
    {
        $this->slugs[] = $slug;
    }

    /**
     * @return string[]
     */
    public function getSlugs(): array
    {
        return $this->slugs;
    }

    /**
     * @return string
     */
    public function getSlug(): string
    {
        if (count($this->slugs) == 0) {
            return '';
        } else {
            return $this->slugs[0];
        }
    }

    /**
     * @return bool
     */
    public function hasSlugs(): bool
    {
        return count($this->slugs) != 0;
    }

    /**
     * @return bool
     */
    public function hasActiveSubItems(): bool
    {
        foreach ($this->subItems as $subItem) {
            if ($subItem->isActive()) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param Item $item
     */
    private function checkIfItemHasActiveSubItems(self $item)
    {
        if (! $this->hasSubItems()) {
            return;
        }

        foreach ($this->subItems as $subItem) {
            if ($subItem->isActive()) {
                return true;
            }
            if ($subItem->hasSubItems() && $this->checkIfItemHasActiveSubItems($subItem)) {
                return true;
            }
        }

        return false;
    }

    /**
     * @return {bool}
     */
    public function hasParent(): bool
    {
        return (bool) $this->parent;
    }

    /**
     * @return Item|null
     */
    public function getParent(): ?self
    {
        return $this->parent;
    }

    /**
     * @return int
     */
    public function getSortOrder(): int
    {
        return $this->sortOrder;
    }

    /**
     * @param int $sortOrder
     * @return Item
     */
    public function setSortOrder(int $sortOrder): self
    {
        $this->sortOrder = $sortOrder;

        return $this;
    }
}