|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Request.php |
|
4
|
|
|
* Copyright (c) 2020 [email protected]. |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of the Firefly III bunq importer |
|
7
|
|
|
* (https://github.com/firefly-iii/bunq-importer). |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
declare(strict_types=1); |
|
24
|
|
|
|
|
25
|
|
|
namespace App\Http\Middleware; |
|
26
|
|
|
|
|
27
|
|
|
use Carbon\Carbon; |
|
28
|
|
|
use Carbon\Exceptions\InvalidDateException; |
|
29
|
|
|
use Exception; |
|
30
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class Request. |
|
34
|
|
|
* |
|
35
|
|
|
* @codeCoverageIgnore |
|
36
|
|
|
*/ |
|
37
|
|
|
class Request extends FormRequest |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @param $array |
|
41
|
|
|
* |
|
42
|
|
|
* @return array|null |
|
43
|
|
|
*/ |
|
44
|
|
|
public function arrayFromValue($array): ?array |
|
45
|
|
|
{ |
|
46
|
|
|
if (is_array($array)) { |
|
47
|
|
|
return $array; |
|
48
|
|
|
} |
|
49
|
|
|
if (null === $array) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
if (is_string($array)) { |
|
53
|
|
|
return explode(',', $array); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string|int|null|bool $value |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function convertBoolean($value): bool |
|
65
|
|
|
{ |
|
66
|
|
|
if (null === $value) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
if ('true' === $value) { |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
if ('yes' === $value) { |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
if (1 === $value) { |
|
76
|
|
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
if ('1' === $value) { |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
if (true === $value) { |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string|null $string |
|
90
|
|
|
* |
|
91
|
|
|
* @return Carbon|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function dateFromValue(?string $string): ?Carbon |
|
94
|
|
|
{ |
|
95
|
|
|
if (null === $string) { |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
if ('' === $string) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
try { |
|
102
|
|
|
$carbon = new Carbon($string); |
|
103
|
|
|
} catch (Exception $e) { |
|
104
|
|
|
app('log')->debug(sprintf('Invalid date: %s: %s', $string, $e->getMessage())); |
|
105
|
|
|
|
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $carbon; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return floating value. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $field |
|
116
|
|
|
* |
|
117
|
|
|
* @return float|null |
|
118
|
|
|
*/ |
|
119
|
|
|
public function float(string $field): ?float |
|
120
|
|
|
{ |
|
121
|
|
|
$res = $this->get($field); |
|
122
|
|
|
if (null === $res) { |
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return (float) $res; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Return integer value. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $field |
|
133
|
|
|
* |
|
134
|
|
|
* @return int |
|
135
|
|
|
*/ |
|
136
|
|
|
public function integer(string $field): int |
|
137
|
|
|
{ |
|
138
|
|
|
return (int) $this->get($field); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Parse to integer. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string|null $string |
|
145
|
|
|
* |
|
146
|
|
|
* @return int|null |
|
147
|
|
|
*/ |
|
148
|
|
|
public function integerFromValue(?string $string): ?int |
|
149
|
|
|
{ |
|
150
|
|
|
if (null === $string) { |
|
151
|
|
|
return null; |
|
152
|
|
|
} |
|
153
|
|
|
if ('' === $string) { |
|
154
|
|
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return (int) $string; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Return integer value, or NULL when it's not set. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $field |
|
164
|
|
|
* |
|
165
|
|
|
* @return int|null |
|
166
|
|
|
*/ |
|
167
|
|
|
public function nullableInteger(string $field): ?int |
|
168
|
|
|
{ |
|
169
|
|
|
if (!$this->has($field)) { |
|
170
|
|
|
return null; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$value = (string) $this->get($field); |
|
174
|
|
|
if ('' === $value) { |
|
175
|
|
|
return null; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return (int) $value; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Return string value, or NULL if empty. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $field |
|
185
|
|
|
* |
|
186
|
|
|
* @return string|null |
|
187
|
|
|
*/ |
|
188
|
|
|
public function nullableString(string $field): ?string |
|
189
|
|
|
{ |
|
190
|
|
|
if (!$this->has($field)) { |
|
191
|
|
|
return null; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $this->cleanString((string) ($this->get($field) ?? '')); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Return string value. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $field |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
public function string(string $field): string |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->cleanString((string) ($this->get($field) ?? '')); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Parse and clean a string. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string|null $string |
|
213
|
|
|
* |
|
214
|
|
|
* @return string|null |
|
215
|
|
|
*/ |
|
216
|
|
|
public function stringFromValue(?string $string): ?string |
|
217
|
|
|
{ |
|
218
|
|
|
if (null === $string) { |
|
219
|
|
|
return null; |
|
220
|
|
|
} |
|
221
|
|
|
$result = $this->cleanString($string); |
|
222
|
|
|
|
|
223
|
|
|
return '' === $result ? null : $result; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Return date or NULL. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $field |
|
230
|
|
|
* |
|
231
|
|
|
* @return Carbon|null |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function date(string $field): ?Carbon |
|
234
|
|
|
{ |
|
235
|
|
|
$result = null; |
|
|
|
|
|
|
236
|
|
|
try { |
|
237
|
|
|
$result = $this->get($field) ? new Carbon($this->get($field)) : null; |
|
238
|
|
|
} catch (Exception $e) { |
|
239
|
|
|
app('log')->debug(sprintf('Exception when parsing date. Not interesting: %s', $e->getMessage())); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return $result; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Return date time or NULL. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $field |
|
249
|
|
|
* |
|
250
|
|
|
* @return Carbon|null |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function dateTime(string $field): ?Carbon |
|
253
|
|
|
{ |
|
254
|
|
|
if (null === $this->get($field)) { |
|
255
|
|
|
return null; |
|
256
|
|
|
} |
|
257
|
|
|
$value = (string) $this->get($field); |
|
258
|
|
|
if (10 === strlen($value)) { |
|
259
|
|
|
// probably a date format. |
|
260
|
|
|
try { |
|
261
|
|
|
$result = Carbon::createFromFormat('Y-m-d', $value); |
|
262
|
|
|
} catch (InvalidDateException $e) { |
|
263
|
|
|
app('log')->error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage())); |
|
264
|
|
|
|
|
265
|
|
|
return null; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $result; |
|
269
|
|
|
} |
|
270
|
|
|
// is an atom string, I hope? |
|
271
|
|
|
try { |
|
272
|
|
|
$result = Carbon::parse($value); |
|
273
|
|
|
} catch (InvalidDateException $e) { |
|
274
|
|
|
app('log')->error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage())); |
|
275
|
|
|
|
|
276
|
|
|
return null; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return $result; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Remove weird chars from strings. |
|
284
|
|
|
* |
|
285
|
|
|
* @param string $string |
|
286
|
|
|
* |
|
287
|
|
|
* @return string |
|
288
|
|
|
*/ |
|
289
|
|
|
private function cleanString(string $string): string |
|
290
|
|
|
{ |
|
291
|
|
|
$search = [ |
|
292
|
|
|
"\u{0001}", // start of heading |
|
293
|
|
|
"\u{0002}", // start of text |
|
294
|
|
|
"\u{0003}", // end of text |
|
295
|
|
|
"\u{0004}", // end of transmission |
|
296
|
|
|
"\u{0005}", // enquiry |
|
297
|
|
|
"\u{0006}", // ACK |
|
298
|
|
|
"\u{0007}", // BEL |
|
299
|
|
|
"\u{0008}", // backspace |
|
300
|
|
|
"\u{000E}", // shift out |
|
301
|
|
|
"\u{000F}", // shift in |
|
302
|
|
|
"\u{0010}", // data link escape |
|
303
|
|
|
"\u{0011}", // DC1 |
|
304
|
|
|
"\u{0012}", // DC2 |
|
305
|
|
|
"\u{0013}", // DC3 |
|
306
|
|
|
"\u{0014}", // DC4 |
|
307
|
|
|
"\u{0015}", // NAK |
|
308
|
|
|
"\u{0016}", // SYN |
|
309
|
|
|
"\u{0017}", // ETB |
|
310
|
|
|
"\u{0018}", // CAN |
|
311
|
|
|
"\u{0019}", // EM |
|
312
|
|
|
"\u{001A}", // SUB |
|
313
|
|
|
"\u{001B}", // escape |
|
314
|
|
|
"\u{001C}", // file separator |
|
315
|
|
|
"\u{001D}", // group separator |
|
316
|
|
|
"\u{001E}", // record separator |
|
317
|
|
|
"\u{001F}", // unit separator |
|
318
|
|
|
"\u{007F}", // DEL |
|
319
|
|
|
"\u{00A0}", // non-breaking space |
|
320
|
|
|
"\u{1680}", // ogham space mark |
|
321
|
|
|
"\u{180E}", // mongolian vowel separator |
|
322
|
|
|
"\u{2000}", // en quad |
|
323
|
|
|
"\u{2001}", // em quad |
|
324
|
|
|
"\u{2002}", // en space |
|
325
|
|
|
"\u{2003}", // em space |
|
326
|
|
|
"\u{2004}", // three-per-em space |
|
327
|
|
|
"\u{2005}", // four-per-em space |
|
328
|
|
|
"\u{2006}", // six-per-em space |
|
329
|
|
|
"\u{2007}", // figure space |
|
330
|
|
|
"\u{2008}", // punctuation space |
|
331
|
|
|
"\u{2009}", // thin space |
|
332
|
|
|
"\u{200A}", // hair space |
|
333
|
|
|
"\u{200B}", // zero width space |
|
334
|
|
|
"\u{202F}", // narrow no-break space |
|
335
|
|
|
"\u{3000}", // ideographic space |
|
336
|
|
|
"\u{FEFF}", // zero width no -break space |
|
337
|
|
|
]; |
|
338
|
|
|
$replace = "\x20"; // plain old normal space |
|
339
|
|
|
$string = str_replace($search, $replace, $string); |
|
340
|
|
|
$string = str_replace(["\n", "\t", "\r"], "\x20", $string); |
|
341
|
|
|
|
|
342
|
|
|
return trim($string); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|