Generate <p> element replacements for supplied elements child nodes as required.

@param Element $node

@return NodeList $nodesToReturn Replacement elements
964105
private function getReplacementNodes(Element $node): NodeList {
        $nodesToReturn = $node->newNodeList();
        $nodesToRemove = $node->newNodeList();
        $replacementNodes = $node->newNodeList();

        $fnCompareSiblingNodes = function($node) {
            if ($node->is(':not(a)') || $node->nodeType == XML_TEXT_NODE) {
                return true;
            }
        };

        foreach ($node->contents() as $child) {
            if ($child->is('p') && $replacementNodes->count()) {
                $nodesToReturn[] = $this->getFlushedBuffer($replacementNodes);
                $replacementNodes->fromArray([]);
                $nodesToReturn[] = $child;
            } else if ($child->nodeType == XML_TEXT_NODE) {
                $replaceText = $child->text();

                if (!empty($replaceText)) {
                    // Get all previous sibling <a> nodes, the current text node, and all next sibling <a> nodes.
                    $siblings = $child
                        ->precedingUntil($fnCompareSiblingNodes, 'a')
                        ->merge([$child])
                        ->merge($child->followingUntil($fnCompareSiblingNodes, 'a'));

                    foreach ($siblings as $sibling) {
                        // Place current nodes textual contents in-between previous and next nodes.
                        if ($sibling->isSameNode($child)) {
                            $replacementNodes[] = new Text($replaceText);

                        // Grab the contents of any unprocessed <a> siblings and flag them for removal.
                        } else if ($sibling->getAttribute('grv-usedalready') != 'yes') {
                            $sibling->setAttribute('grv-usedalready', 'yes');

                            $replacementNodes[] = $sibling->cloneNode(true);
                            $nodesToRemove[] = $sibling;
                        }

                    }
                }

                $nodesToRemove[] = $child;
            } else {
                if ($replacementNodes->count()) {
                    $nodesToReturn[] = $this->getFlushedBuffer($replacementNodes);
                    $replacementNodes->fromArray([]);
                }

                $nodesToReturn[] = $child;
            }
        }

        // Flush any remaining replacementNodes left over from text nodes.
        if ($replacementNodes->count()) {
            $nodesToReturn[] = $this->getFlushedBuffer($replacementNodes);
        }

        // Remove potential duplicate <a> tags.
        foreach ($nodesToReturn as $key => $return) {
            if ($nodesToRemove->exists($return)) {
                unset($nodesToReturn[$key]);
            }
        }

        $nodesToRemove->remove();

        return $nodesToReturn;
    }
1187630
private function visitNode(\DOMNode $node)
    {
        if ($this->node !== $node) {
            $this->node->appendChild($node);
        }

        return $node;
    }
225915
private function processNode(\SimpleXMLElement $node): array
    {
        $config = [];
        foreach ($node as $name => $value) {
            if (\preg_match('/^num__([0-9]+)$/', $name, $matches)) {
                $name = $matches[1];
            }
            if ($value->count() > 0) {
                $config[$name] = $this->processNode($value->children());
            } else {
                $value = (string)$value;
                $config[$name] = $value;

                if (\preg_match('/^[0-9]+$/', $value)) {
                    $config[$name] = (int)$value;
                } elseif (\preg_match('/^[0-9\.]+$/', $value)) {
                    $config[$name] = (float)$value;
                } elseif (\preg_match('/^true|false$/', $value)) {
                    $config[$name] = $value === 'true' ? true : false;
                }
            }
        }
        return $config;
    }
624637
public function enterNode(Node $node)
    {
        if ($node instanceof Namespace_) {
            $this->resetContext($node->name);
        }

        $this->recordDoc($node->getAttribute('comments', []));

        return $node;
    }
1024431
protected function handleBlocks(Node $node)
    {

        $this->blocks = $node->findArray('block');
        foreach ($this->blocks as $blockNode)
            $this->handleBlock($blockNode);

        return $this;
    }
1017940
protected function nodeToDictionary( Node $node )
    {
        $result = $node->attributes;

        $containerKeys = [];

        foreach ($node->children as $child) {
            $arrayChild = $this->nodeToDictionary($child);

            $childName = $child->name;
            $childValue = $arrayChild;

            if (sizeof($child->children) == 0) {
                $childValue = $child->text;
            }

            if (isset( $result[ $childName ] )) {
                if (!in_array($childName, $containerKeys)) {
                    $result[ $childName ] = [$result[ $childName ]];
                    $containerKeys[] = $childName;
                }

                $result[ $childName ][] = $childValue;
            } else {
                $result[ $childName ] = $childValue;
            }
        }

        return $result;
    }
1261103
public function mutate(Node $node)
    {
        return new Node\Expr\AssignOp\Minus($node->var, $node->expr, $node->getAttributes());
    }
