FluentDOM\Node\WholeText::replaceWholeText PHP Method

replaceWholeText() public method

public replaceWholeText ( string $text )
$text string
    public function replaceWholeText($text)
    {
        /** @var \FluentDOM\Text|\FluentDOM\CdataSection $this */
        $text = (string) $text;
        $canReplaceEntity = function (\DOMEntityReference $reference) use(&$canReplaceEntity) {
            if ($reference->firstChild instanceof \DOMEntity && $reference->firstChild->hasChildNodes()) {
                foreach ($reference->firstChild->childNodes as $childNode) {
                    $canReplace = FALSE;
                    if ($childNode instanceof \DOMEntityReference) {
                        $canReplace = $canReplaceEntity($childNode);
                    } elseif ($childNode instanceof \DOMCharacterData) {
                        $canReplace = TRUE;
                    }
                    if (!$canReplace) {
                        return FALSE;
                    }
                }
                return TRUE;
            } else {
                return FALSE;
            }
        };
        $replaceNode = function (\DOMNode $node = NULL) use($canReplaceEntity) {
            if ($node instanceof \DOMElement || $node instanceof \DOMComment || $node instanceof \DOMProcessingInstruction) {
                return FALSE;
            } elseif ($node instanceof \DOMNode && $node->parentNode instanceof \DOMNode) {
                if ($node instanceof \DOMEntityReference && !$canReplaceEntity($node)) {
                    throw new \DOMException(LIBXML_NO_MODIFICATION_ALLOWED_ERR);
                }
                $node->parentNode->removeChild($node);
                return TRUE;
            }
            return FALSE;
        };
        do {
            $previous = $this->previousSibling;
        } while ($replaceNode($previous));
        do {
            $next = $this->nextSibling;
        } while ($replaceNode($next));
        if ($text === '') {
            if ($this->parentNode instanceof \DOMNode) {
                $this->parentNode->removeChild($this);
            }
            $this->textContent = '';
            return NULL;
        } else {
            $this->textContent = $text;
            return $this;
        }
    }