2024-12-31 10:16:27 +01:00
|
|
|
<?php
|
|
|
|
|
2025-01-05 07:40:30 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GdbotsQueryParser.php
|
|
|
|
* Copyright (c) 2025 https://github.com/Sobuno
|
|
|
|
*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2024-12-31 10:16:27 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2025-01-02 22:17:56 +01:00
|
|
|
namespace FireflyIII\Support\Search\QueryParser;
|
2024-12-31 10:16:27 +01:00
|
|
|
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use Gdbots\QueryParser\QueryParser as BaseQueryParser;
|
|
|
|
use Gdbots\QueryParser\Node as GdbotsNode;
|
|
|
|
use Gdbots\QueryParser\Enum\BoolOperator;
|
2025-01-05 09:18:03 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-12-31 10:16:27 +01:00
|
|
|
|
|
|
|
class GdbotsQueryParser implements QueryParserInterface
|
|
|
|
{
|
|
|
|
private BaseQueryParser $parser;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->parser = new BaseQueryParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2025-01-02 23:19:21 +01:00
|
|
|
public function parse(string $query): NodeGroup
|
2024-12-31 10:16:27 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$result = $this->parser->parse($query);
|
2025-01-05 09:23:02 +01:00
|
|
|
$nodes = array_map(
|
|
|
|
fn (GdbotsNode\Node $node) => $this->convertNode($node),
|
2024-12-31 10:16:27 +01:00
|
|
|
$result->getNodes()
|
|
|
|
);
|
2025-01-05 09:23:02 +01:00
|
|
|
|
2025-01-02 23:19:21 +01:00
|
|
|
return new NodeGroup($nodes);
|
2024-12-31 10:16:27 +01:00
|
|
|
} catch (\LogicException|\TypeError $e) {
|
|
|
|
fwrite(STDERR, "Setting up GdbotsQueryParserTest\n");
|
|
|
|
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
|
|
|
|
{
|
2025-01-05 09:18:03 +01:00
|
|
|
|
2024-12-31 10:16:27 +01:00
|
|
|
switch (true) {
|
|
|
|
case $node instanceof GdbotsNode\Word:
|
2025-01-05 09:18:03 +01:00
|
|
|
return new StringNode($node->getValue(), BoolOperator::PROHIBITED === $node->getBoolOperator());
|
2024-12-31 10:16:27 +01:00
|
|
|
|
|
|
|
case $node instanceof GdbotsNode\Field:
|
2025-01-02 23:19:21 +01:00
|
|
|
return new FieldNode(
|
2024-12-31 10:16:27 +01:00
|
|
|
$node->getValue(),
|
|
|
|
(string) $node->getNode()->getValue(),
|
|
|
|
BoolOperator::PROHIBITED === $node->getBoolOperator()
|
|
|
|
);
|
|
|
|
|
|
|
|
case $node instanceof GdbotsNode\Subquery:
|
2025-01-05 09:18:03 +01:00
|
|
|
Log::debug('Subquery');
|
2025-01-05 09:23:02 +01:00
|
|
|
|
2025-01-02 23:19:21 +01:00
|
|
|
return new NodeGroup(
|
2024-12-31 10:16:27 +01:00
|
|
|
array_map(
|
2025-01-05 09:23:02 +01:00
|
|
|
fn (GdbotsNode\Node $subNode) => $this->convertNode($subNode),
|
2024-12-31 10:16:27 +01:00
|
|
|
$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:
|
2025-01-05 09:18:03 +01:00
|
|
|
return new StringNode((string) $node->getValue(), BoolOperator::PROHIBITED === $node->getBoolOperator());
|
2024-12-31 10:16:27 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new FireflyException(
|
|
|
|
sprintf('Unsupported node type: %s', get_class($node))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|