886999
public function processNodes(DOMNode $node)
    {
        // get all link nodes
        $allLinkNodes = $this->xp->query('.//a', $node);
        $allLinkNodesSortedByLength = [];
        foreach ($allLinkNodes as $linkNode) {
            $allLinkNodesSortedByLength[] = [
                'href' => $linkNode->getAttribute('href'),
                'node' => $linkNode,
            ];
        }

        // sort by link-length
        usort(
            $allLinkNodesSortedByLength,
            function ($a, $b) {
                $lengthA = strlen($a['href']);
                $lengthB = strlen($b['href']);
                if ($lengthA == $lengthB) {
                    return 0;
                }

                return ($lengthA < $lengthB) ? 1 : -1;
            }
        );

        // set best matching link node to active, all ancestors to open
        $requestedUri = $this->requestedUri;
        while (DIRECTORY_SEPARATOR !== $requestedUri) {
            foreach ($allLinkNodesSortedByLength as $currentLinkNode) {
                if ($currentLinkNode['href'] === $requestedUri) {
                    $currentLinkNode['node']->setAttribute('class', trim($currentLinkNode['node']->getAttribute('class').' '.self::CLASS_ACTIVE));

                    // set nearest li ancestor to active
                    $ancestorListNode = $this->xp->query('./ancestor::li', $currentLinkNode['node'])->item(
                        $this->xp->query('./ancestor::li', $currentLinkNode['node'])->length - 1
                    );
                    $ancestorListNode->setAttribute('class', trim($ancestorListNode->getAttribute('class').' '.self::CLASS_ACTIVE));

                    // set all other li ancestors to open and all links inside them to open
                    $remainingAncestorListNodes = $this->xp->query('ancestor::li', $ancestorListNode);
                    foreach ($remainingAncestorListNodes as $remainingAncestorListNode) {
                        $remainingAncestorListNode->setAttribute('class', trim($remainingAncestorListNode->getAttribute('class').' '.self::CLASS_OPEN));
                        $descendantAnchorNode = $this->xp->query('descendant::a', $remainingAncestorListNode)->item(0);
                        $descendantAnchorNode->setAttribute('class', trim($descendantAnchorNode->getAttribute('class').' '.self::CLASS_OPEN));
                    }

                    return;
                }
            }
            $requestedUri = dirname($requestedUri);
        }

        return false;
    }
1270986
public function transform($node)
    {
        if (!$node) {
            return $node;
        }

        if (\is_array($node) || $node instanceof \Traversable) {
            $ids = [];
            /** @var array $node */
            foreach ($node as $n) {
                $ids[] = $this->transform($n);
            }

            return $ids;
        }

        return IDEncoder::encode($node);
    }
727438
public function mutate(Node $node)
    {
        return new Node\Expr\Assign($node->left, $node->right, $node->getAttributes());
    }
169340
public function generateTree($nodes)
    {
        $superRoot = count(array_filter($nodes, function (ReadNodeInterface $node) {
            return '-' == $node->getParentId();
        }))? '-': 'root';

        $list = array();
        $list[$superRoot] = array();
        foreach ($nodes as $node) {
            if ($superRoot !== $node->getParentId() && $this->parentInList($node->getParentId(), $nodes)) {
                $list[$node->getParentId()][] = $node;
                continue;
            }
            $list[$superRoot][] = $node;
        }

        $tree = $this->createTree($list[$superRoot], $list);

        return $tree;
    }
1270203
public function transform($node)
    {
        $blocksData = $this->transformBlock($node);
        $documentData = array(
            'id' => $node->getNodeId() . '-' . $node->getLanguage(). '-' . $node->getSiteId(),
            'elementId' => $node->getId(),
            'nodeId' => $node->getNodeId(),
            'siteId' => $node->getSiteId(),
            'language' => $node->getLanguage(),
            'name' => $node->getName(),
            'blocks' => $blocksData
        );

        if (!is_null($node->getUpdatedAt())) {
            $documentData['updatedAt'] = $node->getUpdatedAt()->getTimestamp();
        }

        $document = new Document($documentData['id'], $documentData);

        return $document;
    }
1295262
public function mutate(Node $node)
    {
        return new Node\Stmt\Foreach_(
            new Node\Expr\Array_(),
            $node->valueVar,
            [
                'keyVar' => $node->keyVar,
                'byRef' => $node->byRef,
                'stmts' => $node->stmts,
            ],
            $node->getAttributes()
        );
    }
519276
public function append(Node $node)
    {

        if (!is_array($this->_children)) {
            $this->_children = [];
        }

        $this->_children[] = $node;
        return $this;
    }
12151
protected function getChildren(NodeBuilder $node)
    {
        $node = $this->getOptionsNodes($node);
        $node = $this->getConnectionsNodes($node);
        $node = $this->getOverridesNodes($node);

        return $node;
    }
124951
public function getFlattenChildren(Node $node)
    {
        $flatten = array();
        foreach ($node->getNodes() as $subNode) {
            if (count($subNode->getNodes()) > 1 || isset($subNode->getTrace()['query_source']) || !empty($subNode->getValues())) {
                $flatten[] = $subNode;
            } else {
                $flatten = array_merge($flatten, $this->getFlattenChildren($subNode));
            }
        }

        return $flatten;
    }
746448
public function mutate(Node $node)
    {
        return new Node\Expr\Assign($node->var, $node->expr, $node->getAttributes());
    }