Prewk\XmlStringStreamer\Parser\UniqueNode::getNodeFrom PHP Method

getNodeFrom() public method

Tries to retrieve the next node or returns false
public getNodeFrom ( Prewk\XmlStringStreamer\StreamInterface $stream ) : string | boolean
$stream Prewk\XmlStringStreamer\StreamInterface The stream to use
return string | boolean The next xml node or false if one could not be retrieved
    public function getNodeFrom(StreamInterface $stream)
    {
        while ($this->prepareChunk($stream)) {
            // What's our next course of action?
            if ($this->nextAction === self::FIND_OPENING_TAG_ACTION) {
                // Try to find an opening tag
                $positionInBlob = $this->getOpeningTagPos();
                if ($positionInBlob !== false) {
                    // We found it, start salvaging
                    $this->firstNodeFound = true;
                    if ($this->options["extractContainer"] && $this->preCapture) {
                        $this->containerXml .= substr($this->workingBlob, 0, $positionInBlob);
                        $this->preCapture = false;
                    }
                    $this->startSalvaging($positionInBlob);
                    // The next course of action will be to find a closing tag
                    $this->nextAction = self::FIND_CLOSING_TAG_ACTION;
                }
            }
            if ($this->nextAction === self::FIND_CLOSING_TAG_ACTION) {
                // Try to find a closing tag
                $positionInBlob = $this->getClosingTagPos();
                if ($positionInBlob !== false) {
                    // We found it, we now have a full node to flush out
                    $this->flush($positionInBlob);
                    // The next course of action will be to find an opening tag
                    $this->nextAction = self::FIND_OPENING_TAG_ACTION;
                    // Get the flushed node and make way for the next node
                    $flushed = $this->flushed;
                    $this->flushed = "";
                    return $flushed;
                }
            }
        }
        if ($this->options["extractContainer"]) {
            $this->containerXml .= $this->workingBlob;
        }
        return false;
    }

Usage Example

    public function test_multiple_roots()
    {
        $node1 = <<<eot
            <child>
                <foo baz="attribute" />
                <bar/>
                <index>1</index>
            </child>
eot;
        $node2 = <<<eot
            <child>
                <foo baz="attribute" />
                <bar/>
                <index>2</index>
            </child>
eot;
        $node3 = <<<eot
            <child>
                <foo baz="attribute" />
                <bar/>
                <index>3</index>
            </child>
eot;
        $node4 = <<<eot
            <child>
                <foo baz="attribute" />
                <bar/>
                <index>3</index>
            </child>
eot;
        $xml = <<<eot
            <?xml version="1.0"?>
            <root-a>
                {$node1}
                {$node2}
            </root-a>
            <root-b>
                {$node3}
                {$node4}
            </root-b>
eot;
        $stream = $this->getStreamMock($xml, 50);
        $parser = new UniqueNode(array("uniqueNode" => "child"));
        $this->assertEquals(trim($node1), trim($parser->getNodeFrom($stream)), "Node 1 should be obtained on the first getNodeFrom from root-a");
        $this->assertEquals(trim($node2), trim($parser->getNodeFrom($stream)), "Node 2 should be obtained on the second getNodeFrom from root-a");
        $this->assertEquals(trim($node3), trim($parser->getNodeFrom($stream)), "Node 3 should be obtained on the third getNodeFrom from root-b");
        $this->assertEquals(trim($node4), trim($parser->getNodeFrom($stream)), "Node 4 should be obtained on the third getNodeFrom from root-b");
        $this->assertFalse(false, "When no nodes are left, false should be returned");
    }
All Usage Examples Of Prewk\XmlStringStreamer\Parser\UniqueNode::getNodeFrom