Recusively converts a child node and its children to XML for output

@param navigation_node $child The child to convert
@param int $depth Pointlessly used to track the depth of the XML structure
@return string JSON
498264
protected function convert_child($child, $depth=1) {
        if (!$child->display) {
            return '';
        }
        $attributes = array();
        $attributes['id'] = $child->id;
        $attributes['name'] = (string)$child->text; // This can be lang_string object so typecast it.
        $attributes['type'] = $child->type;
        $attributes['key'] = $child->key;
        $attributes['class'] = $child->get_css_type();
        $attributes['requiresajaxloading'] = $child->requiresajaxloading;

        if ($child->icon instanceof pix_icon) {
            $attributes['icon'] = array(
                'component' => $child->icon->component,
                'pix' => $child->icon->pix,
            );
            foreach ($child->icon->attributes as $key=>$value) {
                if ($key == 'class') {
                    $attributes['icon']['classes'] = explode(' ', $value);
                } else if (!array_key_exists($key, $attributes['icon'])) {
                    $attributes['icon'][$key] = $value;
                }

            }
        } else if (!empty($child->icon)) {
            $attributes['icon'] = (string)$child->icon;
        }

        if ($child->forcetitle || $child->title !== $child->text) {
            $attributes['title'] = htmlentities($child->title, ENT_QUOTES, 'UTF-8');
        }
        if (array_key_exists($child->key.':'.$child->type, $this->expandable)) {
            $attributes['expandable'] = $child->key;
            $child->add_class($this->expandable[$child->key.':'.$child->type]['id']);
        }

        if (count($child->classes)>0) {
            $attributes['class'] .= ' '.join(' ',$child->classes);
        }
        if (is_string($child->action)) {
            $attributes['link'] = $child->action;
        } else if ($child->action instanceof moodle_url) {
            $attributes['link'] = $child->action->out();
        } else if ($child->action instanceof action_link) {
            $attributes['link'] = $child->action->url->out();
        }
        $attributes['hidden'] = ($child->hidden);
        $attributes['haschildren'] = ($child->children->count()>0 || $child->type == navigation_node::TYPE_CATEGORY);
        $attributes['haschildren'] = $attributes['haschildren'] || $child->type == navigation_node::TYPE_MY_CATEGORY;

        if ($child->children->count() > 0) {
            $attributes['children'] = array();
            foreach ($child->children as $subchild) {
                $attributes['children'][] = $this->convert_child($subchild, $depth+1);
            }
        }

        if ($depth > 1) {
            return $attributes;
        } else {
            return json_encode($attributes);
        }
    }
421987
protected function takeChildFromDOM($child)
    {
        $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
        switch ($absoluteNodeName) {
            case $this->lookupNamespace('gphoto') . ':' . 'user';
                $user = new Zend_Gdata_Photos_Extension_User();
                $user->transferFromDOM($child);
                $this->_gphotoUser = $user;
                break;
            case $this->lookupNamespace('gphoto') . ':' . 'nickname';
                $nickname = new Zend_Gdata_Photos_Extension_Nickname();
                $nickname->transferFromDOM($child);
                $this->_gphotoNickname = $nickname;
                break;
            case $this->lookupNamespace('gphoto') . ':' . 'thumbnail';
                $thumbnail = new Zend_Gdata_Photos_Extension_Thumbnail();
                $thumbnail->transferFromDOM($child);
                $this->_gphotoThumbnail = $thumbnail;
                break;
            case $this->lookupNamespace('atom') . ':' . 'entry':
                $entryClassName = $this->_entryClassName;
                $tmpEntry = new Zend_Gdata_App_Entry($child);
                $categories = $tmpEntry->getCategory();
                foreach ($categories as $category) {
                    if ($category->scheme == Zend_Gdata_Photos::KIND_PATH &&
                        $this->_entryKindClassMapping[$category->term] != "") {
                            $entryClassName = $this->_entryKindClassMapping[$category->term];
                            break;
                    } else {
                        require_once 'Zend/Gdata/App/Exception.php';
                        throw new Zend_Gdata_App_Exception('Entry is missing kind declaration.');
                    }
                }

                $newEntry = new $entryClassName($child);
                $newEntry->setHttpClient($this->getHttpClient());
                $this->_entry[] = $newEntry;
                break;
            default:
                parent::takeChildFromDOM($child);
                break;
        }
    }
480284
protected function getHierarchicalChoicesAttributes($nodes, $depth = 0)
    {
        $choices = array();
        $lastNode = end($nodes);
        foreach ($nodes as $node) {
            $choices[$node['node']['nodeId']] = array(
                'data-depth' => $depth,
                'data-last' => $node === $lastNode,
            );
            if (array_key_exists('child', $node)) {
                $choices = array_merge($choices, $this->getHierarchicalChoicesAttributes($node['child'], $depth + 1));
            }
        }

        return $choices;

    }
