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

84 lines
3.0 KiB
PHP
Raw Normal View History

2020-11-29 18:35:49 +01:00
<?php
2021-01-29 18:50:35 +01:00
/*
* CreateRequest.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 CreateRequest
*/
class CreateRequest 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
{
2022-09-18 10:45:38 +02:00
$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.
2020-12-03 06:54:42 +01:00
$return = $this->getAllData($fields);
2023-12-20 19:35:52 +01:00
$return['trigger'] = $triggers[$return['trigger']] ?? (int)$return['trigger'];
$return['response'] = $responses[$return['response']] ?? (int)$return['response'];
$return['delivery'] = $deliveries[$return['delivery']] ?? (int)$return['delivery'];
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
{
2023-10-28 15:03:33 +02:00
$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
2020-11-29 18:35:49 +01:00
return [
2021-01-15 21:01:53 +01:00
'title' => 'required|between:1,512|uniqueObjectForUser:webhooks,title',
2022-10-30 14:23:00 +01:00
'active' => [new IsBoolean()],
2020-11-29 18:35:49 +01:00
'trigger' => sprintf('required|in:%s', $triggers),
'response' => sprintf('required|in:%s', $responses),
'delivery' => sprintf('required|in:%s', $deliveries),
'url' => ['required', sprintf('url:%s', $validProtocols), 'uniqueWebhook'],
2020-11-29 18:35:49 +01:00
];
}
2020-12-22 05:35:06 +01:00
}