Files
firefly-iii/app/Support/Search/QueryParserInterface.php

128 lines
2.4 KiB
PHP
Raw Normal View History

2024-12-31 10:16:27 +01:00
<?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;
2025-01-01 04:17:55 +01:00
private bool $prohibited;
2024-12-31 10:16:27 +01:00
2025-01-01 04:17:55 +01:00
public function __construct(string $value, bool $prohibited = false)
2024-12-31 10:16:27 +01:00
{
$this->value = $value;
2025-01-01 04:17:55 +01:00
$this->prohibited = $prohibited;
2024-12-31 10:16:27 +01:00
}
public function getValue(): string
{
return $this->value;
}
2025-01-01 04:17:55 +01:00
public function isProhibited(): bool
{
return $this->prohibited;
}
2024-12-31 10:16:27 +01:00
public function __toString(): string
{
2025-01-01 04:17:55 +01:00
return ($this->prohibited ? '-' : '') . $this->value;
2024-12-31 10:16:27 +01:00
}
}
/**
* 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;
2025-01-01 04:17:55 +01:00
private bool $prohibited;
2024-12-31 10:16:27 +01:00
/**
* @param Node[] $nodes
*/
2025-01-01 04:17:55 +01:00
public function __construct(array $nodes, bool $prohibited = false)
2024-12-31 10:16:27 +01:00
{
$this->nodes = $nodes;
2025-01-01 04:17:55 +01:00
$this->prohibited = $prohibited;
2024-12-31 10:16:27 +01:00
}
/**
* @return Node[]
*/
public function getNodes(): array
{
return $this->nodes;
}
2025-01-01 04:17:55 +01:00
public function isProhibited(): bool
{
return $this->prohibited;
}
2024-12-31 10:16:27 +01:00
public function __toString(): string
{
2025-01-01 04:17:55 +01:00
return ($this->prohibited ? '-' : '') . '(' . implode(' ', array_map(fn($node) => (string)$node, $this->nodes)) . ')';
2024-12-31 10:16:27 +01:00
}
}