140632
protected function takeChildFromDOM($child)
    {
        $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
        switch ($absoluteNodeName) {
        case $this->lookupNamespace('yt') . ':' . 'description':
            $description = new Zend_Gdata_YouTube_Extension_Description();
            $description->transferFromDOM($child);
            $this->_description = $description;
            break;
        case $this->lookupNamespace('yt') . ':' . 'countHint':
            $countHint = new Zend_Gdata_YouTube_Extension_CountHint();
            $countHint->transferFromDOM($child);
            $this->_countHint = $countHint;
            break;
        case $this->lookupNamespace('yt') . ':' . 'playlistId':
            $playlistId = new Zend_Gdata_YouTube_Extension_PlaylistId();
            $playlistId->transferFromDOM($child);
            $this->_playlistId = $playlistId;
            break;
        case $this->lookupNamespace('gd') . ':' . 'feedLink':
            $feedLink = new Zend_Gdata_Extension_FeedLink();
            $feedLink->transferFromDOM($child);
            $this->_feedLink[] = $feedLink;
            break;
        default:
            parent::takeChildFromDOM($child);
            break;
        }
    }
538421
protected function takeChildFromDOM($child)
    {
        $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
        switch ($absoluteNodeName) {
            case $this->lookupNamespace('gd') . ':' . 'comments':
                $comments = new Zend_Gdata_Extension_Comments();
                $comments->transferFromDOM($child);
                $this->_comments = $comments;
                break;
            case $this->lookupNamespace('gd') . ':' . 'rating':
                $rating = new Zend_Gdata_Extension_Rating();
                $rating->transferFromDOM($child);
                $this->_rating = $rating;
                break;
            case $this->lookupNamespace('yt') . ':' . 'description':
                $description = new Zend_Gdata_YouTube_Extension_Description();
                $description->transferFromDOM($child);
                $this->_description = $description;
                break;
            case $this->lookupNamespace('yt') . ':' . 'statistics':
                $statistics = new Zend_Gdata_YouTube_Extension_Statistics();
                $statistics->transferFromDOM($child);
                $this->_statistics = $statistics;
                break;
            default:
                parent::takeChildFromDOM($child);
                break;
        }
    }
485357
public function getDataAsJsonString($option = 0, $depth = 512): string
    {
        $data = $this->get();

        if (\is_object($data) || \is_array($data)) {
            $data = \json_encode($data, $option, $depth);
        } else {
            $data = \json_encode([$data], $option, $depth);
        }

        return \json_encode($data, $option, $depth);
    }
390246
function child($depth=null){
        if($depth==null)
            return self::where('parent',$this->attributes['id'])->get();
        else
            return self::where('parent',$this->attributes['id'])->where('depth',$depth)->get();
    }
1000231
public function categoryLi ($categories , $child = false)
    {
        $list = "";
        if ($categories) {
            if ($child && count($categories) > 0) {
                $list .= "<ul>";
                foreach ($categories as $category) {
                    $list .= "<li > <a href='" . $category->url() . "'>{$category->title}</a></li>";
                    $list .= $this->categoryLi($category->child() , true);
                }
                $list .= "</ul>";

            } else {
                foreach ($categories as $category) {
                    $list .= "<li > <a href='" . $category->url() . "'>{$category->title}</a></li>";
                    $list .= $this->categoryLi($category->child() , true);
                }
            }
        }
        return $list;
    }
1200358
private function setChildren(\SimpleXMLElement $xml, $depth, $key, array $arrayXML) {
        $return = $arrayXML;
        if ($xml->children()->count() > 0) {
            foreach ($xml->children() as $childValue) {
                $child = $this->xml2array($childValue, $depth);
                if ($depth > 1 && ! empty($child)) {
                    $return = array_merge_recursive($return, [$key => $child]);
                } elseif (! empty($child)) {
                    $return = array_merge_recursive($return, $child);
                }
            }
        }
        return $return;
    }
306961
private function retrieveSpecificAttributeCrossRef(\SimpleXMLElement $xml, $depth, array $arrayXML, $key) {
        // Specific Attribute: Retrieve the given crossref
        if ($xml->attributes()->crossref) {
            $crossref = $this->file->xpath(trim($xml->attributes()->crossref));
            $temp     = [];
            foreach ($crossref as $value) {
                $resultArray = $this->xml2array($value, $depth);

                if (! empty($resultArray)) {
                    $temp = array_merge($temp, $resultArray);
                }
            }
            return array_merge($arrayXML, [$key => $temp]);
        }
        return $arrayXML;
    }
