Skip to content

Commit

Permalink
Respect boolean operator preceding subquery. (#10)
Browse files Browse the repository at this point in the history
* Respect boolean operator preceding subquery (#9)

* Respect boolean operator preceding subquery

* Add test for negating subqueries

* pull #9 ensure fromArray brings in bool operator as well and update doc block and changelog
  • Loading branch information
gdbrown authored Dec 28, 2016
1 parent 654bb28 commit 713f4e6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-0.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
This changelog references the relevant changes done in 0.x versions.


## v0.2.1
* pull #9: Respect boolean operator preceding subquery.


## v0.2.0
__BREAKING CHANGES__

Expand Down
20 changes: 16 additions & 4 deletions src/Node/Subquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Gdbots\QueryParser\Node;

use Gdbots\QueryParser\Builder\QueryBuilder;
use Gdbots\QueryParser\Enum\BoolOperator;

final class Subquery extends Node
{
Expand All @@ -16,14 +17,19 @@ final class Subquery extends Node
* Subquery constructor.
*
* @param Node[] $nodes
* @param BoolOperator $boolOperator
* @param bool $useBoost
* @param float|mixed $boost
*
* @throws \LogicException
*/
public function __construct(array $nodes, $useBoost = false, $boost = self::DEFAULT_BOOST)
{
parent::__construct(null, null, $useBoost, $boost);
public function __construct(
array $nodes,
BoolOperator $boolOperator = null,
$useBoost = false,
$boost = self::DEFAULT_BOOST
) {
parent::__construct(null, $boolOperator, $useBoost, $boost);
$this->nodes = $nodes;

foreach ($this->nodes as $node) {
Expand All @@ -49,7 +55,13 @@ public static function fromArray(array $data = [])
}
}

return new self($nodes, $useBoost, $boost);
try {
$boolOperator = isset($data['bool_operator']) ? BoolOperator::create($data['bool_operator']) : null;
} catch (\Exception $e) {
$boolOperator = null;
}

return new self($nodes, $boolOperator, $useBoost, $boost);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/QueryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ protected function handleFieldWithRange($fieldName, BoolOperator $boolOperator)
return new Field($fieldName, $nodes[0], $boolOperator, $m['use_boost'], $m['boost']);
}

$subquery = new Subquery($nodes, $m['use_boost'], $m['boost']);
$subquery = new Subquery($nodes, null, $m['use_boost'], $m['boost']);
return new Field($fieldName, $subquery, $boolOperator, $m['use_boost'], $m['boost']);
}

Expand Down Expand Up @@ -374,7 +374,7 @@ protected function handleSubquery(BoolOperator $queryBoolOperator)
return $nodes[0]::fromArray($data);
}

return new Subquery($nodes, $m['use_boost'], $m['boost']);
return new Subquery($nodes, $queryBoolOperator, $m['use_boost'], $m['boost']);
}

/**
Expand Down
38 changes: 37 additions & 1 deletion tests/Fixtures/test-queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,42 @@
new Subquery([new Word('word:a'), new Word('hashtag:b')]),
]
],

[
'name' => 'booleans before and in subqueries',
'input' => '"ipad pro" AND (gold OR silver)',
'expected_tokens' => [
[T::T_PHRASE, 'ipad pro'],
T::T_AND,
T::T_SUBQUERY_START,
[T::T_WORD, 'gold'],
T::T_OR,
[T::T_WORD, 'silver'],
T::T_SUBQUERY_END,
],
'expected_nodes' => [
new Phrase('ipad pro', BoolOperator::REQUIRED()),
new Subquery([new Word('gold'), new Word('silver')], BoolOperator::REQUIRED()),
]
],

[
'name' => 'booleans before and in subqueries 2',
'input' => '"iphone 7" -(16gb OR 32gb)',
'expected_tokens' => [
[T::T_PHRASE, 'iphone 7'],
T::T_PROHIBITED,
T::T_SUBQUERY_START,
[T::T_WORD, '16gb'],
T::T_OR,
[T::T_WORD, '32gb'],
T::T_SUBQUERY_END,
],
'expected_nodes' => [
new Phrase('iphone 7'),
new Subquery([new Word('16gb'), new Word('32gb')], BoolOperator::PROHIBITED()),
]
],
/*
* END: SUBQUERIES
*/
Expand Down Expand Up @@ -1569,7 +1605,7 @@
],
'expected_nodes' => [
new Phrase('john smith', null, true, 2.0),
new Subquery([new Word('foo'), new Word('bar')], true, 4.0),
new Subquery([new Word('foo'), new Word('bar')], null, true, 4.0),
]
],

Expand Down

0 comments on commit 713f4e6

Please sign in to comment.