Skip to content

Firefly III: a personal finances manager

Merge pull request #9598 from Sobuno/NewQueryParserV3

James Cole0810f617c7dd
diff --cc app/Support/Search/OperatorQuerySearch.php
index 53fce8cef9,915e8ad26d..8dfda1e1b4
--- a/app/Support/Search/OperatorQuerySearch.php
+++ b/app/Support/Search/OperatorQuerySearch.php
@@@ -171,83 -172,95 +172,95 @@@ class OperatorQuerySearch implements Se
/**
* @throws FireflyException
*
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
+ * @SuppressWarnings("PHPMD.CyclomaticComplexity")
*/
- private function handleSearchNode(Node $searchNode): void
+ private function handleSearchNode(Node $node, $flipProhibitedFlag): void
{
- $class = get_class($searchNode);
- app('log')->debug(sprintf('Now in handleSearchNode(%s)', $class));
+ app('log')->debug(sprintf('Now in handleSearchNode(%s)', get_class($node)));
+
+ switch (true) {
+ case $node instanceof StringNode:
+ $this->handleStringNode($node, $flipProhibitedFlag);
+ break;
+
+ case $node instanceof FieldNode:
+ $this->handleFieldNode($node, $flipProhibitedFlag);
+ break;
+
+ case $node instanceof NodeGroup:
+ $this->handleNodeGroup($node, $flipProhibitedFlag);
+ break;
- switch ($class) {
default:
- app('log')->error(sprintf('Cannot handle node %s', $class));
-
- 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;
-
- break;
-
- case Field::class:
- app('log')->debug(sprintf('Now handle Node class %s', $class));
-
- /** @var Field $searchNode */
- // used to search for x:y
- $operator = strtolower($searchNode->getValue());
- $value = $searchNode->getNode()->getValue();
- $prohibited = BoolOperator::PROHIBITED === $searchNode->getBoolOperator();
- $context = config(sprintf('search.operators.%s.needs_context', $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) {
- $prohibited = true;
- $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 (
- in_array($operator, $this->validOperators, true)
- && $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));
- }
- if (!in_array($operator, $this->validOperators, true)) {
- app('log')->debug(sprintf('Added INVALID operator type "%s"', $operator));
- $this->invalidOperators[] = [
- 'type' => $operator,
- 'value' => (string) $value,
- ];
- }
+ app('log')->error(sprintf('Cannot handle node %s', get_class($node)));
+ throw new FireflyException(sprintf('Firefly III search can\'t handle "%s"-nodes', get_class($node)));
+ }
+ }
+
+ private function handleNodeGroup(NodeGroup $node, $flipProhibitedFlag): void
+ {
+ $prohibited = $node->isProhibited($flipProhibitedFlag);
+
+ foreach ($node->getNodes() as $subNode) {
+ $this->handleSearchNode($subNode, $prohibited);
+ }
+ }
+
+
+
+ private function handleStringNode(StringNode $node, $flipProhibitedFlag): void
+ {
+ $string = (string) $node->getValue();
+
+ $prohibited = $node->isProhibited($flipProhibitedFlag);
+
+ if($prohibited) {
+ app('log')->debug(sprintf('Exclude string "%s" from search string', $string));
+ $this->prohibitedWords[] = $string;
+ } else {
+ app('log')->debug(sprintf('Add string "%s" to search string', $string));
+ $this->words[] = $string;
+ }
+ }
+
+ /**
+ * @throws FireflyException
+ */
+ private function handleFieldNode(FieldNode $node, $flipProhibitedFlag): void
+ {
+ $operator = strtolower($node->getOperator());
+ $value = $node->getValue();
+ $prohibited = $node->isProhibited($flipProhibitedFlag);
+
+ $context = config(sprintf('search.operators.%s.needs_context', $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) {
+ $prohibited = true;
+ $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 (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,
+ ];
}
}