-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpValidAMKA.class.php
191 lines (154 loc) · 4.54 KB
/
phpValidAMKA.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Class phpValidAMKA
*
* a simple php class to validate Greek Social Security Number (AMKA)
* https://github.com/pontikis/php_valid_AMKA
*
* @author Christos Pontikis http://pontikis.net
* @copyright Christos Pontikis
* @license MIT http://opensource.org/licenses/MIT
* @version 0.7.2 (XX XXX 2017)
*
*/
class phpValidAMKA {
/**
* @var int valid value is 11
*/
private $amka_length;
/**
* @var null|string one of 'male'. 'female'
*/
private $gender;
/**
* @var null|string DDMMYY
*/
private $date_of_birth;
private $last_error;
/**
* phpValidAMKA constructor.
* @param array $options
*/
public function __construct($options = array()) {
// initialize ----------------------------------------------------------
$this->amka_length = 11;
// options -------------------------------------------------------------
$defaults = array(
'gender' => null,
'date_of_birth' => null
);
$opt = array_merge($defaults, $options);
// optional
$this->gender = $opt['gender'];
$this->date_of_birth = $opt['date_of_birth'];
// error ---------------------------------------------------------------
$this->last_error = null;
if($this->gender) {
if(!in_array($this->gender, array('male', 'female'))) {
$this->last_error = 'invalid_parameter_gender';
}
}
if($this->date_of_birth) {
if(!$this->_isValidDateString($this->date_of_birth)) {
$this->last_error = 'invalid_parameter_date_of_birth';
}
}
}
// public functions - getters ----------------------------------------------
public function getLastError() {
return $this->last_error;
}
// public functions - setters ----------------------------------------------
/**
* Set option
*
* @param $opt
* @param $val
*/
public function setOption($opt, $val) {
$this->$opt = $val;
}
// public functions - main methods -----------------------------------------
/**
* @param string $amka
* @return bool
*/
public function validateAMKA($amka) {
// validate number of characters
if(strlen($amka) != $this->amka_length) {
$this->last_error = 'fatal_error_invalid_number_of_characters';
return false;
}
// allow only digits
if(preg_match("/[^\pN]/u", $amka)) {
$this->last_error = 'fatal_error_does_not_consisted_of_digits';
return false;
}
// check first part (date of birth)
if(!$this->_isValidDateString(substr($amka, 0, 6))) {
$this->last_error = 'error_amka_first_part_invalid_date_of_birth';
return false;
}
if($this->date_of_birth) {
if($this->date_of_birth != (substr($amka, 0, 6))) {
$this->last_error = 'error_amka_first_part_given_date_of_birth_does_not_match';
return false;
}
}
// check second part
if($this->gender) {
switch($this->gender) {
case 'male':
if(!$this->_is_odd(substr($amka, 6, 4))) {
$this->last_error = 'error_amka_second_part_even_in_male';
return false;
}
break;
case 'female':
if($this->_is_odd(substr($amka, 6, 4))) {
$this->last_error = 'error_amka_second_part_odd_in_female';
return false;
}
break;
}
}
return true;
}
// private functions -------------------------------------------------------
/**
* Check if a string is a valid date(time)
*
* DateTime::createFromFormat requires PHP >= 5.3
*
* @param string $str_dt
* @return bool|int
*/
// private function _isValidDateString($str_dt) {
// $date = DateTime::createFromFormat('dmy', $str_dt, new DateTimeZone('Europe/Athens'));
// $a_err = DateTime::getLastErrors(); // compatibility with php 5.3
// return $date && $a_err['warning_count'] == 0 && $a_err['error_count'] == 0;
// }
private function _isValidDateString($str_dt) {
// Create a DateTime object from the string in the specified format and timezone
$date = DateTime::createFromFormat('dmy', $str_dt, new DateTimeZone('Europe/Athens'));
// If DateTime::createFromFormat() returns false, the string is not a valid date
if ($date === false) {
return false;
}
// Retrieve the array of errors and warnings
$errors = DateTime::getLastErrors();
// Ensure that errors is indeed an array and check for error or warning counts
if (is_array($errors) && ($errors['error_count'] > 0 || $errors['warning_count'] > 0)) {
return false;
}
// The string is a valid date if there are no errors or warnings
return true;
}
/**
* @param $x
* @return bool
*/
private function _is_odd($x) {
return ($x % 2 === 0) ? false : true;
}
}