mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Rearrange structure
This commit is contained in:
36
app/Support/Search/QueryParser/Field.php
Normal file
36
app/Support/Search/QueryParser/Field.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Search\QueryParser;
|
||||
|
||||
/**
|
||||
* Represents a field operator with value (e.g. amount:100)
|
||||
*/
|
||||
class Field extends Node
|
||||
{
|
||||
private string $operator;
|
||||
private string $value;
|
||||
|
||||
public function __construct(string $operator, string $value, bool $prohibited = false)
|
||||
{
|
||||
$this->operator = $operator;
|
||||
$this->value = $value;
|
||||
$this->prohibited = $prohibited;
|
||||
}
|
||||
|
||||
public function getOperator(): string
|
||||
{
|
||||
return $this->operator;
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return ($this->prohibited ? '-' : '') . $this->operator . ':' . $this->value;
|
||||
}
|
||||
}
|
80
app/Support/Search/QueryParser/GdbotsQueryParser.php
Normal file
80
app/Support/Search/QueryParser/GdbotsQueryParser.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Search\QueryParser;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use Gdbots\QueryParser\QueryParser as BaseQueryParser;
|
||||
use Gdbots\QueryParser\Node as GdbotsNode;
|
||||
use Gdbots\QueryParser\Enum\BoolOperator;
|
||||
|
||||
class GdbotsQueryParser implements QueryParserInterface
|
||||
{
|
||||
private BaseQueryParser $parser;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->parser = new BaseQueryParser();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Node[]
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function parse(string $query): array
|
||||
{
|
||||
try {
|
||||
$result = $this->parser->parse($query);
|
||||
return array_map(
|
||||
fn(GdbotsNode\Node $node) => $this->convertNode($node),
|
||||
$result->getNodes()
|
||||
);
|
||||
} catch (\LogicException|\TypeError $e) {
|
||||
fwrite(STDERR, "Setting up GdbotsQueryParserTest\n");
|
||||
dd('Creating GdbotsQueryParser');
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error(sprintf('Could not parse search: "%s".', $query));
|
||||
|
||||
throw new FireflyException(sprintf('Invalid search value "%s". See the logs.', e($query)), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
private function convertNode(GdbotsNode\Node $node): Node
|
||||
{
|
||||
switch (true) {
|
||||
case $node instanceof GdbotsNode\Word:
|
||||
return new Word($node->getValue());
|
||||
|
||||
case $node instanceof GdbotsNode\Field:
|
||||
return new Field(
|
||||
$node->getValue(),
|
||||
(string) $node->getNode()->getValue(),
|
||||
BoolOperator::PROHIBITED === $node->getBoolOperator()
|
||||
);
|
||||
|
||||
case $node instanceof GdbotsNode\Subquery:
|
||||
return new Subquery(
|
||||
array_map(
|
||||
fn(GdbotsNode\Node $subNode) => $this->convertNode($subNode),
|
||||
$node->getNodes()
|
||||
)
|
||||
);
|
||||
|
||||
case $node instanceof GdbotsNode\Phrase:
|
||||
case $node instanceof GdbotsNode\Numbr:
|
||||
case $node instanceof GdbotsNode\Date:
|
||||
case $node instanceof GdbotsNode\Url:
|
||||
case $node instanceof GdbotsNode\Hashtag:
|
||||
case $node instanceof GdbotsNode\Mention:
|
||||
case $node instanceof GdbotsNode\Emoticon:
|
||||
case $node instanceof GdbotsNode\Emoji:
|
||||
return new Word((string) $node->getValue());
|
||||
|
||||
default:
|
||||
throw new FireflyException(
|
||||
sprintf('Unsupported node type: %s', get_class($node))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
20
app/Support/Search/QueryParser/Node.php
Normal file
20
app/Support/Search/QueryParser/Node.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Search\QueryParser;
|
||||
|
||||
/**
|
||||
* Base class for all nodes
|
||||
*/
|
||||
abstract class Node
|
||||
{
|
||||
abstract public function __toString(): string;
|
||||
|
||||
protected bool $prohibited;
|
||||
|
||||
public function isProhibited(): bool
|
||||
{
|
||||
return $this->prohibited;
|
||||
}
|
||||
}
|
176
app/Support/Search/QueryParser/QueryParser.php
Normal file
176
app/Support/Search/QueryParser/QueryParser.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Search\QueryParser;
|
||||
|
||||
/**
|
||||
* Represents a result from parsing a query node
|
||||
*
|
||||
* Contains the parsed node and a flag indicating if this is the end of the query.
|
||||
* Used to handle subquery parsing and termination.
|
||||
*/
|
||||
class NodeResult
|
||||
{
|
||||
public function __construct(
|
||||
public readonly ?Node $node,
|
||||
public readonly bool $isSubqueryEnd
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Single-pass parser that processes query strings into structured nodes.
|
||||
* Scans each character once (O(n)) to build field searches, quoted strings,
|
||||
* prohibited terms and nested subqueries without backtracking.
|
||||
*/
|
||||
class QueryParser implements QueryParserInterface
|
||||
{
|
||||
private string $query;
|
||||
private int $position = 0;
|
||||
|
||||
/** @return Node[] */
|
||||
public function parse(string $query): array
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->position = 0;
|
||||
return $this->parseQuery(false);
|
||||
}
|
||||
|
||||
/** @return Node[] */
|
||||
private function parseQuery(bool $isSubquery): array
|
||||
{
|
||||
$nodes = [];
|
||||
$nodeResult = $this->buildNextNode($isSubquery);
|
||||
|
||||
while ($nodeResult->node !== null) {
|
||||
$nodes[] = $nodeResult->node;
|
||||
if($nodeResult->isSubqueryEnd) {
|
||||
break;
|
||||
}
|
||||
$nodeResult = $this->buildNextNode($isSubquery);
|
||||
}
|
||||
|
||||
return $nodes;
|
||||
}
|
||||
|
||||
private function buildNextNode(bool $isSubquery): NodeResult
|
||||
{
|
||||
$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 new NodeResult(
|
||||
$this->createNode($tokenUnderConstruction, $fieldName, $prohibited),
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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 NodeResult(
|
||||
new Subquery($this->parseQuery(true), $prohibited),
|
||||
false
|
||||
);
|
||||
} else {
|
||||
// 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++;
|
||||
return new NodeResult(
|
||||
$tokenUnderConstruction !== ''
|
||||
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
|
||||
: null,
|
||||
true
|
||||
);
|
||||
}
|
||||
// In any other location, it's just a normal character
|
||||
$tokenUnderConstruction .= $char;
|
||||
break;
|
||||
|
||||
|
||||
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 new NodeResult(
|
||||
$this->createNode($tokenUnderConstruction, $fieldName, $prohibited),
|
||||
false
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$tokenUnderConstruction .= $char;
|
||||
}
|
||||
|
||||
$this->position++;
|
||||
}
|
||||
|
||||
return new NodeResult($tokenUnderConstruction !== '' || $fieldName !== ''
|
||||
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
|
||||
: null, true);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
13
app/Support/Search/QueryParser/QueryParserInterface.php
Normal file
13
app/Support/Search/QueryParser/QueryParserInterface.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Search\QueryParser;
|
||||
|
||||
interface QueryParserInterface
|
||||
{
|
||||
/**
|
||||
* @return Node[]
|
||||
*/
|
||||
public function parse(string $query): array;
|
||||
}
|
38
app/Support/Search/QueryParser/Subquery.php
Normal file
38
app/Support/Search/QueryParser/Subquery.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Search\QueryParser;
|
||||
|
||||
/**
|
||||
* Represents a subquery (group of nodes)
|
||||
*/
|
||||
class Subquery extends Node
|
||||
{
|
||||
/** @var Node[] */
|
||||
private array $nodes;
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @param bool $prohibited
|
||||
*/
|
||||
public function __construct(array $nodes, bool $prohibited = false)
|
||||
{
|
||||
$this->nodes = $nodes;
|
||||
$this->prohibited = $prohibited;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Node[]
|
||||
*/
|
||||
public function getNodes(): array
|
||||
{
|
||||
return $this->nodes;
|
||||
}
|
||||
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return ($this->prohibited ? '-' : '') . '(' . implode(' ', array_map(fn($node) => (string)$node, $this->nodes)) . ')';
|
||||
}
|
||||
}
|
29
app/Support/Search/QueryParser/Word.php
Normal file
29
app/Support/Search/QueryParser/Word.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Search\QueryParser;
|
||||
|
||||
/**
|
||||
* Represents a word in the search query, meaning either a single-word without spaces or a quote-delimited string
|
||||
*/
|
||||
class Word extends Node
|
||||
{
|
||||
private string $value;
|
||||
|
||||
public function __construct(string $value, bool $prohibited = false)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->prohibited = $prohibited;
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return ($this->prohibited ? '-' : '') . $this->value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user