|
1
|
|
|
<?php |
|
2
|
|
|
namespace Xiag\Rql\Parser\NodeParser\Query; |
|
3
|
|
|
|
|
4
|
|
|
use Xiag\Rql\Parser\Token; |
|
5
|
|
|
use Xiag\Rql\Parser\TokenStream; |
|
6
|
|
|
use Xiag\Rql\Parser\NodeParserInterface; |
|
7
|
|
|
use Xiag\Rql\Parser\SubParserInterface; |
|
8
|
|
|
use Xiag\Rql\Parser\Exception\SyntaxErrorException; |
|
9
|
|
|
use Xiag\Rql\Parser\Node\AbstractQueryNode; |
|
10
|
|
|
use Xiag\Rql\Parser\Node\Query\LogicalOperator\AndNode; |
|
11
|
|
|
use Xiag\Rql\Parser\Node\Query\LogicalOperator\OrNode; |
|
12
|
|
|
|
|
13
|
|
|
class GroupNodeParser implements NodeParserInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var SubParserInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $conditionParser; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param SubParserInterface $conditionParser |
|
22
|
|
|
*/ |
|
23
|
64 |
|
public function __construct(SubParserInterface $conditionParser) |
|
24
|
|
|
{ |
|
25
|
64 |
|
$this->conditionParser = $conditionParser; |
|
26
|
64 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
* @return AbstractQueryNode |
|
31
|
|
|
*/ |
|
32
|
5 |
|
public function parse(TokenStream $tokenStream) |
|
33
|
|
|
{ |
|
34
|
5 |
|
$operator = null; |
|
35
|
5 |
|
$queries = []; |
|
36
|
|
|
|
|
37
|
5 |
|
$tokenStream->expect(Token::T_OPEN_PARENTHESIS); |
|
38
|
|
|
|
|
39
|
|
|
do { |
|
40
|
5 |
|
$queries[] = $this->conditionParser->parse($tokenStream); |
|
41
|
|
|
|
|
42
|
5 |
|
if ($tokenStream->nextIf(Token::T_AMPERSAND)) { |
|
43
|
4 |
View Code Duplication |
if ($operator === null) { |
|
|
|
|
|
|
44
|
3 |
|
$operator = Token::T_AMPERSAND; |
|
45
|
4 |
|
} elseif ($operator !== Token::T_AMPERSAND) { |
|
46
|
1 |
|
throw new SyntaxErrorException('Cannot mix "&" and "|" within a group'); |
|
47
|
|
|
} |
|
48
|
5 |
|
} elseif ($tokenStream->nextIf(Token::T_VERTICAL_BAR)) { |
|
49
|
4 |
View Code Duplication |
if ($operator === null) { |
|
|
|
|
|
|
50
|
3 |
|
$operator = Token::T_VERTICAL_BAR; |
|
51
|
4 |
|
} elseif ($operator !== Token::T_VERTICAL_BAR) { |
|
52
|
1 |
|
throw new SyntaxErrorException('Cannot mix "&" and "|" within a group'); |
|
53
|
|
|
} |
|
54
|
3 |
|
} else { |
|
55
|
3 |
|
break; |
|
56
|
|
|
} |
|
57
|
4 |
|
} while (true); |
|
58
|
|
|
|
|
59
|
3 |
|
$tokenStream->expect(Token::T_CLOSE_PARENTHESIS); |
|
60
|
|
|
|
|
61
|
3 |
|
if ($operator === Token::T_VERTICAL_BAR) { |
|
62
|
2 |
|
return new OrNode($queries); |
|
63
|
3 |
|
} elseif ($operator === Token::T_AMPERSAND) { |
|
64
|
2 |
|
return new AndNode($queries); |
|
65
|
|
|
} else { |
|
66
|
1 |
|
return $queries[0]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
*/ |
|
73
|
64 |
|
public function supports(TokenStream $tokenStream) |
|
74
|
|
|
{ |
|
75
|
64 |
|
return $tokenStream->test(Token::T_OPEN_PARENTHESIS); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.