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/topswtwmobile.komma.pro/app/KommaApp/Shop/Tree/TreeService.php
<?php


namespace KommaApp\Shop\Tree;


abstract class TreeService
{
    /**
     * @param $id
     * @return bool
     */
    public function getNodeById($id)
    {
        $tree = $this->getTreeNodes();
        if(isset($tree['index'][$id])) return $tree['index'][$id];
        return false;
    }

    /**
     * @return array
     */
    public function getFirstLetters($sort = false)
    {
        $tree = $this->getTreeNodes(false);

        $letters = [];
        foreach($tree as $branch)
        {
            $firstLetter = substr($branch->name,0,1);
            if( ! in_array($firstLetter,$letters)) $letters[] = $firstLetter;
        }
        if($sort){
            sort($letters);
        }
        return $letters;
    }

    /**
     * @param $node
     * @return bool
     */
    public function getRootNode($node)
    {
        while($parent = $this->checkParent($node))
        {
            $node = $parent;
        }

        return $node;
    }

    /**
     * @param $node
     * @return bool
     */
    protected function checkParent($node)
    {
        if($node && $parent = $node->getParent())
        {
            return $parent;
        }
        return false;
    }
}