Completed
Push — master ( e5de66...386907 )
by Nico
02:02
created

TokenStream::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 * @link        https://github.com/nicoSWD
8
 * @author      Nicolas Oelgart <[email protected]>
9
 */
10
namespace nicoSWD\Rules;
11
12
use Closure;
13
use Iterator;
14
use nicoSWD\Rules\Exceptions\UndefinedVariableException;
15
use nicoSWD\Rules\Tokens\BaseToken;
16
use nicoSWD\Rules\Tokens\TokenEncapsedString;
17
use nicoSWD\Rules\Tokens\TokenFunction;
18
use nicoSWD\Rules\Tokens\TokenOpeningArray;
19
use nicoSWD\Rules\Tokens\TokenRegex;
20
use nicoSWD\Rules\Tokens\TokenString;
21
use nicoSWD\Rules\Tokens\TokenVariable;
22
use nicoSWD\Rules\AST\Nodes\NodeArray;
23
use nicoSWD\Rules\AST\Nodes\NodeFunction;
24
use nicoSWD\Rules\AST\Nodes\NodeString;
25
use nicoSWD\Rules\AST\Nodes\NodeVariable;
26
27
class TokenStream implements Iterator
28
{
29
    /** @var Stack */
30
    protected $stack;
31
32
    /** @var AST */
33
    private $ast;
34
35 220
    public function create(Stack $stack, AST $ast)
36
    {
37 220
        $stream = new self();
38 220
        $stream->stack = $stack;
39 220
        $stream->ast = $ast;
40
41 220
        return $stream;
42
    }
43
44 210
    public function next()
45
    {
46 210
        $this->stack->next();
47 210
    }
48
49 220
    public function valid(): bool
50
    {
51 220
        return $this->stack->valid();
52
    }
53
54 218
    public function current()
55
    {
56 218
        $current = $this->stack->current();
57
58 218
        switch (get_class($current)) {
59
            default:
60 208
                return $current;
61 218
            case TokenString::class:
62 218
            case TokenEncapsedString::class:
63 216
            case TokenRegex::class:
64 168
                $current = new NodeString($this);
65 168
                break;
66 214
            case TokenOpeningArray::class:
67 52
                $current = new NodeArray($this);
68 52
                break;
69 214
            case TokenVariable::class:
70 90
                $current = new NodeVariable($this);
71 90
                break;
72 212
            case TokenFunction::class:
73 26
                $current = new NodeFunction($this);
74 26
                break;
75
        }
76
77 202
        return $current->getNode();
78
    }
79
80
    public function key(): int
81
    {
82
       return $this->stack->key();
83
    }
84
85 220
    public function rewind()
86
    {
87 220
        $this->stack->rewind();
88 220
    }
89
90 90
    public function getVariable(string $name): BaseToken
91
    {
92
        try {
93 90
            return $this->ast->getVariable($name);
94 4
        } catch (UndefinedVariableException $e) {
95 4
            throw new Exceptions\ParserException(sprintf(
96 4
                'Undefined variable "%s" at position %d on line %d',
97 4
                $name,
98 4
                $this->stack->current()->getPosition(),
99 4
                $this->stack->current()->getLine()
100
            ));
101
        }
102
    }
103
104 26
    public function getFunction(string $functionName): Closure
105
    {
106 26
        return $this->ast->getFunction($functionName);
107
    }
108
109 202
    public function getStack(): Stack
110
    {
111 202
        return $this->stack;
112
    }
113
}
114