1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Dimtrovich/Validation. |
||
5 | * |
||
6 | * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Dimtrovich\Validation\Rules; |
||
13 | |||
14 | use DateTime; |
||
0 ignored issues
–
show
|
|||
15 | use DateTimeInterface; |
||
16 | use Exception; |
||
17 | |||
18 | class Date extends AbstractRule |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $fillableParams = ['format']; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $params = [ |
||
29 | 'format' => '', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * {@inheritDoc} |
||
34 | */ |
||
35 | public function check($value): bool |
||
36 | { |
||
37 | if ((! is_string($value) && ! is_numeric($value) && ! ($value instanceof DateTimeInterface))) { |
||
38 | 2 | return false; |
|
39 | } |
||
40 | |||
41 | if (! empty($format = $this->parameter('format'))) { |
||
42 | 4 | $this->message .= ' format'; |
|
43 | |||
44 | 4 | $date = DateTime::createFromFormat($format, $value); |
|
45 | |||
46 | 4 | return $date && $date->format($format) === $value; |
|
47 | // return date_create_from_format($format, $value) !== false; |
||
48 | } |
||
49 | |||
50 | if ($value instanceof DateTimeInterface) { |
||
51 | 2 | return true; |
|
52 | } |
||
53 | |||
54 | try { |
||
55 | if (strtotime($value) === false) { |
||
56 | 2 | return false; |
|
57 | } |
||
58 | } catch (Exception $e) { |
||
59 | return false; |
||
60 | } |
||
61 | |||
62 | 2 | $date = date_parse($value); |
|
63 | |||
64 | 2 | return checkdate($date['month'], $date['day'], $date['year']); |
|
65 | } |
||
66 | } |
||
67 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: