1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace byrokrat\id; |
4
|
|
|
|
5
|
|
|
use byrokrat\id\Exception\DateNotSupportedException; |
6
|
|
|
use byrokrat\id\Exception\InvalidCheckDigitException; |
7
|
|
|
use byrokrat\id\Exception\InvalidStructureException; |
8
|
|
|
use byrokrat\id\Formatter\Formatter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Standard implementation of IdInterface |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractId implements IdInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string Serial number pre delimiter |
17
|
|
|
*/ |
18
|
|
|
protected $serialPre = '000000'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string Serial number post delimiter |
22
|
|
|
*/ |
23
|
|
|
protected $serialPost = '000'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string Date and control string delimiter (- or +) |
27
|
|
|
*/ |
28
|
|
|
protected $delimiter = '-'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string Check digit |
32
|
|
|
*/ |
33
|
|
|
protected $checkDigit = '0'; |
34
|
|
|
|
35
|
100 |
|
public function getId() |
36
|
|
|
{ |
37
|
100 |
|
return $this->getSerialPreDelimiter() |
38
|
100 |
|
. $this->getDelimiter() |
39
|
100 |
|
. $this->getSerialPostDelimiter() |
40
|
100 |
|
. $this->getCheckDigit(); |
41
|
|
|
} |
42
|
|
|
|
43
|
15 |
|
public function __tostring() |
44
|
|
|
{ |
45
|
15 |
|
return $this->getId(); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function format($format) |
49
|
|
|
{ |
50
|
1 |
|
return (new Formatter($format))->format($this); |
51
|
|
|
} |
52
|
|
|
|
53
|
101 |
|
public function getSerialPreDelimiter() |
54
|
|
|
{ |
55
|
101 |
|
return $this->serialPre; |
56
|
|
|
} |
57
|
|
|
|
58
|
102 |
|
public function getSerialPostDelimiter() |
59
|
|
|
{ |
60
|
102 |
|
return $this->serialPost; |
61
|
|
|
} |
62
|
|
|
|
63
|
108 |
|
public function getDelimiter() |
64
|
|
|
{ |
65
|
108 |
|
return $this->delimiter; |
66
|
|
|
} |
67
|
|
|
|
68
|
101 |
|
public function getCheckDigit() |
69
|
|
|
{ |
70
|
101 |
|
return $this->checkDigit; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function getBirthDate() |
74
|
|
|
{ |
75
|
2 |
|
throw new DateNotSupportedException("Trying to access date on id type where it is not supported"); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getDate() |
79
|
|
|
{ |
80
|
|
|
trigger_error('getDate() is deprecated, use getBirthDate() instead.', E_USER_DEPRECATED); |
81
|
|
|
return $this->getBirthDate(); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function getAge(\DateTimeInterface $atDate = null) |
85
|
|
|
{ |
86
|
3 |
|
return (int)$this->getBirthDate()->diff($atDate ?: new \DateTime)->format('%y'); |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
public function getCentury() |
90
|
|
|
{ |
91
|
4 |
|
return substr($this->getBirthDate()->format('Y'), 0, 2); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
public function getSex() |
95
|
|
|
{ |
96
|
2 |
|
return IdInterface::SEX_UNDEFINED; |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
public function isMale() |
100
|
|
|
{ |
101
|
4 |
|
return $this->getSex() == IdInterface::SEX_MALE; |
102
|
|
|
} |
103
|
|
|
|
104
|
4 |
|
public function isFemale() |
105
|
|
|
{ |
106
|
4 |
|
return $this->getSex() == IdInterface::SEX_FEMALE; |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
public function isSexUndefined() |
110
|
|
|
{ |
111
|
4 |
|
return $this->getSex() == IdInterface::SEX_UNDEFINED; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
public function getBirthCounty() |
115
|
|
|
{ |
116
|
2 |
|
return IdInterface::COUNTY_UNDEFINED; |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
public function getLegalForm() |
120
|
|
|
{ |
121
|
2 |
|
return IdInterface::LEGAL_FORM_UNDEFINED; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
public function isLegalFormUndefined() |
125
|
|
|
{ |
126
|
3 |
|
return $this->getLegalForm() == IdInterface::LEGAL_FORM_UNDEFINED; |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
public function isStateOrParish() |
130
|
|
|
{ |
131
|
3 |
|
return $this->getLegalForm() == IdInterface::LEGAL_FORM_STATE_PARISH; |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
public function isIncorporated() |
135
|
|
|
{ |
136
|
3 |
|
return $this->getLegalForm() == IdInterface::LEGAL_FORM_INCORPORATED; |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
public function isPartnership() |
140
|
|
|
{ |
141
|
3 |
|
return $this->getLegalForm() == IdInterface::LEGAL_FORM_PARTNERSHIP; |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
public function isAssociation() |
145
|
|
|
{ |
146
|
3 |
|
return $this->getLegalForm() == IdInterface::LEGAL_FORM_ASSOCIATION; |
147
|
|
|
} |
148
|
|
|
|
149
|
3 |
|
public function isNonProfit() |
150
|
|
|
{ |
151
|
3 |
|
return $this->getLegalForm() == IdInterface::LEGAL_FORM_NONPROFIT; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
public function isTradingCompany() |
155
|
|
|
{ |
156
|
3 |
|
return $this->getLegalForm() == IdInterface::LEGAL_FORM_TRADING; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Parse id using regular expression |
161
|
|
|
* |
162
|
|
|
* @param string $regexp |
163
|
|
|
* @param string $raw |
164
|
|
|
* @return string[] Array of matches |
165
|
|
|
* @throws InvalidStructureException If regular expression does not match |
166
|
|
|
*/ |
167
|
179 |
|
protected function parseNumber($regexp, $raw) |
168
|
|
|
{ |
169
|
179 |
|
if (!preg_match($regexp, $raw, $matches)) { |
170
|
70 |
|
throw new InvalidStructureException("Unable to parse $raw, invalid structure"); |
171
|
|
|
} |
172
|
|
|
|
173
|
109 |
|
return $matches; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Verify that the last digit of id is a valid check digit |
178
|
|
|
* |
179
|
|
|
* @return void |
180
|
|
|
* @throws InvalidCheckDigitException If check digit is not valid |
181
|
|
|
*/ |
182
|
95 |
|
protected function validateCheckDigit() |
183
|
|
|
{ |
184
|
95 |
|
if (!Modulo10::isValid(preg_replace('/[^0-9]/', '', $this->getId()))) { |
185
|
48 |
|
throw new InvalidCheckDigitException("Invalid check digit in {$this->getId()}"); |
186
|
|
|
} |
187
|
47 |
|
} |
188
|
|
|
} |
189
|
|
|
|