|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* RolesPostRequest.php |
|
5
|
|
|
* Copyright (c) 2020 [email protected] |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of the Firefly III CSV importer |
|
8
|
|
|
* (https://github.com/firefly-iii/csv-importer). |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace App\Http\Request; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
use Illuminate\Validation\Validator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class RolesPostRequest |
|
31
|
|
|
*/ |
|
32
|
|
|
class RolesPostRequest extends Request |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Verify the request. |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function authorize(): bool |
|
41
|
|
|
{ |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getAll(): array |
|
49
|
|
|
{ |
|
50
|
|
|
$data = [ |
|
51
|
|
|
'roles' => $this->get('roles') ?? [], |
|
52
|
|
|
'do_mapping' => $this->get('do_mapping') ?? [], |
|
53
|
|
|
]; |
|
54
|
|
|
foreach (array_keys($data['roles']) as $index) { |
|
55
|
|
|
|
|
56
|
|
|
$data['do_mapping'][$index] = $this->convertBoolean($data['do_mapping'][$index] ?? 'false'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $data; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function rules(): array |
|
67
|
|
|
{ |
|
68
|
|
|
$keys = implode(',', array_keys(config('csv_importer.import_roles'))); |
|
69
|
|
|
|
|
70
|
|
|
return [ |
|
71
|
|
|
'roles.*' => sprintf('required|in:%s', $keys), |
|
72
|
|
|
'do_mapping.*' => 'numeric|between:0,1', |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Configure the validator instance with special rules for after the basic validation rules. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Validator $validator |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function withValidator(Validator $validator): void |
|
84
|
|
|
{ |
|
85
|
|
|
$validator->after( |
|
86
|
|
|
function (Validator $validator) { |
|
87
|
|
|
// validate all account info |
|
88
|
|
|
$this->validateAmountRole($validator); |
|
89
|
|
|
} |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param Validator $validator |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function validateAmountRole(Validator $validator): void |
|
97
|
|
|
{ |
|
98
|
|
|
$data = $validator->getData(); |
|
99
|
|
|
$roles = $data['roles'] ?? []; |
|
100
|
|
|
$count = 0; |
|
101
|
|
|
foreach ($roles as $role) { |
|
102
|
|
|
if (in_array($role, ['amount', 'amount_negated', 'amount_debit', 'amount_credit'], true)) { |
|
103
|
|
|
$count++; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
if (0 === $count) { |
|
107
|
|
|
$validator->errors()->add('roles.0', 'The import will fail if no column is assigned an "Amount"-role.'); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|