693557
protected function takeChildFromDOM($child)
    {
        $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;

        switch ($absoluteNodeName) {
            case $this->lookupNamespace('apps') . ':' . 'emailList';
                $emailList = new Zend_Gdata_Gapps_Extension_EmailList();
                $emailList->transferFromDOM($child);
                $this->_emailList = $emailList;
                break;
            case $this->lookupNamespace('gd') . ':' . 'feedLink';
                $feedLink = new Zend_Gdata_Extension_FeedLink();
                $feedLink->transferFromDOM($child);
                $this->_feedLink[] = $feedLink;
                break;
            default:
                parent::takeChildFromDOM($child);
                break;
        }
    }
890496
private function getCategoryTree(string $navigationId, SalesChannelContext $context)
    {
        /** @var Tree $category */
        $category = $this->navigationLoader->loadLevel($navigationId, $context);

        /** @var CategoryEntity $active */
        $active = $category->getActive();

        if ($active->getChildCount() > 0 || $active->getParentId() === null) {
            return $category;
        }

        return $this->navigationLoader->loadLevel($active->getParentId(), $context);
    }
1040054
protected function takeChildFromDOM($child)
    {
        $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
        switch ($absoluteNodeName) {
        case $this->lookupNamespace('gd') . ':' . 'entryLink':
            $entryLink = new Zend_Gdata_Extension_EntryLink();
            $entryLink->transferFromDOM($child);
            $this->_entryLink = $entryLink;
            break;
        default:
            parent::takeChildFromDOM($child);
            break;
        }
    }
941358
public function menuNodeHierarchy($depth = 10)
    {
        if ($depth == 0) {
            return $this;
        }

        return $this
            ->prototype('array')
                ->children()
                    ->scalarNode('route')->end()
                    ->arrayNode('routeParameters')
                        ->prototype('variable')
                        ->end()
                    ->end()
                    ->scalarNode('uri')->end()
                    ->scalarNode('label')->end()
                    ->booleanNode('display')->defaultTrue()->end()
                    ->booleanNode('displayChildren')->defaultTrue()->end()
                    ->integerNode('order')->end()
                    ->arrayNode('attributes')
                        ->prototype('variable')
                        ->end()
                    ->end()
                    ->arrayNode('linkAttributes')
                        ->prototype('variable')
                        ->end()
                    ->end()
                    ->arrayNode('childrenAttributes')
                        ->prototype('variable')
                        ->end()
                    ->end()
                    ->arrayNode('labelAttributes')
                        ->prototype('variable')
                        ->end()
                    ->end()
                    ->arrayNode('roles')
                        ->prototype('scalar')
                        ->end()
                    ->end()
                    ->arrayNode('extras')
                        ->prototype('scalar')
                        ->end()
                    ->end()
                    ->menuNode('children')->menuNodeHierarchy($depth - 1)
                ->end()
            ->end();
    }
1032757
public function addChild(Node $child)
    {
        $this->children[]            = $child;
        $child->parent               = $this;
        $child->properties['parent'] = $this->getId();
    }
1072771
protected function getIdsFrom($parent, $depth)
    {
        if ($depth >= $this->Depth) {
            return;
        }
        $ids = array();
        foreach ($parent->Children() as $kid) {
            $ids[] = $kid->ID;
            $childIds = $this->getIdsFrom($kid, $depth + 1);
            if ($childIds) {
                $ids = array_merge($ids, $childIds);
            }
        }
        return $ids;
    }
1194451
private function convertNodeToXML($data, $nodeName, $depth)
    {
        if (is_scalar($data))
        {
            //Treat it as a scalar, just add the node to the output
            $this->output .= str_repeat(static::TAB_CHARACTER, $depth);
            $this->output .= '<' . $nodeName . '>' . $data . '</' . $nodeName . '>' . PHP_EOL;

            return;
        }

        //Handle objects as arrays and arrays as arrays
        if (is_object($data))
        {
            //Sorry for the typecast
            $data = (array)$data;

            //We can assume an object is an associative array now
            $isAssociative = true;
        }
        else
        {
            //This is considered the "Fastest" way to figure out if an array is associative or not
            $isAssociative = ($data !== array_values($data));
        }


        if ($isAssociative)
        {
            //Associative array, treat each node as an element of the parent, indent each one
            $this->output .= str_repeat(static::TAB_CHARACTER, $depth);
            $this->output .= '<' . $nodeName . '>' . PHP_EOL;

            foreach ($data as $key => $value)
            {
                $this->convertNodeToXML($value, $key, $depth + 1);
            }

            $this->output .= str_repeat(static::TAB_CHARACTER, $depth);
            $this->output .= '</' . $nodeName . '>' . PHP_EOL;
        }
        else
        {
            //Integer indexed array (we hope), treat each node as a clone of the parent with no extra indent
            foreach ($data as $key => $value)
            {
                $this->convertNodeToXML($value, $nodeName, $depth);
            }
        }
    }