1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Birthdate Validation |
4
|
|
|
* |
5
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
6
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
7
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
8
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
9
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
10
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
11
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
12
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
13
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
14
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
15
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
16
|
|
|
* |
17
|
|
|
* This software consists of voluntary contributions made by many individuals |
18
|
|
|
* and is licensed under the MIT license. |
19
|
|
|
* |
20
|
|
|
* @author Jacques Marneweck <[email protected]> |
21
|
|
|
* @copyright 2015-2016 Jacques Marneweck. All rights strictly reserved. |
22
|
|
|
* @license MIT |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Jacques\Validators; |
26
|
|
|
|
27
|
|
|
use Carbon\Carbon; |
28
|
|
|
|
29
|
|
|
class Birthdate |
30
|
|
|
{ |
31
|
|
|
public static function is_valid($birthdate) |
32
|
|
|
{ |
33
|
|
|
try { |
34
|
|
|
$date = Carbon::createFromFormat('Y-m-d', $birthdate, 'UTC'); |
35
|
|
|
if ($date->toDateString() === $birthdate) { |
|
|
|
|
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return false; |
40
|
|
|
} catch (\Exception $e) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function is_valid_for_id($birthdate, $id) |
46
|
|
|
{ |
47
|
|
|
$match = preg_match("!^(\d{2})(\d{2})(\d{2})\d\d{6}$!", $id, $matches); |
48
|
|
|
if (!$match) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
list(, $year, $month, $day) = $matches; |
52
|
|
|
/** |
53
|
|
|
* Check that the date is valid |
54
|
|
|
*/ |
55
|
|
|
if (!Birthdate::is_valid($birthdate)) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($year > 23) { |
60
|
|
|
$year += 1900; |
61
|
|
|
} else { |
62
|
|
|
$year += 2000; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$date = Carbon::createFromFormat('Y-m-d', vsprintf("%d-%d-%d", [$year, $month, $day]), 'UTC'); |
67
|
|
|
if ($date->toDateString() === $birthdate) { |
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: