1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* IsDateOrTime.php |
5
|
|
|
* Copyright (c) 2019 [email protected] |
6
|
|
|
* |
7
|
|
|
* This file is part of Firefly III. |
8
|
|
|
* |
9
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* Firefly III 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 General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
declare(strict_types=1); |
24
|
|
|
|
25
|
|
|
namespace FireflyIII\Rules; |
26
|
|
|
|
27
|
|
|
use Carbon\Carbon; |
28
|
|
|
use Carbon\Exceptions\InvalidDateException; |
29
|
|
|
use Illuminate\Contracts\Validation\Rule; |
30
|
|
|
use Log; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class IsDateOrTime |
34
|
|
|
*/ |
35
|
|
|
class IsDateOrTime implements Rule |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the validation error message. |
40
|
|
|
* |
41
|
|
|
* @return string|array |
42
|
|
|
*/ |
43
|
|
|
public function message() |
44
|
|
|
{ |
45
|
|
|
return (string)trans('validation.date_or_time'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Determine if the validation rule passes. |
50
|
|
|
* |
51
|
|
|
* @param string $attribute |
52
|
|
|
* @param mixed $value |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function passes($attribute, $value): bool |
57
|
|
|
{ |
58
|
|
|
$value = (string)$value; |
59
|
|
|
if (10 === \strlen($value)) { |
60
|
|
|
// probably a date format. |
61
|
|
|
try { |
62
|
|
|
Carbon::createFromFormat('Y-m-d', $value); |
63
|
|
|
} catch (InvalidDateException $e) { |
64
|
|
|
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage())); |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
// is an atom string, I hope? |
72
|
|
|
try { |
73
|
|
|
Carbon::parse($value); |
74
|
|
|
} catch (InvalidDateException $e) { |
75
|
|
|
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage())); |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|