Add a branch to an XML object recursively

@param  Zend_Config      $config
@param  SimpleXMLElement $xml
@param  SimpleXMLElement $parent
@return void
172125
protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
    {
        $branchType = null;

        foreach ($config as $key => $value) {
            if ($branchType === null) {
                if (is_numeric($key)) {
                    $branchType = 'numeric';
                    $branchName = $xml->getName();
                    $xml        = $parent;

                    unset($parent->{$branchName});
                } else {
                    $branchType = 'string';
                }
            } else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
                // require_once 'Zend/Config/Exception.php';
                throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed');
            }

            if ($branchType === 'numeric') {
                if ($value instanceof Zend_Config) {
                    $child = $parent->addChild($branchName);

                    $this->_addBranch($value, $child, $parent);
                } else {
                    $parent->addChild($branchName, (string) $value);
                }
            } else {
                if ($value instanceof Zend_Config) {
                    $child = $xml->addChild($key);

                    $this->_addBranch($value, $child, $xml);
                } else {
                    $xml->addChild($key, (string) $value);
                }
            }
        }
    }
103939
public static function addList(DOMElement $parent, array $extensions) : void
    {
        if (empty($extensions)) {
            return;
        }

        $extElement = $parent->ownerDocument->createElementNS(Constants::NS_SAMLP, 'samlp:Extensions');
        $parent->appendChild($extElement);

        foreach ($extensions as $ext) {
            $ext->toXML($extElement);
        }
    }
128377
private function loadDocumentTree($config, ContainerBuilder $container)
    {
        $configuration = [
            'routing_defaults' => $config['routing_defaults'],
            'repository_name' => $config['repository_name'],
            'sortable_by' => $config['sortable_by'],
            'move' => true,
            'reorder' => true,
        ];

        $container->setParameter('sonata_admin_doctrine_phpcr.tree_block.configuration', $configuration);

        foreach ($configuration as $key => $value) {
            $container->setParameter('sonata_admin_doctrine_phpcr.tree_block.'.$key, $value);
        }
    }
677098
public static function add($parent = false)
    {
        $db = Database::connection();
        $treeNodeParentID = 0;
        $treeID = 0;
        $treeNodeDisplayOrder = 0;
        $inheritPermissionsFromTreeNodeID = 0;
        if (is_object($parent)) {
            $treeNodeParentID = $parent->getTreeNodeID();
            $treeID = $parent->getTreeID();
            $inheritPermissionsFromTreeNodeID = $parent->getTreeNodePermissionsNodeID();
            $treeNodeDisplayOrder = (int) $db->fetchColumn('select count(treeNodeDisplayOrder) from TreeNodes where treeNodeParentID = ?', [$treeNodeParentID]);
            $parent->updateDateModified();
        } else {
            $parent = null;
        }

        $treeNodeTypeHandle = uncamelcase(strrchr(get_called_class(), '\\'));

        $dateModified = Core::make('date')->toDB();
        $dateCreated = Core::make('date')->toDB();

        $type = TreeNodeType::getByHandle($treeNodeTypeHandle);
        $db->executeQuery(
            'insert into TreeNodes (treeNodeTypeID, treeNodeParentID, treeNodeDisplayOrder, inheritPermissionsFromTreeNodeID, dateModified, dateCreated, treeID) values (?, ?, ?, ?, ?, ?, ?)',
            [
                $type->getTreeNodeTypeID(),
                $treeNodeParentID,
                $treeNodeDisplayOrder,
                $inheritPermissionsFromTreeNodeID,
                $dateModified,
                $dateCreated,
                $treeID,
            ]
        );
        $id = $db->lastInsertId();
        $node = self::getByID($id);

        if (!$inheritPermissionsFromTreeNodeID) {
            $node->setTreeNodePermissionsToOverride();
        }

        if ($parent !== null && $parent->childNodesLoaded) {
            $parent->childNodes[] = $node;
        }

        return $node;
    }
627384
public function setParent($parent)
	{
		if ($parent instanceof JCategoryNode || is_null($parent))
		{
			if (!is_null($this->_parent))
			{
				$key = array_search($this, $this->_parent->_children);
				unset($this->_parent->_children[$key]);
			}

			if (!is_null($parent))
			{
				$parent->_children[] = & $this;
			}

			$this->_parent = $parent;

			if ($this->id != 'root')
			{
				if ($this->parent_id != 1)
				{
					$this->_path = $parent->getPath();
				}

				$this->_path[] = $this->id . ':' . $this->alias;
			}

			if (count($parent->_children) > 1)
			{
				end($parent->_children);
				$this->_leftSibling = prev($parent->_children);
				$this->_leftSibling->_rightsibling = & $this;
			}
		}
	}
514114
protected function treeView(array $config, int $level = 0)
    {
        foreach ($config as $name => $value) {
            if (is_array($value)) {
                $this->writeln(
                    sprintf(
                        '%s<info>- %s</>:',
                        str_repeat(' ', $level * 2),
                        $name
                    )
                );

                if (count($value) > 0) {
                    $this->treeView($value, ++$level);
                    --$level;
                }
            } else {
                if (is_int($name)) {
                    $this->writeln(
                        sprintf(
                            '%s<info>- %s</>',
                            str_repeat(' ', $level * 2),
                            $value
                        )
                    );
                } else {
                    $this->writeln(
                        sprintf(
                            '%s<info>- %s</>: %s',
                            str_repeat(' ', $level * 2),
                            $name,
                            $value
                        )
                    );
                }
            }
        }
    }
