1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Request.php |
4
|
|
|
* Copyright (c) 2017 [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
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Http\Requests; |
24
|
|
|
|
25
|
|
|
use Carbon\Carbon; |
26
|
|
|
use Carbon\Exceptions\InvalidDateException; |
27
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
28
|
|
|
use Log; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class Request. |
32
|
|
|
* |
33
|
|
|
* @codeCoverageIgnore |
34
|
|
|
* |
35
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
36
|
|
|
*/ |
37
|
|
|
class Request extends FormRequest |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Return a boolean value. |
41
|
|
|
* |
42
|
|
|
* @param string $field |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function boolean(string $field): bool |
47
|
|
|
{ |
48
|
|
|
if ('true' === (string)$this->input($field)) { |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
if ('false' === (string)$this->input($field)) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return 1 === (int)$this->input($field); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $value |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function convertBoolean(string $value): bool |
64
|
|
|
{ |
65
|
|
|
if ('true' === $value) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
if (1 === $value) { |
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
if ('1' === $value) { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
if (true === $value) { |
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return floating value. |
83
|
|
|
* |
84
|
|
|
* @param string $field |
85
|
|
|
* |
86
|
|
|
* @return float|null |
87
|
|
|
*/ |
88
|
|
|
public function float(string $field): ?float |
89
|
|
|
{ |
90
|
|
|
$res = $this->get($field); |
91
|
|
|
if (null === $res) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return (float)$res; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return integer value. |
100
|
|
|
* |
101
|
|
|
* @param string $field |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function integer(string $field): int |
106
|
|
|
{ |
107
|
|
|
return (int)$this->get($field); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return string value. |
112
|
|
|
* |
113
|
|
|
* @param string $field |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
* |
117
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
118
|
|
|
*/ |
119
|
|
|
public function string(string $field): string |
120
|
|
|
{ |
121
|
|
|
return app('steam')->cleanString((string)($this->get($field) ?? '')); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return date or NULL. |
126
|
|
|
* |
127
|
|
|
* @param string $field |
128
|
|
|
* |
129
|
|
|
* @return Carbon|null |
130
|
|
|
*/ |
131
|
|
|
protected function date(string $field): ?Carbon |
132
|
|
|
{ |
133
|
|
|
return $this->get($field) ? new Carbon($this->get($field)) : null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return date time or NULL. |
138
|
|
|
* |
139
|
|
|
* @param string $field |
140
|
|
|
* |
141
|
|
|
* @return Carbon|null |
142
|
|
|
*/ |
143
|
|
|
protected function dateTime(string $field): ?Carbon |
144
|
|
|
{ |
145
|
|
|
if (null === $this->get($field)) { |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
|
|
$value = (string)$this->get($field); |
149
|
|
|
if (10 === \strlen($value)) { |
150
|
|
|
// probably a date format. |
151
|
|
|
try { |
152
|
|
|
$result = Carbon::createFromFormat('Y-m-d', $value); |
153
|
|
|
} catch (InvalidDateException $e) { |
154
|
|
|
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage())); |
155
|
|
|
|
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $result; |
160
|
|
|
} |
161
|
|
|
// is an atom string, I hope? |
162
|
|
|
try { |
163
|
|
|
$result = Carbon::parse($value); |
164
|
|
|
} catch (InvalidDateException $e) { |
165
|
|
|
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage())); |
166
|
|
|
|
167
|
|
|
return null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $result; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|