1 | <?php |
||
10 | class Formatter implements FormatTokens |
||
11 | { |
||
12 | /** |
||
13 | * @var string[] Maps tokens to formatting functions |
||
14 | */ |
||
15 | private static $tokenMap = [ |
||
16 | self::TOKEN_DATE_CENTURY => 'formatCentury', |
||
17 | self::TOKEN_SERIAL_PRE => 'formatSerialPre', |
||
18 | self::TOKEN_SERIAL_POST => 'formatSerialPost', |
||
19 | self::TOKEN_DELIMITER => 'formatDelimiter', |
||
20 | self::TOKEN_CHECK_DIGIT => 'formatCheckDigit', |
||
21 | self::TOKEN_SEX => 'formatSex', |
||
22 | self::TOKEN_AGE => 'formatAge', |
||
23 | self::TOKEN_LEGAL_FORM => 'formatLegalForm', |
||
24 | self::TOKEN_BIRTH_COUNTY => 'formatBirthCounty' |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var \Closure Formatting function, takes an Id object and returns a string |
||
29 | */ |
||
30 | private $formatter; |
||
31 | |||
32 | /** |
||
33 | * Create formatter from format string |
||
34 | * |
||
35 | * @param string $format |
||
36 | */ |
||
37 | 6 | public function __construct($format = '') |
|
90 | |||
91 | /** |
||
92 | * Register formatting function |
||
93 | * |
||
94 | * Registered function must take an Id object and return a string |
||
95 | * |
||
96 | * @param callable $formatter Formatting function |
||
97 | * @return void |
||
98 | */ |
||
99 | 6 | public function registerFormatter(callable $formatter) |
|
106 | |||
107 | /** |
||
108 | * Format id using registered formatting functions |
||
109 | * |
||
110 | * @param IdInterface $idObject |
||
111 | * @return string |
||
112 | */ |
||
113 | 6 | public function format(IdInterface $idObject) |
|
118 | |||
119 | /** |
||
120 | * Format birth date century |
||
121 | * |
||
122 | * @param IdInterface $idObject |
||
123 | * @return string |
||
124 | */ |
||
125 | 1 | private function formatCentury(IdInterface $idObject) |
|
129 | |||
130 | /** |
||
131 | * Format serial number pre delimiter |
||
132 | * |
||
133 | * @param IdInterface $idObject |
||
134 | * @return string |
||
135 | */ |
||
136 | 1 | private function formatSerialPre(IdInterface $idObject) |
|
140 | |||
141 | /** |
||
142 | * Format serial number post delimiter |
||
143 | * |
||
144 | * @param IdInterface $idObject |
||
145 | * @return string |
||
146 | */ |
||
147 | 1 | private function formatSerialPost(IdInterface $idObject) |
|
151 | |||
152 | /** |
||
153 | * Format delimiter |
||
154 | * |
||
155 | * @param IdInterface $idObject |
||
156 | * @return string |
||
157 | */ |
||
158 | 1 | private function formatDelimiter(IdInterface $idObject) |
|
162 | |||
163 | /** |
||
164 | * Format check digit |
||
165 | * |
||
166 | * @param IdInterface $idObject |
||
167 | * @return string |
||
168 | */ |
||
169 | 1 | private function formatCheckDigit(IdInterface $idObject) |
|
173 | |||
174 | /** |
||
175 | * Format sex |
||
176 | * |
||
177 | * @param IdInterface $idObject |
||
178 | * @return string |
||
179 | */ |
||
180 | 1 | private function formatSex(IdInterface $idObject) |
|
184 | |||
185 | /** |
||
186 | * Format age |
||
187 | * |
||
188 | * @param IdInterface $idObject |
||
189 | * @return string |
||
190 | */ |
||
191 | 1 | private function formatAge(IdInterface $idObject) |
|
195 | |||
196 | /** |
||
197 | * Format legal form |
||
198 | * |
||
199 | * @param IdInterface $idObject |
||
200 | * @return string |
||
201 | */ |
||
202 | 1 | private function formatLegalForm(IdInterface $idObject) |
|
206 | |||
207 | /** |
||
208 | * Format birth county |
||
209 | * |
||
210 | * @param IdInterface $idObject |
||
211 | * @return string |
||
212 | */ |
||
213 | 1 | private function formatBirthCounty(IdInterface $idObject) |
|
217 | } |
||
218 |