313096
public function creatOrUpdateByParent( $parent, $metas, $config = [] ) {

		$modelClass = static::$modelClass;

		// Disable all existing metas for given type
		if( isset( $config[ 'type' ] ) && isset( $config[ 'disable' ] ) ) {

			$this->disableByType( $parent, $config[ 'type' ] );
		}

		// Create/Update given metas
		if( !empty( $metas ) && count( $metas ) > 0 ) {

			foreach( $metas as $meta ) {

				$model = new $modelClass();

				$model->name	= $meta[ 'name' ];
				$model->label	= empty( $meta[ 'label' ] ) ? $meta[ 'name' ] : $meta[ 'label' ];
				$model->value	= $meta[ 'value' ];
				$model->type	= $meta[ 'type' ];
				$model->active	= true;
				$model->icon	= !empty( $meta[ 'icon' ] ) ? $meta[ 'icon' ] : null;

				$model->modelId	= $parent->id;

				$model->valueType = empty( $meta[ 'valueType' ] ) ? $modelClass::VALUE_TYPE_TEXT : $meta[ 'valueType' ];

				if( isset( $meta[ 'id' ] ) ) {

					$model->id		= $meta[ 'id' ];
					$model->icon	= !empty( $meta[ 'icon' ] ) ? $meta[ 'icon' ] : $model->icon;

					parent::update( $model, [ 'attributes' => [ 'icon', 'name', 'value', 'active', 'type', 'valueType' ] ] );
				}
				else {

					parent::create( $model );
				}
			}
		}
	}
254028
protected final void setParentNode(final DiffNode parentNode)
	{
		if (this.parentNode != null && this.parentNode != parentNode)
		{
			throw new IllegalStateException("The parent of a node cannot be changed, once it's set.");
		}
		this.parentNode = parentNode;
	}
799132
public function addChild(Node $child)
    {
        $this->children[]                           = $child;
        $child->parent                              = $this;
        $child->properties[$this->config['parent']] = $this->properties[$this->config['pk']];
    }
1227354
protected function buildGlobalAction(array $config, array $parentConfig)
    {
        return $this->actionBuilder->build($this->prepareConfig($config, $parentConfig));
    }
1170904
private function add_to_parent_category_branch($category, $newnode, &$categoriesbranch) {

        foreach ($categoriesbranch as $key => $branch) {
            if (!empty($branch['categoryid']) && $branch['categoryid'] == $category->parent) {
                // It may be empty (if it does not contain courses and this is the first child cat).
                if (!isset($categoriesbranch[$key]['branches'])) {
                    $categoriesbranch[$key]['branches'] = [];
                }
                $categoriesbranch[$key]['branches'][] = $newnode;
                return true;
            }
            if (!empty($branch['branches'])) {
                $parent = $this->add_to_parent_category_branch($category, $newnode, $categoriesbranch[$key]['branches']);
                if ($parent) {
                    return true;
                }
            }
        }

        return false;
    }
865841
private function setTargets(SimpleXMLElement $parent)
    {
        $xml_targets = new CNabuXMLSiteTargetList($this->nb_data_object->getTargets(true));
        $xml_targets->build($parent);
    }
649619
public function xml(SimpleXMLElement $parent)
    {
        $option = $parent->addChild('option');
        $option->addAttribute('name', '--' . $this->_name);
        $short = '';
        if (strlen($this->_short) > 0) {
            $short = '-' . $this->_short;
        }
        $option->addAttribute('short', $short);
        $option->addAttribute('help', $this->_help);
        $option->addAttribute('boolean', (int)$this->_boolean);
        $option->addChild('default', $this->_default);
        $choices = $option->addChild('choices');
        foreach ($this->_choices as $valid) {
            $choices->addChild('choice', $valid);
        }

        return $parent;
    }
286381
public function build(SimpleXMLElement $parent = null) : SimpleXMLElement
    {
        if ($parent === null) {
            $xml = new SimpleXMLElement('<' . get_called_class()::getTagName() . "/>", LIBXML_PARSEHUGE);
        } else {
            $xml = $parent->addChild(get_called_class()::getTagName());
        }
        $this->setAttributes($xml);
        $this->setChilds($xml);

        return $xml;
    }
1137083
private function appendInheritanceNode(Inheritance $inheritance, \DOMNode $parentNode)
    {
        $inheritanceNode = $parentNode->appendChild($this->document->createElement('inheritance'));
        $inheritanceNode->setAttribute('key', $inheritance->getKey());
        $inheritanceNode->setAttribute('class', $inheritance->getClassName());

        if ($ancestor = $inheritance->getAncestor()) {
            $inheritanceNode->setAttribute('extends', $ancestor);
        }
    }
571397
@SuppressWarnings("unchecked")
  public void setParent(PvmExecutionImpl parent) {
    PvmExecutionImpl currentParent = getParent();

    setParentExecution(parent);

    if (currentParent != null) {
      currentParent.getExecutions().remove(this);
    }

    if (parent != null) {
      ((List<PvmExecutionImpl>) parent.getExecutions()).add(this);
    }
  }
901808
protected function buildBatch(array $config, array $parentConfig)
    {
        return $this->batchBuilder->build($this->prepareConfig($config, $parentConfig));
    }