This commit is contained in:
Sobuno
2025-01-01 04:17:55 +01:00
parent 74f76a2835
commit 4d25336d87
5 changed files with 341 additions and 61 deletions

View File

@@ -166,9 +166,15 @@ class OperatorQuerySearch implements SearchInterface
switch (true) {
case $node instanceof Word:
$allWords = (string) $node->getValue();
app('log')->debug(sprintf('Add words "%s" to search string', $allWords));
$this->words[] = $allWords;
$word = (string) $node->getValue();
if($node->isProhibited()) {
app('log')->debug(sprintf('Exclude word "%s" from search string', $word));
$this->prohibitedWords[] = $word;
} else {
app('log')->debug(sprintf('Add word "%s" to search string', $word));
$this->words[] = $word;
}
break;
case $node instanceof Field:
@@ -176,6 +182,7 @@ class OperatorQuerySearch implements SearchInterface
break;
case $node instanceof Subquery:
//TODO: Handle Subquery prohibition, i.e. flip all prohibition flags inside the subquery
foreach ($node->getNodes() as $subNode) {
$this->handleSearchNode($subNode);
}

View File

@@ -0,0 +1,136 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Support\Search;
/**
* Query parser class
*/
class QueryParser2 implements QueryParserInterface
{
private string $query;
private int $position = 0;
public function parse(string $query): array
{
$this->query = $query;
$this->position = 0;
return $this->parseQuery();
}
private function parseQuery(): array
{
$nodes = [];
$token = $this->buildNextNode();
while ($token !== null) {
$nodes[] = $token;
$token = $this->buildNextNode();
}
return $nodes;
}
private function buildNextNode(): ?Node
{
$tokenUnderConstruction = '';
$inQuotes = false;
$fieldName = '';
$prohibited = false;
while ($this->position < strlen($this->query)) {
$char = $this->query[$this->position];
// If we're in a quoted string, we treat all characters except another quote as ordinary characters
if ($inQuotes) {
if($char !== '"') {
$tokenUnderConstruction .= $char;
$this->position++;
continue;
} else {
$this->position++;
return $this->createNode($tokenUnderConstruction, $fieldName, $prohibited);
}
}
switch ($char) {
case '-':
if ($tokenUnderConstruction === '') {
// A minus sign at the beginning of a token indicates prohibition
$prohibited = true;
} else {
// In any other location, it's just a normal character
$tokenUnderConstruction .= $char;
}
break;
case '"':
if ($tokenUnderConstruction === '') {
// A quote sign at the beginning of a token indicates the start of a quoted string
$inQuotes = true;
} else {
// In any other location, it's just a normal character
$tokenUnderConstruction .= $char;
}
break;
case '(':
if ($tokenUnderConstruction === '') {
// A left parentheses at the beginning of a token indicates the start of a subquery
$this->position++;
return new Subquery($this->parseQuery(), $prohibited);
} else {
// In any other location, it's just a normal character
$tokenUnderConstruction .= $char;
}
break;
case ')':
if ($tokenUnderConstruction !== '') {
$this->position++;
return $this->createNode($tokenUnderConstruction, $fieldName, $prohibited);
}
$this->position++;
return null;
case ':':
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 = '';
} else {
// 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++;
return $this->createNode($tokenUnderConstruction, $fieldName, $prohibited);
}
break;
default:
$tokenUnderConstruction .= $char;
}
$this->position++;
}
return $fieldName !== '' || $tokenUnderConstruction !== ''
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
: null;
}
private function createNode(string $token, string $fieldName, bool $prohibited): Node
{
if (strlen($fieldName) > 0) {
return new Field(trim($fieldName), trim($token), $prohibited);
}
return new Word(trim($token), $prohibited);
}
}

View File

@@ -27,10 +27,12 @@ abstract class Node
class Word extends Node
{
private string $value;
private bool $prohibited;
public function __construct(string $value)
public function __construct(string $value, bool $prohibited = false)
{
$this->value = $value;
$this->prohibited = $prohibited;
}
public function getValue(): string
@@ -38,9 +40,14 @@ class Word extends Node
return $this->value;
}
public function isProhibited(): bool
{
return $this->prohibited;
}
public function __toString(): string
{
return $this->value;
return ($this->prohibited ? '-' : '') . $this->value;
}
}
@@ -89,12 +96,15 @@ class Subquery extends Node
/** @var Node[] */
private array $nodes;
private bool $prohibited;
/**
* @param Node[] $nodes
*/
public function __construct(array $nodes)
public function __construct(array $nodes, bool $prohibited = false)
{
$this->nodes = $nodes;
$this->prohibited = $prohibited;
}
/**
@@ -105,8 +115,13 @@ class Subquery extends Node
return $this->nodes;
}
public function isProhibited(): bool
{
return $this->prohibited;
}
public function __toString(): string
{
return '(' . implode(' ', array_map(fn($node) => (string)$node, $this->nodes)) . ')';
return ($this->prohibited ? '-' : '') . '(' . implode(' ', array_map(fn($node) => (string)$node, $this->nodes)) . ')';
}
}