We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of Respect/Validation. |
||
5 | * |
||
6 | * (c) Alexandre Gomes Gaigalas <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE file |
||
9 | * that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Respect\Validation\Rules; |
||
15 | |||
16 | use Respect\Validation\Exceptions\ComponentException; |
||
17 | use Respect\Validation\Helpers\CanValidateDateTime; |
||
18 | |||
19 | use function date; |
||
0 ignored issues
–
show
|
|||
20 | use function is_scalar; |
||
21 | use function preg_match; |
||
22 | use function sprintf; |
||
23 | use function strtotime; |
||
24 | |||
25 | /** |
||
26 | * Validates if input is a date. |
||
27 | * |
||
28 | * @author Bruno Luiz da Silva <[email protected]> |
||
29 | * @author Henrique Moody <[email protected]> |
||
30 | */ |
||
31 | final class Date extends AbstractRule |
||
32 | { |
||
33 | use CanValidateDateTime; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $format; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $sample; |
||
44 | |||
45 | /** |
||
46 | * Initializes the rule. |
||
47 | * |
||
48 | * @throws ComponentException |
||
49 | 4 | */ |
|
50 | public function __construct(string $format = 'Y-m-d') |
||
51 | 4 | { |
|
52 | 2 | if (!preg_match('/^[djSFmMnYy\W]+$/', $format)) { |
|
53 | throw new ComponentException(sprintf('"%s" is not a valid date format', $format)); |
||
54 | } |
||
55 | 2 | ||
56 | 2 | $this->format = $format; |
|
57 | 2 | $this->sample = date($format, strtotime('2005-12-30')); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | 24 | */ |
|
63 | public function validate($input): bool |
||
64 | 24 | { |
|
65 | 4 | if (!is_scalar($input)) { |
|
66 | return false; |
||
67 | } |
||
68 | 20 | ||
69 | return $this->isDateTime($this->format, (string) $input); |
||
70 | } |
||
71 | } |
||
72 |
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: