Files
firefly-iii/app/Api/V1/Requests/Models/Webhook/UpdateRequest.php

98 lines
3.4 KiB
PHP
Raw Normal View History

2020-11-29 18:35:49 +01:00
<?php
2021-01-29 18:50:35 +01:00
/*
* UpdateRequest.php
* Copyright (c) 2021 james@firefly-iii.org
*
* 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/>.
*/
2020-12-22 05:35:06 +01:00
declare(strict_types=1);
2020-11-29 18:35:49 +01:00
2021-03-07 12:13:22 +01:00
namespace FireflyIII\Api\V1\Requests\Models\Webhook;
2020-11-29 18:35:49 +01:00
2022-09-17 07:07:25 +02:00
use FireflyIII\Models\Webhook;
2020-11-29 18:35:49 +01:00
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class UpdateRequest
*/
class UpdateRequest extends FormRequest
{
2022-10-30 14:23:00 +01:00
use ChecksLogin;
use ConvertsDataTypes;
2020-11-29 18:35:49 +01:00
public function getData(): array
{
$triggers = Webhook::getTriggersForValidation();
$responses = Webhook::getResponsesForValidation();
$deliveries = Webhook::getDeliveriesForValidation();
2020-11-29 18:35:49 +01:00
$fields = [
2022-05-04 05:53:47 +02:00
'title' => ['title', 'convertString'],
'active' => ['active', 'boolean'],
2022-05-04 05:53:47 +02:00
'trigger' => ['trigger', 'convertString'],
'response' => ['response', 'convertString'],
'delivery' => ['delivery', 'convertString'],
'url' => ['url', 'convertString'],
];
// this is the way.
$return = $this->getAllData($fields);
2021-03-08 09:56:40 +01:00
if (array_key_exists('trigger', $return)) {
$return['trigger'] = $triggers[$return['trigger']] ?? 0;
}
if (array_key_exists('response', $return)) {
$return['response'] = $responses[$return['response']] ?? 0;
}
if (array_key_exists('delivery', $return)) {
$return['delivery'] = $deliveries[$return['delivery']] ?? 0;
}
$return['secret'] = null !== $this->get('secret');
if (null !== $this->get('title')) {
2022-05-02 19:35:35 +02:00
$return['title'] = $this->convertString('title');
2021-03-08 09:56:40 +01:00
}
2020-12-03 06:54:42 +01:00
return $return;
2020-11-29 18:35:49 +01:00
}
/**
* Rules for this request.
*/
public function rules(): array
{
$triggers = implode(',', array_keys(Webhook::getTriggersForValidation()));
$responses = implode(',', array_keys(Webhook::getResponsesForValidation()));
$deliveries = implode(',', array_keys(Webhook::getDeliveriesForValidation()));
$validProtocols = config('firefly.valid_url_protocols');
2023-12-20 19:35:52 +01:00
2022-10-31 05:39:22 +01:00
/** @var Webhook $webhook */
$webhook = $this->route()->parameter('webhook');
2020-11-29 18:35:49 +01:00
return [
2024-01-05 09:48:59 +01:00
'title' => sprintf('min:1|max:255|uniqueObjectForUser:webhooks,title,%d', $webhook->id),
2022-10-30 14:23:00 +01:00
'active' => [new IsBoolean()],
2021-03-08 09:56:40 +01:00
'trigger' => sprintf('in:%s', $triggers),
'response' => sprintf('in:%s', $responses),
'delivery' => sprintf('in:%s', $deliveries),
'url' => [sprintf('url:%s', $validProtocols), sprintf('uniqueExistingWebhook:%d', $webhook->id)],
2020-11-29 18:35:49 +01:00
];
}
2020-12-22 05:35:06 +01:00
}