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.md" |
9
|
|
|
* file 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\Helpers\DateTimeHelper; |
17
|
|
|
use function date; |
|
|
|
|
18
|
|
|
use function date_parse_from_format; |
19
|
|
|
use function is_scalar; |
20
|
|
|
use function strtotime; |
21
|
|
|
use function vsprintf; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Abstract class to validate ages. |
25
|
|
|
* |
26
|
|
|
* @author Henrique Moody <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractAge extends AbstractRule |
29
|
|
|
{ |
30
|
|
|
use DateTimeHelper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $age; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string|null |
39
|
|
|
*/ |
40
|
|
|
private $format; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $baseDate; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes the rule. |
49
|
|
|
* |
50
|
|
|
* @param int $age |
51
|
|
|
* @param string|null $format |
52
|
|
|
*/ |
53
|
3 |
|
public function __construct(int $age, string $format = null) |
54
|
|
|
{ |
55
|
3 |
|
$this->age = $age; |
56
|
3 |
|
$this->format = $format; |
57
|
3 |
|
$this->baseDate = date('Ymd') - $this->age * 10000; |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Should compare the current base date with the given one. |
62
|
|
|
* |
63
|
|
|
* The dates are represented as integers in the format "Ymd". |
64
|
|
|
* |
65
|
|
|
* @param int $baseDate |
66
|
|
|
* @param int $givenDate |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
abstract protected function compare(int $baseDate, int $givenDate): bool; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
21 |
|
public function validate($input): bool |
76
|
|
|
{ |
77
|
21 |
|
if (!is_scalar($input)) { |
78
|
7 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
14 |
|
if (null === $this->format) { |
82
|
7 |
|
return $this->isValidWithoutFormat((string) $input); |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
return $this->isValidWithFormat($this->format, (string) $input); |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
private function isValidWithoutFormat(string $dateTime): bool |
89
|
|
|
{ |
90
|
7 |
|
$timestamp = strtotime($dateTime); |
91
|
7 |
|
if (false === $timestamp) { |
|
|
|
|
92
|
1 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
return $this->compare($this->baseDate, (int) date('Ymd', $timestamp)); |
96
|
|
|
} |
97
|
|
|
|
98
|
9 |
|
private function isValidWithFormat(string $format, string $dateTime): bool |
99
|
|
|
{ |
100
|
9 |
|
if (!$this->isDateTime($format, $dateTime)) { |
101
|
3 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
return $this->compare( |
105
|
6 |
|
$this->baseDate, |
106
|
6 |
|
(int) vsprintf('%d%02d%02d', date_parse_from_format($format, $dateTime)) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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: