firefly-iii/app/Http/Requests/JournalLinkRequest.php

83 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* JournalLinkRequest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Requests;
2017-08-24 23:58:28 -05:00
use FireflyIII\Models\LinkType;
/**
2017-11-15 05:25:49 -06:00
* Class JournalLink.
*/
class JournalLinkRequest extends Request
{
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
return auth()->check();
}
2017-08-24 23:58:28 -05:00
/**
* @return array
*/
public function getLinkInfo(): array
{
$return = [];
$linkType = $this->get('link_type');
$parts = explode('_', $linkType);
$return['link_type_id'] = intval($parts[0]);
$return['transaction_journal_id'] = $this->integer('link_journal_id');
2018-01-17 03:40:44 -06:00
$return['notes'] = strlen($this->string('notes')) > 0 ? $this->string('notes') : '';
2017-08-24 23:58:28 -05:00
$return['direction'] = $parts[1];
2017-11-15 05:25:49 -06:00
if (0 === $return['transaction_journal_id'] && ctype_digit($this->string('link_other'))) {
2017-08-24 23:58:28 -05:00
$return['transaction_journal_id'] = $this->integer('link_other');
}
return $return;
}
/**
* @return array
*/
public function rules()
{
2017-08-24 23:58:28 -05:00
// all possible combinations of link types and inward / outward:
$combinations = [];
$linkTypes = LinkType::get(['id']);
/** @var LinkType $type */
foreach ($linkTypes as $type) {
$combinations[] = sprintf('%d_inward', $type->id);
$combinations[] = sprintf('%d_outward', $type->id);
}
$string = join(',', $combinations);
2017-09-14 10:40:02 -05:00
2017-09-12 14:44:31 -05:00
// fixed
2017-08-24 23:58:28 -05:00
return [
'link_type' => sprintf('required|in:%s', $string),
'link_other' => 'belongsToUser:transaction_journals',
'link_journal_id' => 'belongsToUser:transaction_journals',
];
}
}