mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 02:45:58 +00:00
WIP
This commit is contained in:
@@ -24,6 +24,8 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Providers;
|
namespace FireflyIII\Providers;
|
||||||
|
|
||||||
use FireflyIII\Support\Search\OperatorQuerySearch;
|
use FireflyIII\Support\Search\OperatorQuerySearch;
|
||||||
|
use FireflyIII\Support\Search\QueryParser;
|
||||||
|
use FireflyIII\Support\Search\QueryParserInterface;
|
||||||
use FireflyIII\Support\Search\SearchInterface;
|
use FireflyIII\Support\Search\SearchInterface;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
@@ -43,6 +45,15 @@ class SearchServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
|
$this->app->bind(
|
||||||
|
QueryParserInterface::class,
|
||||||
|
static function (Application $app) {
|
||||||
|
/** @var QueryParser $queryParser */
|
||||||
|
$queryParser = app(QueryParser::class);
|
||||||
|
return $queryParser;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
$this->app->bind(
|
$this->app->bind(
|
||||||
SearchInterface::class,
|
SearchInterface::class,
|
||||||
static function (Application $app) {
|
static function (Application $app) {
|
||||||
|
80
app/Support/Search/GdbotsQueryParser.php
Normal file
80
app/Support/Search/GdbotsQueryParser.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace FireflyIII\Support\Search;
|
||||||
|
|
||||||
|
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))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -41,20 +41,6 @@ use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
|||||||
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
|
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
|
||||||
use FireflyIII\Support\ParseDateString;
|
use FireflyIII\Support\ParseDateString;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Gdbots\QueryParser\Enum\BoolOperator;
|
|
||||||
use Gdbots\QueryParser\Node\Date;
|
|
||||||
use Gdbots\QueryParser\Node\Emoji;
|
|
||||||
use Gdbots\QueryParser\Node\Emoticon;
|
|
||||||
use Gdbots\QueryParser\Node\Field;
|
|
||||||
use Gdbots\QueryParser\Node\Hashtag;
|
|
||||||
use Gdbots\QueryParser\Node\Mention;
|
|
||||||
use Gdbots\QueryParser\Node\Node;
|
|
||||||
use Gdbots\QueryParser\Node\Numbr;
|
|
||||||
use Gdbots\QueryParser\Node\Phrase;
|
|
||||||
use Gdbots\QueryParser\Node\Subquery;
|
|
||||||
use Gdbots\QueryParser\Node\Url;
|
|
||||||
use Gdbots\QueryParser\Node\Word;
|
|
||||||
use Gdbots\QueryParser\QueryParser;
|
|
||||||
use Illuminate\Pagination\LengthAwarePaginator;
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
@@ -145,10 +131,11 @@ class OperatorQuerySearch implements SearchInterface
|
|||||||
public function parseQuery(string $query): void
|
public function parseQuery(string $query): void
|
||||||
{
|
{
|
||||||
app('log')->debug(sprintf('Now in parseQuery(%s)', $query));
|
app('log')->debug(sprintf('Now in parseQuery(%s)', $query));
|
||||||
$parser = new QueryParser();
|
$parser = app(QueryParserInterface::class);
|
||||||
|
app('log')->debug(sprintf('Using %s as implementation for QueryParserInterface', get_class($parser)));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$query1 = $parser->parse($query);
|
$nodes = $parser->parse($query);
|
||||||
} catch (\LogicException|\TypeError $e) {
|
} catch (\LogicException|\TypeError $e) {
|
||||||
app('log')->error($e->getMessage());
|
app('log')->error($e->getMessage());
|
||||||
app('log')->error(sprintf('Could not parse search: "%s".', $query));
|
app('log')->error(sprintf('Could not parse search: "%s".', $query));
|
||||||
@@ -156,9 +143,9 @@ class OperatorQuerySearch implements SearchInterface
|
|||||||
throw new FireflyException(sprintf('Invalid search value "%s". See the logs.', e($query)), 0, $e);
|
throw new FireflyException(sprintf('Invalid search value "%s". See the logs.', e($query)), 0, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
app('log')->debug(sprintf('Found %d node(s)', count($query1->getNodes())));
|
app('log')->debug(sprintf('Found %d node(s)', count($nodes)));
|
||||||
foreach ($query1->getNodes() as $searchNode) {
|
foreach ($nodes as $node) {
|
||||||
$this->handleSearchNode($searchNode);
|
$this->handleSearchNode($node);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add missing information
|
// add missing information
|
||||||
@@ -173,81 +160,71 @@ class OperatorQuerySearch implements SearchInterface
|
|||||||
*
|
*
|
||||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||||
*/
|
*/
|
||||||
private function handleSearchNode(Node $searchNode): void
|
private function handleSearchNode(Node $node): void
|
||||||
{
|
{
|
||||||
$class = get_class($searchNode);
|
app('log')->debug(sprintf('Now in handleSearchNode(%s)', get_class($node)));
|
||||||
app('log')->debug(sprintf('Now in handleSearchNode(%s)', $class));
|
|
||||||
|
|
||||||
switch ($class) {
|
switch (true) {
|
||||||
default:
|
case $node instanceof Word:
|
||||||
app('log')->error(sprintf('Cannot handle node %s', $class));
|
$allWords = (string) $node->getValue();
|
||||||
|
app('log')->debug(sprintf('Add words "%s" to search string', $allWords));
|
||||||
throw new FireflyException(sprintf('Firefly III search can\'t handle "%s"-nodes', $class));
|
|
||||||
|
|
||||||
case Subquery::class:
|
|
||||||
// loop all notes in subquery:
|
|
||||||
foreach ($searchNode->getNodes() as $subNode) { // @phpstan-ignore-line PHPStan thinks getNodes() does not exist but it does.
|
|
||||||
$this->handleSearchNode($subNode); // let's hope it's not too recursive
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Word::class:
|
|
||||||
case Phrase::class:
|
|
||||||
case Numbr::class:
|
|
||||||
case Url::class:
|
|
||||||
case Date::class:
|
|
||||||
case Hashtag::class:
|
|
||||||
case Emoticon::class:
|
|
||||||
case Emoji::class:
|
|
||||||
case Mention::class:
|
|
||||||
$allWords = (string) $searchNode->getValue();
|
|
||||||
app('log')->debug(sprintf('Add words "%s" to search string, because Node class is "%s"', $allWords, $class));
|
|
||||||
$this->words[] = $allWords;
|
$this->words[] = $allWords;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Field::class:
|
case $node instanceof Field:
|
||||||
app('log')->debug(sprintf('Now handle Node class %s', $class));
|
$this->handleFieldNode($node);
|
||||||
|
break;
|
||||||
|
|
||||||
/** @var Field $searchNode */
|
case $node instanceof Subquery:
|
||||||
// used to search for x:y
|
foreach ($node->getNodes() as $subNode) {
|
||||||
$operator = strtolower($searchNode->getValue());
|
$this->handleSearchNode($subNode);
|
||||||
$value = $searchNode->getNode()->getValue();
|
}
|
||||||
$prohibited = BoolOperator::PROHIBITED === $searchNode->getBoolOperator();
|
break;
|
||||||
$context = config(sprintf('search.operators.%s.needs_context', $operator));
|
|
||||||
|
|
||||||
// is an operator that needs no context, and value is false, then prohibited = true.
|
default:
|
||||||
if ('false' === $value && in_array($operator, $this->validOperators, true) && false === $context && !$prohibited) {
|
app('log')->error(sprintf('Cannot handle node %s', get_class($node)));
|
||||||
$prohibited = true;
|
throw new FireflyException(sprintf('Firefly III search can\'t handle "%s"-nodes', get_class($node)));
|
||||||
$value = 'true';
|
}
|
||||||
}
|
}
|
||||||
// if the operator is prohibited, but the value is false, do an uno reverse
|
|
||||||
if ('false' === $value && $prohibited && in_array($operator, $this->validOperators, true) && false === $context) {
|
|
||||||
$prohibited = false;
|
|
||||||
$value = 'true';
|
|
||||||
}
|
|
||||||
|
|
||||||
// must be valid operator:
|
/**
|
||||||
if (
|
* @throws FireflyException
|
||||||
in_array($operator, $this->validOperators, true)
|
*/
|
||||||
&& $this->updateCollector($operator, (string) $value, $prohibited)) {
|
private function handleFieldNode(Field $node): void
|
||||||
$this->operators->push(
|
{
|
||||||
[
|
$operator = strtolower($node->getOperator());
|
||||||
'type' => self::getRootOperator($operator),
|
$value = $node->getValue();
|
||||||
'value' => (string) $value,
|
$prohibited = $node->isProhibited();
|
||||||
'prohibited' => $prohibited,
|
|
||||||
]
|
$context = config(sprintf('search.operators.%s.needs_context', $operator));
|
||||||
);
|
|
||||||
app('log')->debug(sprintf('Added operator type "%s"', $operator));
|
// is an operator that needs no context, and value is false, then prohibited = true.
|
||||||
}
|
if ('false' === $value && in_array($operator, $this->validOperators, true) && false === $context && !$prohibited) {
|
||||||
if (!in_array($operator, $this->validOperators, true)) {
|
$prohibited = true;
|
||||||
app('log')->debug(sprintf('Added INVALID operator type "%s"', $operator));
|
$value = 'true';
|
||||||
$this->invalidOperators[] = [
|
}
|
||||||
'type' => $operator,
|
// if the operator is prohibited, but the value is false, do an uno reverse
|
||||||
'value' => (string) $value,
|
if ('false' === $value && $prohibited && in_array($operator, $this->validOperators, true) && false === $context) {
|
||||||
];
|
$prohibited = false;
|
||||||
}
|
$value = 'true';
|
||||||
|
}
|
||||||
|
|
||||||
|
// must be valid operator:
|
||||||
|
if (in_array($operator, $this->validOperators, true)) {
|
||||||
|
if ($this->updateCollector($operator, (string)$value, $prohibited)) {
|
||||||
|
$this->operators->push([
|
||||||
|
'type' => self::getRootOperator($operator),
|
||||||
|
'value' => (string)$value,
|
||||||
|
'prohibited' => $prohibited,
|
||||||
|
]);
|
||||||
|
app('log')->debug(sprintf('Added operator type "%s"', $operator));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
app('log')->debug(sprintf('Added INVALID operator type "%s"', $operator));
|
||||||
|
$this->invalidOperators[] = [
|
||||||
|
'type' => $operator,
|
||||||
|
'value' => (string)$value,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
171
app/Support/Search/QueryParser.php
Normal file
171
app/Support/Search/QueryParser.php
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace FireflyIII\Support\Search;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query parser class
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseQuery(): array
|
||||||
|
{
|
||||||
|
$nodes = [];
|
||||||
|
|
||||||
|
while ($this->position < strlen($this->query)) {
|
||||||
|
$this->skipWhitespace();
|
||||||
|
|
||||||
|
if ($this->position >= strlen($this->query)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle subquery
|
||||||
|
if ($this->query[$this->position] === '(') {
|
||||||
|
$nodes[] = $this->parseSubquery();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle field operator
|
||||||
|
if ($this->isStartOfField()) {
|
||||||
|
$nodes[] = $this->parseField();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle word
|
||||||
|
$nodes[] = $this->parseWord();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseSubquery(): Subquery
|
||||||
|
{
|
||||||
|
$this->position++; // Skip opening parenthesis
|
||||||
|
$nodes = [];
|
||||||
|
|
||||||
|
while ($this->position < strlen($this->query)) {
|
||||||
|
$this->skipWhitespace();
|
||||||
|
|
||||||
|
if ($this->query[$this->position] === ')') {
|
||||||
|
$this->position++; // Skip closing parenthesis
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->query[$this->position] === '(') {
|
||||||
|
$nodes[] = $this->parseSubquery();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->isStartOfField()) {
|
||||||
|
$nodes[] = $this->parseField();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$nodes[] = $this->parseWord();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Subquery($nodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseField(): Field
|
||||||
|
{
|
||||||
|
$prohibited = false;
|
||||||
|
if ($this->query[$this->position] === '-') {
|
||||||
|
$prohibited = true;
|
||||||
|
$this->position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$operator = '';
|
||||||
|
while ($this->position < strlen($this->query) && $this->query[$this->position] !== ':') {
|
||||||
|
$operator .= $this->query[$this->position];
|
||||||
|
$this->position++;
|
||||||
|
}
|
||||||
|
$this->position++; // Skip colon
|
||||||
|
|
||||||
|
$value = '';
|
||||||
|
$inQuotes = false;
|
||||||
|
while ($this->position < strlen($this->query)) {
|
||||||
|
$char = $this->query[$this->position];
|
||||||
|
|
||||||
|
if ($char === '"' && !$inQuotes) {
|
||||||
|
$inQuotes = true;
|
||||||
|
$this->position++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($char === '"' && $inQuotes) {
|
||||||
|
$inQuotes = false;
|
||||||
|
$this->position++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$inQuotes && ($char === ' ' || $char === ')')) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value .= $char;
|
||||||
|
$this->position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Field(trim($operator), trim($value), $prohibited);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseWord(): Word
|
||||||
|
{
|
||||||
|
$word = '';
|
||||||
|
while ($this->position < strlen($this->query)) {
|
||||||
|
$char = $this->query[$this->position];
|
||||||
|
if ($char === ' ' || $char === '(' || $char === ')') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$word .= $char;
|
||||||
|
$this->position++;
|
||||||
|
}
|
||||||
|
return new Word(trim($word));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isStartOfField(): bool
|
||||||
|
{
|
||||||
|
$pos = $this->position;
|
||||||
|
if ($this->query[$pos] === '-') {
|
||||||
|
$pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look ahead for a colon that's not inside quotes
|
||||||
|
$inQuotes = false;
|
||||||
|
while ($pos < strlen($this->query)) {
|
||||||
|
if ($this->query[$pos] === '"') {
|
||||||
|
$inQuotes = !$inQuotes;
|
||||||
|
}
|
||||||
|
if ($this->query[$pos] === ':' && !$inQuotes) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ($this->query[$pos] === ' ' && !$inQuotes) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$pos++;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function skipWhitespace(): void
|
||||||
|
{
|
||||||
|
while ($this->position < strlen($this->query) && $this->query[$this->position] === ' ') {
|
||||||
|
$this->position++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
112
app/Support/Search/QueryParserInterface.php
Normal file
112
app/Support/Search/QueryParserInterface.php
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace FireflyIII\Support\Search;
|
||||||
|
|
||||||
|
interface QueryParserInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return Node[]
|
||||||
|
*/
|
||||||
|
public function parse(string $query): array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for all nodes
|
||||||
|
*/
|
||||||
|
abstract class Node
|
||||||
|
{
|
||||||
|
abstract public function __toString(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a word in the search query
|
||||||
|
*/
|
||||||
|
class Word extends Node
|
||||||
|
{
|
||||||
|
private string $value;
|
||||||
|
|
||||||
|
public function __construct(string $value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValue(): string
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a field operator with value (e.g. amount:100)
|
||||||
|
*/
|
||||||
|
class Field extends Node
|
||||||
|
{
|
||||||
|
private string $operator;
|
||||||
|
private string $value;
|
||||||
|
private bool $prohibited;
|
||||||
|
|
||||||
|
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 isProhibited(): bool
|
||||||
|
{
|
||||||
|
return $this->prohibited;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return ($this->prohibited ? '-' : '') . $this->operator . ':' . $this->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a subquery (group of nodes)
|
||||||
|
*/
|
||||||
|
class Subquery extends Node
|
||||||
|
{
|
||||||
|
/** @var Node[] */
|
||||||
|
private array $nodes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Node[] $nodes
|
||||||
|
*/
|
||||||
|
public function __construct(array $nodes)
|
||||||
|
{
|
||||||
|
$this->nodes = $nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Node[]
|
||||||
|
*/
|
||||||
|
public function getNodes(): array
|
||||||
|
{
|
||||||
|
return $this->nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return '(' . implode(' ', array_map(fn($node) => (string)$node, $this->nodes)) . ')';
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,213 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* NavigationAddPeriodTest.php
|
||||||
|
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
|
||||||
|
*
|
||||||
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\unit\Support;
|
||||||
|
|
||||||
|
use FireflyIII\Support\Search\Field;
|
||||||
|
use FireflyIII\Support\Search\QueryParserInterface;
|
||||||
|
use FireflyIII\Support\Search\Word;
|
||||||
|
use FireflyIII\Support\Search\Subquery;
|
||||||
|
use Tests\integration\TestCase;
|
||||||
|
|
||||||
|
|
||||||
|
abstract class AbstractQueryParserInterfaceParseQueryTest extends TestCase
|
||||||
|
{
|
||||||
|
abstract protected function createParser(): QueryParserInterface;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct(string $name)
|
||||||
|
{
|
||||||
|
parent::__construct($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenEmptyStringWhenParsingQueryThenReturnsEmptyArray(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('');
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertEmpty($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenProhibitedFieldOperatorWhenParsingQueryThenReturnsFieldNode(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('-amount:100');
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertCount(1, $result);
|
||||||
|
$this->assertInstanceOf(Field::class, $result[0]);
|
||||||
|
$this->assertTrue($result[0]->isProhibited());
|
||||||
|
$this->assertEquals('amount', $result[0]->getOperator());
|
||||||
|
$this->assertEquals('100', $result[0]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public function testGivenNestedSubqueryWhenParsingQueryThenReturnsSubqueryNode(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('(amount:100 (description_contains:"test payment" -has_attachments:true))');
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertCount(1, $result);
|
||||||
|
$this->assertInstanceOf(Subquery::class, $result[0]);
|
||||||
|
|
||||||
|
$nodes = $result[0]->getNodes();
|
||||||
|
$this->assertCount(2, $nodes);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $nodes[0]);
|
||||||
|
$this->assertEquals('amount', $nodes[0]->getOperator());
|
||||||
|
$this->assertEquals('100', $nodes[0]->getValue());
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Subquery::class, $nodes[1]);
|
||||||
|
$subNodes = $nodes[1]->getNodes();
|
||||||
|
$this->assertCount(2, $subNodes);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $subNodes[0]);
|
||||||
|
$this->assertEquals('description_contains', $subNodes[0]->getOperator());
|
||||||
|
$this->assertEquals('test payment', $subNodes[0]->getValue());
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $subNodes[1]);
|
||||||
|
$this->assertTrue($subNodes[1]->isProhibited());
|
||||||
|
$this->assertEquals('has_attachments', $subNodes[1]->getOperator());
|
||||||
|
$this->assertEquals('true', $subNodes[1]->getValue());
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public function testGivenSimpleWordWhenParsingQueryThenReturnsWordNode(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('groceries');
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertCount(1, $result);
|
||||||
|
$this->assertInstanceOf(Word::class, $result[0]);
|
||||||
|
$this->assertEquals('groceries', $result[0]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenMultipleWordsWhenParsingQueryThenReturnsWordNodes(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('groceries shopping market');
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertCount(3, $result);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Word::class, $result[0]);
|
||||||
|
$this->assertEquals('groceries', $result[0]->getValue());
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Word::class, $result[1]);
|
||||||
|
$this->assertEquals('shopping', $result[1]->getValue());
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Word::class, $result[2]);
|
||||||
|
$this->assertEquals('market', $result[2]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenMixedWordsAndOperatorsWhenParsingQueryThenReturnsCorrectNodes(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('groceries amount:50 shopping');
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertCount(3, $result);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Word::class, $result[0]);
|
||||||
|
$this->assertEquals('groceries', $result[0]->getValue());
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $result[1]);
|
||||||
|
$this->assertEquals('amount', $result[1]->getOperator());
|
||||||
|
$this->assertEquals('50', $result[1]->getValue());
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Word::class, $result[2]);
|
||||||
|
$this->assertEquals('shopping', $result[2]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenQuotedValueWithSpacesWhenParsingQueryThenReturnsFieldNode(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('description_contains:"shopping at market"');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $result[0]);
|
||||||
|
$this->assertEquals('description_contains', $result[0]->getOperator());
|
||||||
|
$this->assertEquals('shopping at market', $result[0]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenDecimalNumberWhenParsingQueryThenReturnsFieldNode(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('amount:123.45');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $result[0]);
|
||||||
|
$this->assertEquals('amount', $result[0]->getOperator());
|
||||||
|
$this->assertEquals('123.45', $result[0]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenBooleanOperatorWhenParsingQueryThenReturnsFieldNode(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('has_any_category:true');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $result[0]);
|
||||||
|
$this->assertEquals('has_any_category', $result[0]->getOperator());
|
||||||
|
$this->assertEquals('true', $result[0]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public function testGivenIncompleteFieldOperatorWhenParsingQueryThenHandlesGracefully(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('amount:');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $result[0]);
|
||||||
|
$this->assertEquals('amount', $result[0]->getOperator());
|
||||||
|
$this->assertEquals('', $result[0]->getValue());
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public function testGivenUnterminatedQuoteWhenParsingQueryThenHandlesGracefully(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('description_contains:"unterminated');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Field::class, $result[0]);
|
||||||
|
$this->assertEquals('description_contains', $result[0]->getOperator());
|
||||||
|
$this->assertEquals('unterminated', $result[0]->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGivenWordFollowedBySubqueryWithoutSpaceWhenParsingQueryThenReturnsCorrectNodes(): void
|
||||||
|
{
|
||||||
|
$result = $this->createParser()->parse('groceries(amount:100 description_contains:"test")');
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertCount(2, $result);
|
||||||
|
|
||||||
|
// Test the word node
|
||||||
|
$this->assertInstanceOf(Word::class, $result[0]);
|
||||||
|
$this->assertEquals('groceries', $result[0]->getValue());
|
||||||
|
|
||||||
|
// Test the subquery node
|
||||||
|
$this->assertInstanceOf(Subquery::class, $result[1]);
|
||||||
|
$nodes = $result[1]->getNodes();
|
||||||
|
$this->assertCount(2, $nodes);
|
||||||
|
|
||||||
|
// Test first field in subquery
|
||||||
|
$this->assertInstanceOf(Field::class, $nodes[0]);
|
||||||
|
$this->assertEquals('amount', $nodes[0]->getOperator());
|
||||||
|
$this->assertEquals('100', $nodes[0]->getValue());
|
||||||
|
|
||||||
|
// Test second field in subquery
|
||||||
|
$this->assertInstanceOf(Field::class, $nodes[1]);
|
||||||
|
$this->assertEquals('description_contains', $nodes[1]->getOperator());
|
||||||
|
$this->assertEquals('test', $nodes[1]->getValue());
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\unit\Support;
|
||||||
|
|
||||||
|
use FireflyIII\Support\Search\GdbotsQueryParser;
|
||||||
|
use FireflyIII\Support\Search\QueryParserInterface;
|
||||||
|
use Tests\unit\Support\AbstractQueryParserInterfaceParseQueryTest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group unit-test
|
||||||
|
* @group support
|
||||||
|
* @group navigation
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
final class GdbotsQueryParserParseQueryTest extends AbstractQueryParserInterfaceParseQueryTest
|
||||||
|
{
|
||||||
|
protected function createParser(): QueryParserInterface
|
||||||
|
{
|
||||||
|
return new GdbotsQueryParser();
|
||||||
|
}
|
||||||
|
}
|
25
tests/unit/Support/Search/QueryParserParseQueryTest.php
Normal file
25
tests/unit/Support/Search/QueryParserParseQueryTest.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\unit\Support;
|
||||||
|
|
||||||
|
use FireflyIII\Support\Search\QueryParser;
|
||||||
|
use FireflyIII\Support\Search\QueryParserInterface;
|
||||||
|
use Tests\unit\Support\AbstractQueryParserInterfaceParseQueryTest;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group unit-test
|
||||||
|
* @group support
|
||||||
|
* @group navigation
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
final class QueryParserParseQueryTest extends AbstractQueryParserInterfaceParseQueryTest
|
||||||
|
{
|
||||||
|
protected function createParser(): QueryParserInterface
|
||||||
|
{
|
||||||
|
return new QueryParser();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user