1 | <?php |
||
2 | /** |
||
3 | * BudgetLimitRequest.php |
||
4 | * Copyright (c) 2018 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Api\V1\Requests; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Class BudgetLimitRequest |
||
29 | */ |
||
30 | class BudgetLimitRequest extends Request |
||
31 | { |
||
32 | /** |
||
33 | * Authorize logged in users. |
||
34 | * |
||
35 | * @return bool |
||
36 | */ |
||
37 | public function authorize(): bool |
||
38 | { |
||
39 | // Only allow authenticated users |
||
40 | return auth()->check(); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get all data from the request. |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public function getAll(): array |
||
49 | { |
||
50 | return [ |
||
51 | 'budget_id' => $this->integer('budget_id'), |
||
52 | 'start' => $this->date('start'), |
||
53 | 'end' => $this->date('end'), |
||
54 | 'amount' => $this->string('amount'), |
||
55 | 'currency_id' => $this->integer('currency_id'), |
||
56 | 'currency_code' => $this->string('currency_code'), |
||
57 | ]; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * The rules that the incoming request must be matched against. |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | public function rules(): array |
||
66 | { |
||
67 | $rules = [ |
||
68 | 'budget_id' => 'required|exists:budgets,id|belongsToUser:budgets,id', |
||
69 | 'start' => 'required|before:end|date', |
||
70 | 'end' => 'required|after:start|date', |
||
71 | 'amount' => 'required|more:0', |
||
72 | 'currency_id' => 'numeric|exists:transaction_currencies,id', |
||
73 | 'currency_code' => 'min:3|max:3|exists:transaction_currencies,code', |
||
74 | ]; |
||
75 | switch ($this->method()) { |
||
76 | default: |
||
77 | break; |
||
78 | case 'PUT': |
||
79 | case 'PATCH': |
||
80 | $rules['budget_id'] = 'required|exists:budgets,id|belongsToUser:budgets,id'; |
||
81 | break; |
||
82 | } |
||
83 | // if request has a budget already, drop the rule. |
||
84 | $budget = $this->route()->parameter('budget'); |
||
85 | if (null !== $budget) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
86 | unset($rules['budget_id']); |
||
87 | } |
||
88 | |||
89 | |||
90 | return $rules; |
||
91 | } |
||
92 | |||
93 | } |
||
94 |