Kahlan\Jit\TokenStream::nextMatchingBracket PHP Method

nextMatchingBracket() public method

Move to the next matching bracket.
public nextMatchingBracket ( ) : string | null
return string | null Returns the skipped text content.
    public function nextMatchingBracket()
    {
        if (!$this->valid()) {
            return;
        }
        $matches = ['(' => ')', '{' => '}', '[' => ']'];
        $token = $this->current();
        $content = $open = $token[0];
        if (!isset($matches[$open])) {
            return;
        }
        $level = 1;
        $close = $matches[$open];
        $start = $this->_current;
        $count = $this->count();
        $this->_current++;
        while ($this->_current < $count) {
            $type = $this->_data[$this->_current][0];
            if ($type === $close) {
                $level--;
            } elseif ($type === $open) {
                $level++;
            }
            $content .= $this->_data[$this->_current][1];
            if ($level === 0) {
                return $content;
            }
            $this->_current++;
        }
        $this->_current = $start;
    }

Usage Example

Beispiel #1
0
         expect($actual)->toBe('{ { } }');
     });
     it("bails out nicely if there's no further tags", function () {
         $stream = new TokenStream(['source' => '']);
         $actual = $stream->nextMatchingBracket();
         expect($actual)->toBe(null);
     });
     it("bails out nicely if the current tags is not an open tags", function () {
         $stream = new TokenStream(['source' => '<?php ?>']);
         $actual = $stream->nextMatchingBracket();
         expect($actual)->toBe(null);
     });
     it("cancels the lookup if there's no closing tags", function () {
         $stream = new TokenStream(['source' => '<?php { { } ?>']);
         $stream->next('{');
         $actual = $stream->nextMatchingBracket();
         expect($actual)->toBe(null);
         expect($stream->getValue())->toBe('{');
     });
 });
 describe("->prev()", function () {
     it("moves prev", function () {
         $key = $this->stream->key();
         $this->stream->next();
         $this->stream->prev();
         expect($key)->not->toBe($this->stream->current());
     });
     it("gets the previous token value", function () {
         $this->stream->seek(1);
         $actual = $this->stream->prev();
         expect($actual)->toBe("<?php\n");