Completed
Push — master ( a004d3...9bce71 )
by Hannes
02:34
created

Formatter::formatCentury()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace byrokrat\id\Formatter;
4
5
use byrokrat\id\IdInterface;
6
7
/**
8
 * Id formatter
9
 */
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 = '')
38
    {
39
        // Register empty formatting function
40
        $this->formatter = function () {
41 6
            return '';
42
        };
43
44
        // Used to track escaping state
45 6
        $escape = '';
46
47 6
        foreach (str_split($format) as $token) {
48 6
            switch ($escape . $token) {
49 6
                case self::TOKEN_DATE_YEAR_FULL:
50 6
                case self::TOKEN_DATE_YEAR:
51 5
                case self::TOKEN_DATE_MONTH:
52 5
                case self::TOKEN_DATE_MONTH_SHORT:
53 5
                case self::TOKEN_DATE_MONTH_TEXT:
54 5
                case self::TOKEN_DATE_MONTH_TEXT_SHORT:
55 5
                case self::TOKEN_DATE_MONTH_DAYS:
56 5
                case self::TOKEN_DATE_WEEK:
57 5
                case self::TOKEN_DATE_DAY:
58 5
                case self::TOKEN_DATE_DAY_SHORT:
59 5
                case self::TOKEN_DATE_DAY_TEXT:
60 5
                case self::TOKEN_DATE_DAY_TEST_SHORT:
61 5
                case self::TOKEN_DATE_DAY_NUMERIC:
62 5
                case self::TOKEN_DATE_DAY_NUMERIC_ISO:
63 5
                case self::TOKEN_DATE_DAY_OF_YEAR:
64
                    $this->registerFormatter(function (IdInterface $idObject) use ($token) {
65 2
                        return $idObject->getBirthDate()->format($token);
66 2
                    });
67 2
                    break;
68 5
                case self::TOKEN_DATE_CENTURY:
69 5
                case self::TOKEN_SERIAL_PRE:
70 5
                case self::TOKEN_SERIAL_POST:
71 5
                case self::TOKEN_DELIMITER:
72 5
                case self::TOKEN_CHECK_DIGIT:
73 5
                case self::TOKEN_SEX:
74 5
                case self::TOKEN_AGE:
75 4
                case self::TOKEN_LEGAL_FORM:
76 3
                case self::TOKEN_BIRTH_COUNTY:
77 3
                    $this->registerFormatter([$this, self::$tokenMap[$token]]);
78 3
                    break;
79 3
                case self::TOKEN_ESCAPE:
80 1
                    $escape = $token;
81 1
                    break;
82
                default:
83 3
                    $escape = '';
84
                    $this->registerFormatter(function () use ($token) {
85 3
                        return $token;
86 6
                    });
87
            }
88
        }
89 6
    }
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)
100
    {
101 6
        $oldFormatter = $this->formatter;
102 6
        $this->formatter = function (IdInterface $idObject) use ($oldFormatter, $formatter) {
103 6
            return $oldFormatter($idObject) . $formatter($idObject);
104
        };
105 6
    }
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)
114
    {
115 6
        $formatter = $this->formatter;
116 6
        return $formatter($idObject);
117
    }
118
119
    /**
120
     * Format birth date century
121
     *
122
     * @param  IdInterface $idObject
123
     * @return string
124
     */
125 1
    private function formatCentury(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
126
    {
127 1
        return $idObject->getCentury();
128
    }
129
130
    /**
131
     * Format serial number pre delimiter
132
     *
133
     * @param  IdInterface $idObject
134
     * @return string
135
     */
136 1
    private function formatSerialPre(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
137
    {
138 1
        return $idObject->getSerialPreDelimiter();
139
    }
140
141
    /**
142
     * Format serial number post delimiter
143
     *
144
     * @param  IdInterface $idObject
145
     * @return string
146
     */
147 1
    private function formatSerialPost(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
148
    {
149 1
        return $idObject->getSerialPostDelimiter();
150
    }
151
152
    /**
153
     * Format delimiter
154
     *
155
     * @param  IdInterface $idObject
156
     * @return string
157
     */
158 1
    private function formatDelimiter(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
159
    {
160 1
        return $idObject->getDelimiter();
161
    }
162
163
    /**
164
     * Format check digit
165
     *
166
     * @param  IdInterface $idObject
167
     * @return string
168
     */
169 1
    private function formatCheckDigit(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
170
    {
171 1
        return $idObject->getCheckDigit();
172
    }
173
174
    /**
175
     * Format sex
176
     *
177
     * @param  IdInterface $idObject
178
     * @return string
179
     */
180 1
    private function formatSex(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
181
    {
182 1
        return $idObject->getSex();
183
    }
184
185
    /**
186
     * Format age
187
     *
188
     * @param  IdInterface $idObject
189
     * @return string
190
     */
191 1
    private function formatAge(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
192
    {
193 1
        return (string)$idObject->getAge();
194
    }
195
196
    /**
197
     * Format legal form
198
     *
199
     * @param  IdInterface $idObject
200
     * @return string
201
     */
202 1
    private function formatLegalForm(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
203
    {
204 1
        return $idObject->getLegalForm();
205
    }
206
207
    /**
208
     * Format birth county
209
     *
210
     * @param  IdInterface $idObject
211
     * @return string
212
     */
213 1
    private function formatBirthCounty(IdInterface $idObject)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
214
    {
215 1
        return $idObject->getBirthCounty();
216
    }
217
}
218