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

74 lines
2.6 KiB
PHP
Raw Normal View History

2020-11-29 18:35:49 +01:00
<?php
/*
* UpdateRequest.php
* Copyright (c) 2020 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/>.
*/
namespace FireflyIII\Api\V1\Requests\Webhook;
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
{
use ChecksLogin, ConvertsDataTypes;
/**
* @return array
*/
public function getData(): array
{
$triggers = array_flip(config('firefly.webhooks.triggers'));
$responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries'));
return [
'active' => $this->boolean('active'),
'trigger' => $triggers[$this->string('trigger')] ?? 0,
'response' => $responses[$this->string('response')] ?? 0,
'delivery' => $deliveries[$this->string('delivery')] ?? 0,
'url' => $this->string('url'),
];
}
/**
* Rules for this request.
*
* @return array
*/
public function rules(): array
{
$triggers = implode(',', array_values(config('firefly.webhooks.triggers')));
$responses = implode(',', array_values(config('firefly.webhooks.responses')));
$deliveries = implode(',', array_values(config('firefly.webhooks.deliveries')));
$webhook = $this->route()->parameter('webhook');
return [
'active' => [new IsBoolean],
'trigger' => sprintf('required|in:%s', $triggers),
'response' => sprintf('required|in:%s', $responses),
'delivery' => sprintf('required|in:%s', $deliveries),
'url' => ['required', 'url', 'starts_with:https://', sprintf('uniqueExistingWebhook:%d', $webhook->id)],
];
}
}