Fix a few test cases.

This commit is contained in:
James Cole
2025-01-05 09:18:03 +01:00
parent b6960dc299
commit 481bb3fb0a
3 changed files with 38 additions and 25 deletions

View File

@@ -29,6 +29,7 @@ use FireflyIII\Exceptions\FireflyException;
use Gdbots\QueryParser\QueryParser as BaseQueryParser; use Gdbots\QueryParser\QueryParser as BaseQueryParser;
use Gdbots\QueryParser\Node as GdbotsNode; use Gdbots\QueryParser\Node as GdbotsNode;
use Gdbots\QueryParser\Enum\BoolOperator; use Gdbots\QueryParser\Enum\BoolOperator;
use Illuminate\Support\Facades\Log;
class GdbotsQueryParser implements QueryParserInterface class GdbotsQueryParser implements QueryParserInterface
{ {
@@ -63,9 +64,10 @@ class GdbotsQueryParser implements QueryParserInterface
private function convertNode(GdbotsNode\Node $node): Node private function convertNode(GdbotsNode\Node $node): Node
{ {
switch (true) { switch (true) {
case $node instanceof GdbotsNode\Word: case $node instanceof GdbotsNode\Word:
return new StringNode($node->getValue()); return new StringNode($node->getValue(), BoolOperator::PROHIBITED === $node->getBoolOperator());
case $node instanceof GdbotsNode\Field: case $node instanceof GdbotsNode\Field:
return new FieldNode( return new FieldNode(
@@ -75,6 +77,7 @@ class GdbotsQueryParser implements QueryParserInterface
); );
case $node instanceof GdbotsNode\Subquery: case $node instanceof GdbotsNode\Subquery:
Log::debug('Subquery');
return new NodeGroup( return new NodeGroup(
array_map( array_map(
fn(GdbotsNode\Node $subNode) => $this->convertNode($subNode), fn(GdbotsNode\Node $subNode) => $this->convertNode($subNode),
@@ -90,7 +93,7 @@ class GdbotsQueryParser implements QueryParserInterface
case $node instanceof GdbotsNode\Mention: case $node instanceof GdbotsNode\Mention:
case $node instanceof GdbotsNode\Emoticon: case $node instanceof GdbotsNode\Emoticon:
case $node instanceof GdbotsNode\Emoji: case $node instanceof GdbotsNode\Emoji:
return new StringNode((string) $node->getValue()); return new StringNode((string) $node->getValue(), BoolOperator::PROHIBITED === $node->getBoolOperator());
default: default:
throw new FireflyException( throw new FireflyException(

View File

@@ -25,6 +25,8 @@ declare(strict_types=1);
namespace FireflyIII\Support\Search\QueryParser; namespace FireflyIII\Support\Search\QueryParser;
use Illuminate\Support\Facades\Log;
/** /**
* Base class for all nodes * Base class for all nodes
*/ */
@@ -45,8 +47,10 @@ abstract class Node
public function isProhibited(bool $flipFlag): bool public function isProhibited(bool $flipFlag): bool
{ {
if ($flipFlag) { if ($flipFlag) {
//Log::debug(sprintf('This %s is (flipped) now prohibited: %s',get_class($this), var_export(!$this->prohibited, true)));
return !$this->prohibited; return !$this->prohibited;
} }
//Log::debug(sprintf('This %s is (not flipped) now prohibited: %s',get_class($this), var_export($this->prohibited, true)));
return $this->prohibited; return $this->prohibited;
} }

View File

@@ -25,6 +25,8 @@ declare(strict_types=1);
namespace FireflyIII\Support\Search\QueryParser; namespace FireflyIII\Support\Search\QueryParser;
use Illuminate\Support\Facades\Log;
/** /**
* Represents a result from parsing a query node * Represents a result from parsing a query node
* *
@@ -35,9 +37,8 @@ class NodeResult
{ {
public function __construct( public function __construct(
public readonly ?Node $node, public readonly ?Node $node,
public readonly bool $isSubqueryEnd public readonly bool $isSubqueryEnd
) { ) {}
}
} }
@@ -49,12 +50,13 @@ class NodeResult
class QueryParser implements QueryParserInterface class QueryParser implements QueryParserInterface
{ {
private string $query; private string $query;
private int $position = 0; private int $position = 0;
/** @return NodeGroup */ /** @return NodeGroup */
public function parse(string $query): NodeGroup public function parse(string $query): NodeGroup
{ {
$this->query = $query; Log::debug(sprintf('Parsing query in QueryParser: "%s"', $query));
$this->query = $query;
$this->position = 0; $this->position = 0;
return $this->buildNodeGroup(false); return $this->buildNodeGroup(false);
} }
@@ -62,12 +64,12 @@ class QueryParser implements QueryParserInterface
/** @return NodeGroup */ /** @return NodeGroup */
private function buildNodeGroup(bool $isSubquery, bool $prohibited = false): NodeGroup private function buildNodeGroup(bool $isSubquery, bool $prohibited = false): NodeGroup
{ {
$nodes = []; $nodes = [];
$nodeResult = $this->buildNextNode($isSubquery); $nodeResult = $this->buildNextNode($isSubquery);
while ($nodeResult->node !== null) { while ($nodeResult->node !== null) {
$nodes[] = $nodeResult->node; $nodes[] = $nodeResult->node;
if($nodeResult->isSubqueryEnd) { if ($nodeResult->isSubqueryEnd) {
break; break;
} }
$nodeResult = $this->buildNextNode($isSubquery); $nodeResult = $this->buildNextNode($isSubquery);
@@ -79,9 +81,9 @@ class QueryParser implements QueryParserInterface
private function buildNextNode(bool $isSubquery): NodeResult private function buildNextNode(bool $isSubquery): NodeResult
{ {
$tokenUnderConstruction = ''; $tokenUnderConstruction = '';
$inQuotes = false; $inQuotes = false;
$fieldName = ''; $fieldName = '';
$prohibited = false; $prohibited = false;
while ($this->position < strlen($this->query)) { while ($this->position < strlen($this->query)) {
$char = $this->query[$this->position]; $char = $this->query[$this->position];
@@ -93,17 +95,19 @@ class QueryParser implements QueryParserInterface
$this->position++; $this->position++;
continue; continue;
} }
$this->position++; // char is "
return new NodeResult( $this->position++;
$this->createNode($tokenUnderConstruction, $fieldName, $prohibited), return new NodeResult(
false $this->createNode($tokenUnderConstruction, $fieldName, $prohibited),
); false
);
} }
switch ($char) { switch ($char) {
case '-': case '-':
if ($tokenUnderConstruction === '') { if ($tokenUnderConstruction === '') {
// A minus sign at the beginning of a token indicates prohibition // A minus sign at the beginning of a token indicates prohibition
Log::debug('Indicate prohibition');
$prohibited = true; $prohibited = true;
} }
if ($tokenUnderConstruction !== '') { if ($tokenUnderConstruction !== '') {
@@ -128,11 +132,11 @@ class QueryParser implements QueryParserInterface
// A left parentheses at the beginning of a token indicates the start of a subquery // A left parentheses at the beginning of a token indicates the start of a subquery
$this->position++; $this->position++;
return new NodeResult($this->buildNodeGroup(true, $prohibited), return new NodeResult($this->buildNodeGroup(true, $prohibited),
false false
); );
} }
// In any other location, it's just a normal character // In any other location, it's just a normal character
$tokenUnderConstruction .= $char; $tokenUnderConstruction .= $char;
break; break;
case ')': case ')':
@@ -142,8 +146,8 @@ class QueryParser implements QueryParserInterface
$this->position++; $this->position++;
return new NodeResult( return new NodeResult(
$tokenUnderConstruction !== '' $tokenUnderConstruction !== ''
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited) ? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
: null, : null,
true true
); );
} }
@@ -155,7 +159,7 @@ class QueryParser implements QueryParserInterface
case ':': case ':':
if ($tokenUnderConstruction !== '') { if ($tokenUnderConstruction !== '') {
// If we meet a colon with a left-hand side string, we know we're in a field and are about to set up the value // If we meet a colon with a left-hand side string, we know we're in a field and are about to set up the value
$fieldName = $tokenUnderConstruction; $fieldName = $tokenUnderConstruction;
$tokenUnderConstruction = ''; $tokenUnderConstruction = '';
} }
if ($tokenUnderConstruction === '') { if ($tokenUnderConstruction === '') {
@@ -183,8 +187,8 @@ class QueryParser implements QueryParserInterface
} }
$finalNode = $tokenUnderConstruction !== '' || $fieldName !== '' $finalNode = $tokenUnderConstruction !== '' || $fieldName !== ''
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited) ? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
: null; : null;
return new NodeResult($finalNode, true); return new NodeResult($finalNode, true);
} }
@@ -192,8 +196,10 @@ class QueryParser implements QueryParserInterface
private function createNode(string $token, string $fieldName, bool $prohibited): Node private function createNode(string $token, string $fieldName, bool $prohibited): Node
{ {
if (strlen($fieldName) > 0) { if (strlen($fieldName) > 0) {
Log::debug(sprintf('Create FieldNode %s:%s (%s)', $fieldName, $token, var_export($prohibited, true)));
return new FieldNode(trim($fieldName), trim($token), $prohibited); return new FieldNode(trim($fieldName), trim($token), $prohibited);
} }
Log::debug(sprintf('Create StringNode "%s" (%s)', $token, var_export($prohibited, true)));
return new StringNode(trim($token), $prohibited); return new StringNode(trim($token), $prohibited);
} }
} }