Files
firefly-iii/app/Transformers/RuleTransformer.php

155 lines
5.2 KiB
PHP
Raw Normal View History

2018-06-24 06:51:22 +02:00
<?php
/**
* RuleTransformer.php
2020-02-16 13:57:18 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-06-24 06:51:22 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 06:51:22 +02:00
*
* 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.
2018-06-24 06:51:22 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 06:51:22 +02:00
* 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.
2018-06-24 06:51:22 +02:00
*
* 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/>.
2018-06-24 06:51:22 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Exceptions\FireflyException;
2018-06-24 06:51:22 +02:00
use FireflyIII\Models\Rule;
2018-12-07 15:36:04 +01:00
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger;
2018-12-20 20:50:05 +01:00
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
2018-06-24 06:51:22 +02:00
/**
* Class RuleTransformer
*/
class RuleTransformer extends AbstractTransformer
2018-06-24 06:51:22 +02:00
{
2018-12-20 20:50:05 +01:00
/** @var RuleRepositoryInterface */
private $ruleRepository;
2018-06-24 06:51:22 +02:00
/**
* CurrencyTransformer constructor.
*/
public function __construct()
2018-06-24 06:51:22 +02:00
{
2018-12-20 20:50:05 +01:00
$this->ruleRepository = app(RuleRepositoryInterface::class);
2018-06-24 06:51:22 +02:00
}
/**
* Transform the rule.
*
2019-08-25 07:40:14 +02:00
* @throws FireflyException
2018-06-24 06:51:22 +02:00
*/
public function transform(Rule $rule): array
{
2018-12-20 20:50:05 +01:00
$this->ruleRepository->setUser($rule->user);
2020-10-23 19:11:25 +02:00
return [
2022-12-29 19:42:40 +01:00
'id' => (string)$rule->id,
2022-03-29 15:00:29 +02:00
'created_at' => $rule->created_at->toAtomString(),
'updated_at' => $rule->updated_at->toAtomString(),
2022-12-29 19:42:40 +01:00
'rule_group_id' => (string)$rule->rule_group_id,
'rule_group_title' => (string)$rule->ruleGroup->title,
2022-03-29 15:00:29 +02:00
'title' => $rule->title,
'description' => $rule->description,
2023-11-05 19:55:39 +01:00
'order' => $rule->order,
2022-03-29 15:00:29 +02:00
'active' => $rule->active,
'strict' => $rule->strict,
'stop_processing' => $rule->stop_processing,
'trigger' => $this->getRuleTrigger($rule),
'triggers' => $this->triggers($rule),
'actions' => $this->actions($rule),
'links' => [
2018-06-24 06:51:22 +02:00
[
'rel' => 'self',
'uri' => '/rules/' . $rule->id,
2018-06-24 06:51:22 +02:00
],
],
];
}
2018-12-07 15:36:04 +01:00
2023-05-29 13:56:55 +02:00
/**
* @throws FireflyException
*/
2019-08-25 07:40:14 +02:00
private function getRuleTrigger(Rule $rule): string
{
$moment = null;
$triggers = $this->ruleRepository->getRuleTriggers($rule);
2023-12-20 19:35:52 +01:00
/** @var RuleTrigger $ruleTrigger */
foreach ($triggers as $ruleTrigger) {
if ('user_action' === $ruleTrigger->trigger_type) {
$moment = $ruleTrigger->trigger_value;
}
}
if (null === $moment) {
throw new FireflyException(sprintf('Rule #%d has no valid trigger moment. Edit it in the Firefly III user interface to correct this.', $rule->id));
}
return $moment;
}
2018-12-07 15:36:04 +01:00
private function triggers(Rule $rule): array
{
$result = [];
2018-12-20 20:50:05 +01:00
$triggers = $this->ruleRepository->getRuleTriggers($rule);
2023-12-20 19:35:52 +01:00
2018-12-07 15:36:04 +01:00
/** @var RuleTrigger $ruleTrigger */
foreach ($triggers as $ruleTrigger) {
if ('user_action' === $ruleTrigger->trigger_type) {
continue;
}
$triggerValue = (string)$ruleTrigger->trigger_value;
$needsContext = config(sprintf('search.operators.%s.needs_context', $ruleTrigger->trigger_type), true);
if (false === $needsContext) {
$triggerValue = 'true';
}
2018-12-07 15:36:04 +01:00
$result[] = [
2022-12-29 19:42:40 +01:00
'id' => (string)$ruleTrigger->id,
2018-12-07 15:36:04 +01:00
'created_at' => $ruleTrigger->created_at->toAtomString(),
2018-12-12 20:30:25 +01:00
'updated_at' => $ruleTrigger->updated_at->toAtomString(),
2018-12-07 15:36:04 +01:00
'type' => $ruleTrigger->trigger_type,
'value' => $triggerValue,
2018-12-07 15:36:04 +01:00
'order' => $ruleTrigger->order,
'active' => $ruleTrigger->active,
'stop_processing' => $ruleTrigger->stop_processing,
];
}
return $result;
}
2023-06-21 12:34:58 +02:00
private function actions(Rule $rule): array
{
$result = [];
$actions = $this->ruleRepository->getRuleActions($rule);
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var RuleAction $ruleAction */
foreach ($actions as $ruleAction) {
$result[] = [
'id' => (string)$ruleAction->id,
'created_at' => $ruleAction->created_at->toAtomString(),
'updated_at' => $ruleAction->updated_at->toAtomString(),
'type' => $ruleAction->action_type,
'value' => $ruleAction->action_value,
'order' => $ruleAction->order,
'active' => $ruleAction->active,
'stop_processing' => $ruleAction->stop_processing,
];
}
return $result;
}
}