2024-03-06 17:50:16 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2024-03-07 13:00:57 -05:00
|
|
|
* ActionExpression.php
|
2024-03-06 17:50:16 -05:00
|
|
|
* Copyright (c) 2024 Michael Thomas
|
|
|
|
*
|
|
|
|
* 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 FireflyIII\TransactionRules\Expressions;
|
|
|
|
|
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
|
|
|
use Symfony\Component\ExpressionLanguage\SyntaxError;
|
|
|
|
|
2024-03-07 13:00:57 -05:00
|
|
|
class ActionExpression
|
2024-03-06 17:50:16 -05:00
|
|
|
{
|
|
|
|
private static array $NAMES = array("transaction");
|
|
|
|
|
2024-03-07 12:23:32 -05:00
|
|
|
private ExpressionLanguage $expressionLanguage;
|
2024-03-06 17:50:16 -05:00
|
|
|
private string $expr;
|
|
|
|
private bool $isExpression;
|
2024-03-07 12:23:32 -05:00
|
|
|
private ?SyntaxError $validationError;
|
2024-03-06 17:50:16 -05:00
|
|
|
|
2024-03-07 13:00:57 -05:00
|
|
|
public function __construct(string $expr)
|
2024-03-06 17:50:16 -05:00
|
|
|
{
|
2024-03-07 13:00:57 -05:00
|
|
|
$this->expressionLanguage = app(ExpressionLanguage::class);
|
2024-03-06 17:50:16 -05:00
|
|
|
$this->expr = $expr;
|
|
|
|
|
|
|
|
$this->isExpression = self::isExpression($expr);
|
2024-03-07 12:23:32 -05:00
|
|
|
$this->validationError = $this->validate();
|
2024-03-06 17:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function isExpression(string $expr): bool
|
|
|
|
{
|
|
|
|
return str_starts_with($expr, "=");
|
|
|
|
}
|
|
|
|
|
2024-03-07 12:23:32 -05:00
|
|
|
private function validate(): ?SyntaxError
|
2024-03-06 17:50:16 -05:00
|
|
|
{
|
|
|
|
if (!$this->isExpression) {
|
2024-03-07 12:23:32 -05:00
|
|
|
return null;
|
2024-03-06 17:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-03-07 12:23:32 -05:00
|
|
|
$this->lint();
|
|
|
|
return null;
|
2024-03-06 17:50:16 -05:00
|
|
|
} catch (SyntaxError $e) {
|
2024-03-07 12:23:32 -05:00
|
|
|
return $e;
|
2024-03-06 17:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function lintExpression(string $expr): void
|
|
|
|
{
|
|
|
|
$this->expressionLanguage->lint($expr, self::$NAMES);
|
|
|
|
}
|
|
|
|
|
2024-03-07 12:23:32 -05:00
|
|
|
private function lint(): void
|
2024-03-06 17:50:16 -05:00
|
|
|
{
|
|
|
|
if (!$this->isExpression) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->lintExpression(substr($this->expr, 1));
|
|
|
|
}
|
|
|
|
|
2024-03-07 12:23:32 -05:00
|
|
|
public function isValid(): bool
|
|
|
|
{
|
|
|
|
return $this->validationError === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValidationError()
|
|
|
|
{
|
|
|
|
return $this->validationError;
|
|
|
|
}
|
|
|
|
|
2024-03-06 17:50:16 -05:00
|
|
|
private function evaluateExpression(string $expr, array $journal): string
|
|
|
|
{
|
|
|
|
$result = $this->expressionLanguage->evaluate($expr, [
|
|
|
|
"transaction" => $journal
|
|
|
|
]);
|
|
|
|
return strval($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function evaluate(array $journal): string
|
|
|
|
{
|
|
|
|
if (!$this->isExpression) {
|
|
|
|
return $this->expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->evaluateExpression(substr($this->expr, 1), $journal);
|
|
|
|
}
|
|
|
|
}
|