Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain multiple comparision expressions #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions src/Ast/Disjunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

class Disjunction extends Filter
{
/** @var Term[] */
/** @var Filter[] */
private $terms = [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this to $filters.


/**
* @param Term[] $terms
* @param Filter[] $terms
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same goes for this one, I would rename to $filters

*/
public function __construct(array $terms = [])
{
Expand All @@ -27,15 +27,15 @@ public function __construct(array $terms = [])
}

/**
* @param Term $term
* @param Filter $term
*/
public function add(Term $term)
public function add(Filter $term)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same goes for this one, I would rename to $filter

{
$this->terms[] = $term;
}

/**
* @return Term[]
* @return Filter[]
*/
public function getTerms()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming changing this from Term[] to Filter[] doesn't break anything, I would suggest renaming this to getFilters().

{
Expand Down
16 changes: 16 additions & 0 deletions src/Ast/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ public function dump()
return array_merge($this->valuePath->dump(), $this->attributePath->dump());
}
}

/**
* @return AttributePath
*/
public function getAttributePath()
{
return $this->attributePath;
}

/**
* @return ValuePath
*/
public function getValuePath()
{
return $this->valuePath;
}
Comment on lines +67 to +82
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a pending PR for this one: #7

}
10 changes: 8 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ private function disjunction()
$terms = [];
$terms[] = $this->conjunction();

if ($this->lexer->isNextToken(Tokens::T_SP)) {
$isNextTokenOr = true;
while($this->lexer->isNextToken(Tokens::T_SP) && $isNextTokenOr) {
$nextToken = $this->lexer->glimpse();
if ($this->isName('or', $nextToken)) {
$this->match(Tokens::T_SP);
$this->match(Tokens::T_NAME);
$this->match(Tokens::T_SP);
$terms[] = $this->conjunction();
} else {
$isNextTokenOr = false;
}
}
Comment on lines -130 to 141
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a variable to break the while loop, simply use the break statement instead.

while ($this->lexer->isNextToken(Tokens::T_SP)) {
    $nextToken = $this->lexer->glimpse();
    
    if ($this->isName('or', $nextToken) === false) {
        break;
    }
    
    $this->match(Tokens::T_SP);
    $this->match(Tokens::T_NAME);
    $this->match(Tokens::T_SP);
    $terms[] = $this->conjunction();
}


Expand All @@ -152,13 +155,16 @@ private function conjunction()
$factors = [];
$factors[] = $this->factor();

if ($this->lexer->isNextToken(Tokens::T_SP)) {
$isNextTokenAnd = true;
while($this->lexer->isNextToken(Tokens::T_SP) && $isNextTokenAnd) {
$nextToken = $this->lexer->glimpse();
if ($this->isName('and', $nextToken)) {
$this->match(Tokens::T_SP);
$this->match(Tokens::T_NAME);
$this->match(Tokens::T_SP);
$factors[] = $this->factor();
} else {
$isNextTokenAnd = false;
}
}
Comment on lines -155 to 169
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same goes for this one:

while ($this->lexer->isNextToken(Tokens::T_SP)) {
    $nextToken = $this->lexer->glimpse();
    
    if ($this->isName('and', $nextToken) === false) {
        break;
    }
    
    $this->match(Tokens::T_SP);
    $this->match(Tokens::T_NAME);
    $this->match(Tokens::T_SP);
    $factors[] = $this->factor();
}


Expand Down
75 changes: 74 additions & 1 deletion tests/ParserFilterModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,25 @@ public function parser_provider_v2()
]
]
],

[
'((name eq "mike" or id eq "123") or name eq "alina") and username eq "john"',
[
'Conjunction' => [
[
'Disjunction' => [
[
'Disjunction' => [
['ComparisonExpression' => 'name eq mike'],
['ComparisonExpression' => 'id eq 123'],
]
],
['ComparisonExpression' => 'name eq alina']
]
],
['ComparisonExpression' => 'username eq john']
]
]
],
[
'username eq "john" and not (name sw "mike" or id ew "123")',
[
Expand All @@ -235,6 +253,60 @@ public function parser_provider_v2()
]
]
],
[
'emails co "example.com" and emails.value co "example.org" and emails.value co "example.info"',
[
'Conjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
['ComparisonExpression' => 'emails.value co example.info'],
]
]
],
[
'emails co "example.com" or emails.value co "example.org" or emails.value co "example.info"',
[
'Disjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
['ComparisonExpression' => 'emails.value co example.info'],
]
]
],
[
'emails co "example.com" or emails.value co "example.org" or emails.value co "example.info" or (emails co "example.com" and emails.value co "example.org" and emails.value co "example.info")',
[
'Disjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
['ComparisonExpression' => 'emails.value co example.info'],
[
'Conjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
['ComparisonExpression' => 'emails.value co example.info'],
]
]
]
]
],
[
'emails co "example.com" or emails.value co "example.org" or emails.value co "example.info" or emails co "example.com" and emails.value co "example.org" and emails.value co "example.info"',
[
'Disjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
['ComparisonExpression' => 'emails.value co example.info'],
[
'Conjunction' => [
['ComparisonExpression' => 'emails co example.com'],
['ComparisonExpression' => 'emails.value co example.org'],
['ComparisonExpression' => 'emails.value co example.info'],
]
]
]
]
],
];
}

Expand All @@ -245,6 +317,7 @@ public function test_parser_v2($filterString, array $expectedDump)
{
$parser = $this->getParser();
$node = $parser->parse($filterString);

$this->assertEquals($expectedDump, $node->dump(), sprintf("\n\n%s\n%s\n\n", $filterString, json_encode($node->dump(), JSON_PRETTY_PRINT)));
}

Expand Down