|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Validations reworked for use with Valitron. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Jacques Marneweck <[email protected]> |
|
6
|
|
|
* @copyright 2002-2018 Jacques Marneweck. All rights strictly reserved. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Jacques\Validators; |
|
10
|
|
|
|
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
|
|
13
|
|
|
class Valitron |
|
14
|
|
|
{ |
|
15
|
|
|
static public function addrules() |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Validate the possibility of the South African Identity Number beng a valid South |
|
19
|
|
|
* African identity number. |
|
20
|
|
|
*/ |
|
21
|
|
|
\Valitron\Validator::addRule('za_identity_number', function($field, $value, array $params, array $fields) { |
|
|
|
|
|
|
22
|
|
|
if (!ctype_digit($value)) { |
|
23
|
|
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$match = preg_match("!^(\d{2})(\d{2})(\d{2})\d\d{6}$!", $value, $matches); |
|
27
|
|
|
if (!$match) { |
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
list (, $year, $month, $day) = $matches; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check citizenship of the users id (0 = .za, 1 = permanent resident) |
|
34
|
|
|
*/ |
|
35
|
|
|
if (!in_array($value{10}, array(0, 1))) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Seen 8 or 9 here. |
|
41
|
|
|
*/ |
|
42
|
|
|
if (!in_array($value{11}, [8, 9])) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$idvalid = \PayBreak\Luhn\Luhn::validateNumber($value); |
|
47
|
|
|
|
|
48
|
|
|
return ($idvalid); |
|
49
|
|
|
}, 'must be a valid South African Identity Number'); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Validate the possibility of a mobile number being a valid South African Mobile Number. |
|
53
|
|
|
*/ |
|
54
|
|
|
\Valitron\Validator::addRule('za_mobile_number', function($field, $value, array $params, array $fields) { |
|
|
|
|
|
|
55
|
|
|
$msisdn = str_replace(array(' ', '-', '(', ')'), '', $value); |
|
56
|
|
|
$country = 'ZA'; |
|
57
|
|
|
|
|
58
|
|
|
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); |
|
59
|
|
|
try { |
|
60
|
|
|
$phoneNumberProto = $phoneUtil->parse($msisdn, $country); |
|
61
|
|
|
} catch (\libphonenumber\NumberParseException $e) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!$phoneUtil->isValidNumber($phoneNumberProto)) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ( |
|
70
|
|
|
!in_array( |
|
71
|
|
|
$phoneUtil->getNumberType($phoneNumberProto), |
|
72
|
|
|
[ |
|
73
|
|
|
1, |
|
74
|
|
|
2, |
|
75
|
|
|
] |
|
76
|
|
|
) |
|
77
|
|
|
) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ( |
|
82
|
|
|
!is_null($country) |
|
83
|
|
|
) { |
|
84
|
|
|
if ($country == $phoneUtil->getRegionCodeForNumber($phoneNumberProto)) { |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return true; |
|
92
|
|
|
}, 'must be a valid South African Mobile Number'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|