Files
firefly-iii/app/Api/V1/Requests/RuleUpdateRequest.php

208 lines
7.2 KiB
PHP
Raw Normal View History

2018-06-30 16:46:51 +02:00
<?php
/**
* RuleUpdateRequest.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);
namespace FireflyIII\Api\V1\Requests;
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;
use Illuminate\Validation\Validator;
use function is_array;
2018-06-30 16:46:51 +02:00
/**
* Class RuleUpdateRequest
2018-06-30 16:46:51 +02:00
*/
class RuleUpdateRequest extends FormRequest
2018-06-30 16:46:51 +02:00
{
2020-11-08 13:36:13 +01:00
use ConvertsDataTypes, GetRuleConfiguration, ChecksLogin;
2020-10-18 08:00:49 +02:00
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
{
$strict = null;
$active = null;
$stopProcessing = null;
2018-12-07 16:03:05 +01:00
if (null !== $this->get('active')) {
$active = $this->boolean('active');
}
if (null !== $this->get('strict')) {
$strict = $this->boolean('strict');
}
if (null !== $this->get('stop_processing')) {
$stopProcessing = $this->boolean('stop_processing');
}
2020-03-15 08:16:16 +01:00
return [
'title' => $this->nullableString('title'),
'description' => $this->nullableString('description'),
'rule_group_id' => $this->nullableInteger('rule_group_id'),
'rule_group_title' => $this->nullableString('rule_group_title'),
'trigger' => $this->nullableString('trigger'),
2018-12-07 16:03:05 +01:00
'strict' => $strict,
'stop_processing' => $stopProcessing,
'active' => $active,
'triggers' => $this->getRuleTriggers(),
'actions' => $this->getRuleActions(),
2018-06-30 16:46:51 +02:00
];
}
2020-10-18 08:00:49 +02:00
/**
* @return array|null
*/
private function getRuleTriggers(): ?array
{
if (!$this->has('triggers')) {
return null;
}
$triggers = $this->get('triggers');
$return = [];
if (is_array($triggers)) {
foreach ($triggers as $trigger) {
$return[] = [
'type' => $trigger['type'],
'value' => $trigger['value'],
'active' => $this->convertBoolean((string) ($trigger['active'] ?? 'false')),
'stop_processing' => $this->convertBoolean((string) ($trigger['stop_processing'] ?? 'false')),
];
}
}
return $return;
}
/**
* @return array|null
*/
private function getRuleActions(): ?array
{
if (!$this->has('actions')) {
return null;
}
$actions = $this->get('actions');
$return = [];
if (is_array($actions)) {
foreach ($actions as $action) {
$return[] = [
'type' => $action['type'],
'value' => $action['value'],
'active' => $this->convertBoolean((string) ($action['active'] ?? 'false')),
'stop_processing' => $this->convertBoolean((string) ($action['stop_processing'] ?? 'false')),
];
}
}
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'));
$rule = $this->route()->parameter('rule');
2018-06-30 16:46:51 +02:00
// 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 [
'title' => sprintf('between:1,100|uniqueObjectForUser:rules,title,%d', $rule->id),
2018-12-16 13:55:19 +01:00
'description' => 'between:1,5000|nullable',
'rule_group_id' => 'belongsToUser:rule_groups',
'rule_group_title' => 'nullable|between:1,255|belongsToUser:rule_groups,title',
'trigger' => '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
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
$this->atLeastOneTrigger($validator);
$this->atLeastOneAction($validator);
}
);
}
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
*/
protected function atLeastOneTrigger(Validator $validator): void
{
$data = $validator->getData();
$triggers = $data['triggers'] ?? null;
2019-09-04 17:39:39 +02:00
// need at least one trigger
if (is_array($triggers) && 0 === count($triggers)) {
$validator->errors()->add('title', (string) trans('validation.at_least_one_trigger'));
}
}
2018-06-30 16:46:51 +02:00
/**
2020-10-18 08:00:49 +02:00
* Adds an error to the validator when there are no repetitions in the array of data.
*
* @param Validator $validator
2019-06-09 08:26:23 +02:00
*/
2020-10-18 08:00:49 +02:00
protected function atLeastOneAction(Validator $validator): void
2019-06-09 08:26:23 +02:00
{
2020-10-18 08:00:49 +02:00
$data = $validator->getData();
$actions = $data['actions'] ?? null;
// need at least one action
if (is_array($actions) && 0 === count($actions)) {
$validator->errors()->add('title', (string) trans('validation.at_least_one_action'));
2019-06-09 08:26:23 +02:00
}
}
}