Completed
Push — master ( 82eb41...4f6b0a )
by Alexander
03:49
created

NodeParserChain::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3
1
<?php
2
namespace Xiag\Rql\Parser;
3
4
use Xiag\Rql\Parser\Exception\SyntaxErrorException;
5
6
class NodeParserChain implements NodeParserInterface
7
{
8
    /**
9
     * @var NodeParserInterface[]
10
     */
11
    protected $nodeParsers;
12
13
    /**
14
     * @param NodeParserInterface $nodeParser
15
     * @return $this
16
     */
17 64
    public function addNodeParser(NodeParserInterface $nodeParser)
18
    {
19 64
        $this->nodeParsers[] = $nodeParser;
20 64
        return $this;
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 64
    public function parse(TokenStream $tokenStream)
27
    {
28 64
        foreach ($this->nodeParsers as $nodeParser) {
29 64
            if ($nodeParser->supports($tokenStream)) {
30 63
                return $nodeParser->parse($tokenStream);
31
            }
32 64
        }
33
34 1
        throw new SyntaxErrorException(
35 1
            sprintf(
36 1
                'Unexpected token "%s" (%s) at position %d',
37 1
                $tokenStream->getCurrent()->getValue(),
38 1
                $tokenStream->getCurrent()->getName(),
39 1
                $tokenStream->getCurrent()->getStart()
40 1
            )
41 1
        );
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 64
    public function supports(TokenStream $tokenStream)
48
    {
49 64
        foreach ($this->nodeParsers as $nodeParser) {
50 64
            if ($nodeParser->supports($tokenStream)) {
51 58
                return true;
52
            }
53 59
        }
54
55 6
        return false;
56
    }
57
}
58