mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-19 08:00:19 +00:00
Auto commit for release 'develop' on 2025-01-05
This commit is contained in:
@@ -35,8 +35,8 @@ class FieldNode extends Node
|
||||
|
||||
public function __construct(string $operator, string $value, bool $prohibited = false)
|
||||
{
|
||||
$this->operator = $operator;
|
||||
$this->value = $value;
|
||||
$this->operator = $operator;
|
||||
$this->value = $value;
|
||||
$this->prohibited = $prohibited;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,17 +41,17 @@ class GdbotsQueryParser implements QueryParserInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return NodeGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function parse(string $query): NodeGroup
|
||||
{
|
||||
try {
|
||||
$result = $this->parser->parse($query);
|
||||
$nodes = array_map(
|
||||
fn(GdbotsNode\Node $node) => $this->convertNode($node),
|
||||
$nodes = array_map(
|
||||
fn (GdbotsNode\Node $node) => $this->convertNode($node),
|
||||
$result->getNodes()
|
||||
);
|
||||
|
||||
return new NodeGroup($nodes);
|
||||
} catch (\LogicException|\TypeError $e) {
|
||||
fwrite(STDERR, "Setting up GdbotsQueryParserTest\n");
|
||||
@@ -78,9 +78,10 @@ class GdbotsQueryParser implements QueryParserInterface
|
||||
|
||||
case $node instanceof GdbotsNode\Subquery:
|
||||
Log::debug('Subquery');
|
||||
|
||||
return new NodeGroup(
|
||||
array_map(
|
||||
fn(GdbotsNode\Node $subNode) => $this->convertNode($subNode),
|
||||
fn (GdbotsNode\Node $subNode) => $this->convertNode($subNode),
|
||||
$node->getNodes()
|
||||
)
|
||||
);
|
||||
|
||||
@@ -47,10 +47,11 @@ abstract class Node
|
||||
public function isProhibited(bool $flipFlag): bool
|
||||
{
|
||||
if ($flipFlag) {
|
||||
//Log::debug(sprintf('This %s is (flipped) now prohibited: %s',get_class($this), var_export(!$this->prohibited, true)));
|
||||
// Log::debug(sprintf('This %s is (flipped) now prohibited: %s',get_class($this), var_export(!$this->prohibited, true)));
|
||||
return !$this->prohibited;
|
||||
}
|
||||
//Log::debug(sprintf('This %s is (not flipped) now prohibited: %s',get_class($this), var_export($this->prohibited, true)));
|
||||
|
||||
// Log::debug(sprintf('This %s is (not flipped) now prohibited: %s',get_class($this), var_export($this->prohibited, true)));
|
||||
return $this->prohibited;
|
||||
|
||||
}
|
||||
|
||||
@@ -37,11 +37,10 @@ class NodeGroup extends Node
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @param bool $prohibited
|
||||
*/
|
||||
public function __construct(array $nodes, bool $prohibited = false)
|
||||
{
|
||||
$this->nodes = $nodes;
|
||||
$this->nodes = $nodes;
|
||||
$this->prohibited = $prohibited;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,23 +52,22 @@ class QueryParser implements QueryParserInterface
|
||||
private string $query;
|
||||
private int $position = 0;
|
||||
|
||||
/** @return NodeGroup */
|
||||
public function parse(string $query): NodeGroup
|
||||
{
|
||||
Log::debug(sprintf('Parsing query in QueryParser: "%s"', $query));
|
||||
$this->query = $query;
|
||||
$this->position = 0;
|
||||
|
||||
return $this->buildNodeGroup(false);
|
||||
}
|
||||
|
||||
/** @return NodeGroup */
|
||||
private function buildNodeGroup(bool $isSubquery, bool $prohibited = false): NodeGroup
|
||||
{
|
||||
$nodes = [];
|
||||
$nodeResult = $this->buildNextNode($isSubquery);
|
||||
|
||||
while ($nodeResult->node !== null) {
|
||||
$nodes[] = $nodeResult->node;
|
||||
while (null !== $nodeResult->node) {
|
||||
$nodes[] = $nodeResult->node;
|
||||
if ($nodeResult->isSubqueryEnd) {
|
||||
break;
|
||||
}
|
||||
@@ -90,13 +89,15 @@ class QueryParser implements QueryParserInterface
|
||||
|
||||
// If we're in a quoted string, we treat all characters except another quote as ordinary characters
|
||||
if ($inQuotes) {
|
||||
if ($char !== '"') {
|
||||
if ('"' !== $char) {
|
||||
$tokenUnderConstruction .= $char;
|
||||
$this->position++;
|
||||
++$this->position;
|
||||
|
||||
continue;
|
||||
}
|
||||
// char is "
|
||||
$this->position++;
|
||||
++$this->position;
|
||||
|
||||
return new NodeResult(
|
||||
$this->createNode($tokenUnderConstruction, $fieldName, $prohibited),
|
||||
false
|
||||
@@ -105,47 +106,53 @@ class QueryParser implements QueryParserInterface
|
||||
|
||||
switch ($char) {
|
||||
case '-':
|
||||
if ($tokenUnderConstruction === '') {
|
||||
if ('' === $tokenUnderConstruction) {
|
||||
// A minus sign at the beginning of a token indicates prohibition
|
||||
Log::debug('Indicate prohibition');
|
||||
$prohibited = true;
|
||||
}
|
||||
if ($tokenUnderConstruction !== '') {
|
||||
if ('' !== $tokenUnderConstruction) {
|
||||
// In any other location, it's just a normal character
|
||||
$tokenUnderConstruction .= $char;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case '"':
|
||||
if ($tokenUnderConstruction === '') {
|
||||
if ('' === $tokenUnderConstruction) {
|
||||
// A quote sign at the beginning of a token indicates the start of a quoted string
|
||||
$inQuotes = true;
|
||||
}
|
||||
if ($tokenUnderConstruction !== '') {
|
||||
if ('' !== $tokenUnderConstruction) {
|
||||
// In any other location, it's just a normal character
|
||||
$tokenUnderConstruction .= $char;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case '(':
|
||||
if ($tokenUnderConstruction === '') {
|
||||
if ('' === $tokenUnderConstruction) {
|
||||
// A left parentheses at the beginning of a token indicates the start of a subquery
|
||||
$this->position++;
|
||||
return new NodeResult($this->buildNodeGroup(true, $prohibited),
|
||||
false
|
||||
++$this->position;
|
||||
|
||||
return new NodeResult(
|
||||
$this->buildNodeGroup(true, $prohibited),
|
||||
false
|
||||
);
|
||||
}
|
||||
// In any other location, it's just a normal character
|
||||
$tokenUnderConstruction .= $char;
|
||||
|
||||
break;
|
||||
|
||||
case ')':
|
||||
// A right parentheses while in a subquery means the subquery ended,
|
||||
// thus also signaling the end of any node currently being built
|
||||
if ($isSubquery) {
|
||||
$this->position++;
|
||||
++$this->position;
|
||||
|
||||
return new NodeResult(
|
||||
$tokenUnderConstruction !== ''
|
||||
'' !== $tokenUnderConstruction
|
||||
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
|
||||
: null,
|
||||
true
|
||||
@@ -153,40 +160,44 @@ class QueryParser implements QueryParserInterface
|
||||
}
|
||||
// In any other location, it's just a normal character
|
||||
$tokenUnderConstruction .= $char;
|
||||
|
||||
break;
|
||||
|
||||
|
||||
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
|
||||
$fieldName = $tokenUnderConstruction;
|
||||
$tokenUnderConstruction = '';
|
||||
}
|
||||
if ($tokenUnderConstruction === '') {
|
||||
if ('' === $tokenUnderConstruction) {
|
||||
// In any other location, it's just a normal character
|
||||
$tokenUnderConstruction .= $char;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ' ':
|
||||
// A space indicates the end of a token construction if non-empty, otherwise it's just ignored
|
||||
if ($tokenUnderConstruction !== '') {
|
||||
$this->position++;
|
||||
if ('' !== $tokenUnderConstruction) {
|
||||
++$this->position;
|
||||
|
||||
return new NodeResult(
|
||||
$this->createNode($tokenUnderConstruction, $fieldName, $prohibited),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$tokenUnderConstruction .= $char;
|
||||
}
|
||||
|
||||
$this->position++;
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
$finalNode = $tokenUnderConstruction !== '' || $fieldName !== ''
|
||||
$finalNode = '' !== $tokenUnderConstruction || '' !== $fieldName
|
||||
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
|
||||
: null;
|
||||
|
||||
@@ -195,11 +206,13 @@ class QueryParser implements QueryParserInterface
|
||||
|
||||
private function createNode(string $token, string $fieldName, bool $prohibited): Node
|
||||
{
|
||||
if (strlen($fieldName) > 0) {
|
||||
if ('' !== $fieldName) {
|
||||
Log::debug(sprintf('Create FieldNode %s:%s (%s)', $fieldName, $token, var_export($prohibited, true)));
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ namespace FireflyIII\Support\Search\QueryParser;
|
||||
interface QueryParserInterface
|
||||
{
|
||||
/**
|
||||
* @return NodeGroup
|
||||
* @throws \LogicException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
|
||||
@@ -34,7 +34,7 @@ class StringNode extends Node
|
||||
|
||||
public function __construct(string $value, bool $prohibited = false)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->value = $value;
|
||||
$this->prohibited = $prohibited;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user