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

SortNodeParser::parse()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 8.9197
cc 4
eloc 14
nc 4
nop 1
crap 4
1
<?php
2
namespace Xiag\Rql\Parser\NodeParser;
3
4
use Xiag\Rql\Parser\Token;
5
use Xiag\Rql\Parser\TokenStream;
6
use Xiag\Rql\Parser\NodeParserInterface;
7
use Xiag\Rql\Parser\Node\SortNode;
8
use Xiag\Rql\Parser\SubParserInterface;
9
10
class SortNodeParser implements NodeParserInterface
11
{
12
    /**
13
     * @var SubParserInterface
14
     */
15
    protected $fieldNameParser;
16
17
    /**
18
     * @param SubParserInterface $fieldNameParser
19
     */
20 64
    public function __construct(SubParserInterface $fieldNameParser)
21
    {
22 64
        $this->fieldNameParser = $fieldNameParser;
23 64
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28 1
    public function parse(TokenStream $tokenStream)
29
    {
30 1
        $fields = [];
31
32 1
        $tokenStream->expect(Token::T_OPERATOR, 'sort');
33 1
        $tokenStream->expect(Token::T_OPEN_PARENTHESIS);
34
35
        do {
36 1
            $direction = $tokenStream->expect([Token::T_PLUS, Token::T_MINUS]);
37 1
            $fields[$this->fieldNameParser->parse($tokenStream)] = $direction->test(Token::T_PLUS) ?
38 1
                SortNode::SORT_ASC :
39 1
                SortNode::SORT_DESC;
40
41 1
            if (!$tokenStream->nextIf(Token::T_COMMA)) {
42 1
                break;
43
            }
44 1
        } while (true);
45
46 1
        $tokenStream->expect(Token::T_CLOSE_PARENTHESIS);
47
48 1
        return new SortNode($fields);
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 6
    public function supports(TokenStream $tokenStream)
55
    {
56 6
        return $tokenStream->test(Token::T_OPERATOR, 'sort');
57
    }
58
}
59