Files
firefly-iii/app/Api/V1/Requests/Models/Rule/StoreRequest.php

250 lines
8.9 KiB
PHP
Raw Normal View History

2018-06-30 16:46:51 +02:00
<?php
/**
* RuleStoreRequest.php
* Copyright (c) 2019 james@firefly-iii.org
2018-06-30 16:46:51 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-30 16:46:51 +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-30 16:46:51 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-30 16:46:51 +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-30 16:46:51 +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-30 16:46:51 +02:00
*/
declare(strict_types=1);
2021-03-06 17:43:06 +01:00
namespace FireflyIII\Api\V1\Requests\Models\Rule;
2018-06-30 16:46:51 +02:00
2018-12-07 16:03:05 +01:00
use FireflyIII\Rules\IsBoolean;
2020-11-08 13:36:13 +01:00
use FireflyIII\Support\Request\ChecksLogin;
2020-07-18 08:34:00 +02:00
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Support\Request\GetRuleConfiguration;
use Illuminate\Foundation\Http\FormRequest;
2018-06-30 16:46:51 +02:00
use Illuminate\Validation\Validator;
use function is_array;
2018-06-30 16:46:51 +02:00
/**
2021-03-06 17:43:06 +01:00
* Class StoreRequest
2018-06-30 16:46:51 +02:00
*/
2021-03-06 17:43:06 +01:00
class StoreRequest extends FormRequest
2018-06-30 16:46:51 +02:00
{
2020-11-08 13:36:13 +01:00
use ConvertsDataTypes, GetRuleConfiguration, ChecksLogin;
2018-06-30 16:46:51 +02:00
/**
* Get all data from the request.
*
2018-06-30 16:46:51 +02:00
* @return array
*/
public function getAll(): array
{
2021-03-14 16:08:49 +01:00
$fields = [
2022-05-04 05:53:47 +02:00
'title' => ['title', 'convertString'],
'description' => ['description', 'convertString'],
2021-03-14 16:08:49 +01:00
'rule_group_id' => ['rule_group_id', 'integer'],
'order' => ['order', 'integer'],
2022-05-04 05:53:47 +02:00
'rule_group_title' => ['rule_group_title', 'convertString'],
'trigger' => ['trigger', 'convertString'],
2021-03-14 16:08:49 +01:00
'strict' => ['strict', 'boolean'],
'stop_processing' => ['stop_processing', 'boolean'],
'active' => ['active', 'boolean'],
2018-06-30 16:46:51 +02:00
];
2021-03-14 16:08:49 +01:00
$data = $this->getAllData($fields);
$data['triggers'] = $this->getRuleTriggers();
$data['actions'] = $this->getRuleActions();
return $data;
2018-06-30 16:46:51 +02:00
}
2020-10-18 08:00:49 +02:00
/**
* @return array
*/
private function getRuleTriggers(): array
{
$triggers = $this->get('triggers');
$return = [];
if (is_array($triggers)) {
foreach ($triggers as $trigger) {
$return[] = [
'type' => $trigger['type'],
'value' => $trigger['value'],
2022-03-29 14:56:27 +02:00
'active' => $this->convertBoolean((string) ($trigger['active'] ?? 'false')),
'stop_processing' => $this->convertBoolean((string) ($trigger['stop_processing'] ?? 'false')),
2020-10-18 08:00:49 +02:00
];
}
}
return $return;
}
/**
* @return array
*/
private function getRuleActions(): array
{
$actions = $this->get('actions');
$return = [];
if (is_array($actions)) {
foreach ($actions as $action) {
$return[] = [
'type' => $action['type'],
'value' => $action['value'],
2022-03-29 14:56:27 +02:00
'active' => $this->convertBoolean((string) ($action['active'] ?? 'false')),
'stop_processing' => $this->convertBoolean((string) ($action['stop_processing'] ?? 'false')),
2020-10-18 08:00:49 +02:00
];
}
}
return $return;
}
2018-06-30 16:46:51 +02:00
/**
* The rules that the incoming request must be matched against.
*
2018-06-30 16:46:51 +02:00
* @return array
*/
public function rules(): array
{
$validTriggers = $this->getTriggers();
2018-06-30 16:46:51 +02:00
$validActions = array_keys(config('firefly.rule-actions'));
// some triggers and actions require text:
$contextTriggers = implode(',', $this->getTriggersWithContext());
$contextActions = implode(',', config('firefly.context-rule-actions'));
2020-03-15 08:16:16 +01:00
return [
2018-12-16 13:55:19 +01:00
'title' => 'required|between:1,100|uniqueObjectForUser:rules,title',
'description' => 'between:1,5000|nullable',
2021-03-14 16:08:49 +01:00
'rule_group_id' => 'belongsToUser:rule_groups|required_without:rule_group_title',
2018-12-16 13:55:19 +01:00
'rule_group_title' => 'nullable|between:1,255|required_without:rule_group_id|belongsToUser:rule_groups,title',
'trigger' => 'required|in:store-journal,update-journal',
'triggers.*.type' => 'required|in:' . implode(',', $validTriggers),
2018-12-09 20:54:11 +01:00
'triggers.*.value' => 'required_if:actions.*.type,' . $contextTriggers . '|min:1|ruleTriggerValue',
'triggers.*.stop_processing' => [new IsBoolean],
'triggers.*.active' => [new IsBoolean],
2018-12-16 13:55:19 +01:00
'actions.*.type' => 'required|in:' . implode(',', $validActions),
'actions.*.value' => 'required_if:actions.*.type,' . $contextActions . '|ruleActionValue',
'actions.*.stop_processing' => [new IsBoolean],
'actions.*.active' => [new IsBoolean],
'strict' => [new IsBoolean],
'stop_processing' => [new IsBoolean],
'active' => [new IsBoolean],
2018-06-30 16:46:51 +02:00
];
}
/**
* Configure the validator instance.
*
* @param Validator $validator
2018-06-30 16:46:51 +02:00
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
$this->atLeastOneTrigger($validator);
$this->atLeastOneAction($validator);
$this->atLeastOneActiveTrigger($validator);
$this->atLeastOneActiveAction($validator);
2018-06-30 16:46:51 +02:00
}
);
}
/**
* Adds an error to the validator when there are no triggers in the array of data.
*
* @param Validator $validator
*/
2019-09-04 17:39:39 +02:00
protected function atLeastOneTrigger(Validator $validator): void
{
2019-09-04 17:39:39 +02:00
$data = $validator->getData();
$triggers = $data['triggers'] ?? [];
// need at least one trigger
2021-06-12 19:32:34 +02:00
if (!is_countable($triggers) || empty($triggers)) {
2022-03-29 14:56:27 +02:00
$validator->errors()->add('title', (string) trans('validation.at_least_one_trigger'));
}
}
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneAction(Validator $validator): void
{
$data = $validator->getData();
$actions = $data['actions'] ?? [];
// need at least one trigger
if (!is_countable($actions) || empty($actions)) {
$validator->errors()->add('title', (string) trans('validation.at_least_one_action'));
}
}
2019-06-09 08:26:23 +02:00
/**
* Adds an error to the validator when there are no ACTIVE triggers in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneActiveTrigger(Validator $validator): void
{
$data = $validator->getData();
$triggers = $data['triggers'] ?? [];
// need at least one trigger
if (!is_countable($triggers) || empty($triggers)) {
return;
}
$allInactive = true;
$inactiveIndex = 0;
foreach ($triggers as $index => $trigger) {
$active = array_key_exists('active', $trigger) ? $trigger['active'] : true; // assume true
if (true === $active) {
$allInactive = false;
}
if (false === $active) {
$inactiveIndex = $index;
}
}
if (true === $allInactive) {
2022-03-29 14:56:27 +02:00
$validator->errors()->add(sprintf('triggers.%d.active', $inactiveIndex), (string) trans('validation.at_least_one_active_trigger'));
}
}
/**
* Adds an error to the validator when there are no ACTIVE actions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneActiveAction(Validator $validator): void
{
$data = $validator->getData();
$actions = $data['actions'] ?? [];
// need at least one trigger
if (!is_countable($actions) || empty($actions)) {
return;
}
$allInactive = true;
$inactiveIndex = 0;
foreach ($actions as $index => $action) {
$active = array_key_exists('active', $action) ? $action['active'] : true; // assume true
if (true === $active) {
$allInactive = false;
}
if (false === $active) {
$inactiveIndex = $index;
}
}
if (true === $allInactive) {
2022-03-29 14:56:27 +02:00
$validator->errors()->add(sprintf('actions.%d.active', $inactiveIndex), (string) trans('validation.at_least_one_active_action'));
2019-06-09 08:26:23 +02:00
}
}
}