GedcomCodeRela::getValue()   F
last analyzed

Complexity

Conditions 73
Paths 144

Size

Total Lines 276
Code Lines 149

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 73
eloc 149
nop 2
dl 0
loc 276
rs 3.0399
c 0
b 0
f 0
nc 144

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\GedcomCode;
21
22
use Fisharebest\Webtrees\GedcomRecord;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Individual;
25
26
/**
27
 * Class GedcomCodeRela - Functions and logic for GEDCOM "RELA" codes
28
 */
29
class GedcomCodeRela
30
{
31
    // List of possible values for the RELA tag
32
    private const TYPES = [
33
        'attendant',
34
        'attending',
35
        'best_man',
36
        'bridesmaid',
37
        'buyer',
38
        'circumciser',
39
        'civil_registrar',
40
        'employee',
41
        'employer',
42
        'foster_child',
43
        'foster_father',
44
        'foster_mother',
45
        'friend',
46
        'godfather',
47
        'godmother',
48
        'godparent',
49
        'godson',
50
        'goddaughter',
51
        'godchild',
52
        'guardian',
53
        'informant',
54
        'lodger',
55
        'nanny',
56
        'nurse',
57
        'owner',
58
        'priest',
59
        'rabbi',
60
        'registry_officer',
61
        'seller',
62
        'servant',
63
        'slave',
64
        'ward',
65
        'witness',
66
    ];
67
68
    /**
69
     * Translate a code, for an (optional) record.
70
     * We need the record to translate the sex (godfather/godmother) but
71
     * we won’t have this when adding data for new individuals.
72
     *
73
     * @param string            $type
74
     * @param GedcomRecord|null $record
75
     *
76
     * @return string
77
     */
78
    public static function getValue(string $type, GedcomRecord $record = null): string
79
    {
80
        if ($record instanceof Individual) {
81
            $sex = $record->sex();
82
        } else {
83
            $sex = 'U';
84
        }
85
86
        switch ($type) {
87
            case 'attendant':
88
                if ($sex === 'M') {
89
                    return I18N::translateContext('MALE', 'Attendant');
90
                }
91
92
                if ($sex === 'F') {
93
                    return I18N::translateContext('FEMALE', 'Attendant');
94
                }
95
96
                return I18N::translate('Attendant');
97
98
            case 'attending':
99
                if ($sex === 'M') {
100
                    return I18N::translateContext('MALE', 'Attending');
101
                }
102
103
                if ($sex === 'F') {
104
                    return I18N::translateContext('FEMALE', 'Attending');
105
                }
106
107
                return I18N::translate('Attending');
108
109
            case 'best_man':
110
                // always male
111
                return I18N::translate('Best man');
112
113
            case 'bridesmaid':
114
                // always female
115
                return I18N::translate('Bridesmaid');
116
117
            case 'buyer':
118
                if ($sex === 'M') {
119
                    return I18N::translateContext('MALE', 'Buyer');
120
                }
121
122
                if ($sex === 'F') {
123
                    return I18N::translateContext('FEMALE', 'Buyer');
124
                }
125
126
                return I18N::translate('Buyer');
127
128
            case 'circumciser':
129
                // always male
130
                return I18N::translate('Circumciser');
131
132
            case 'civil_registrar':
133
                if ($sex === 'M') {
134
                    return I18N::translateContext('MALE', 'Civil registrar');
135
                }
136
137
                if ($sex === 'F') {
138
                    return I18N::translateContext('FEMALE', 'Civil registrar');
139
                }
140
141
                return I18N::translate('Civil registrar');
142
143
            case 'employee':
144
                if ($sex === 'M') {
145
                    return I18N::translateContext('MALE', 'Employee');
146
                }
147
148
                if ($sex === 'F') {
149
                    return I18N::translateContext('FEMALE', 'Employee');
150
                }
151
152
                return I18N::translate('Employee');
153
154
            case 'employer':
155
                if ($sex === 'M') {
156
                    return I18N::translateContext('MALE', 'Employer');
157
                }
158
159
                if ($sex === 'F') {
160
                    return I18N::translateContext('FEMALE', 'Employer');
161
                }
162
163
                return I18N::translate('Employer');
164
165
            case 'foster_child':
166
                // no sex implied
167
                return I18N::translate('Foster child');
168
169
            case 'foster_father':
170
                // always male
171
                return I18N::translate('Foster father');
172
173
            case 'foster_mother':
174
                // always female
175
                return I18N::translate('Foster mother');
176
177
            case 'friend':
178
                if ($sex === 'M') {
179
                    return I18N::translateContext('MALE', 'Friend');
180
                }
181
182
                if ($sex === 'F') {
183
                    return I18N::translateContext('FEMALE', 'Friend');
184
                }
185
186
                return I18N::translate('Friend');
187
188
            case 'godfather':
189
                // always male
190
                return I18N::translate('Godfather');
191
192
            case 'godmother':
193
                // always female
194
                return I18N::translate('Godmother');
195
196
            case 'godparent':
197
                if ($sex === 'M') {
198
                    return I18N::translate('Godfather');
199
                }
200
201
                if ($sex === 'F') {
202
                    return I18N::translate('Godmother');
203
                }
204
205
                return I18N::translate('Godparent');
206
207
            case 'godson':
208
                // always male
209
                return I18N::translate('Godson');
210
211
            case 'goddaughter':
212
                // always female
213
                return I18N::translate('Goddaughter');
214
215
            case 'godchild':
216
                if ($sex === 'M') {
217
                    return I18N::translate('Godson');
218
                }
219
220
                if ($sex === 'F') {
221
                    return I18N::translate('Goddaughter');
222
                }
223
224
                return I18N::translate('Godchild');
225
226
            case 'guardian':
227
                if ($sex === 'M') {
228
                    return I18N::translateContext('MALE', 'Guardian');
229
                }
230
231
                if ($sex === 'F') {
232
                    return I18N::translateContext('FEMALE', 'Guardian');
233
                }
234
235
                return I18N::translate('Guardian');
236
237
            case 'informant':
238
                if ($sex === 'M') {
239
                    return I18N::translateContext('MALE', 'Informant');
240
                }
241
242
                if ($sex === 'F') {
243
                    return I18N::translateContext('FEMALE', 'Informant');
244
                }
245
246
                return I18N::translate('Informant');
247
248
            case 'lodger':
249
                if ($sex === 'M') {
250
                    return I18N::translateContext('MALE', 'Lodger');
251
                }
252
253
                if ($sex === 'F') {
254
                    return I18N::translateContext('FEMALE', 'Lodger');
255
                }
256
257
                return I18N::translate('Lodger');
258
259
            case 'nanny':
260
                // no sex implied
261
                return I18N::translate('Nanny');
262
263
            case 'nurse':
264
                if ($sex === 'M') {
265
                    return I18N::translateContext('MALE', 'Nurse');
266
                }
267
268
                if ($sex === 'F') {
269
                    return I18N::translateContext('FEMALE', 'Nurse');
270
                }
271
272
                return I18N::translate('Nurse');
273
274
            case 'owner':
275
                if ($sex === 'M') {
276
                    return I18N::translateContext('MALE', 'Owner');
277
                }
278
279
                if ($sex === 'F') {
280
                    return I18N::translateContext('FEMALE', 'Owner');
281
                }
282
283
                return I18N::translate('Owner');
284
285
            case 'priest':
286
                // no sex implied
287
                return I18N::translate('Priest');
288
289
            case 'rabbi':
290
                // always male
291
                return I18N::translate('Rabbi');
292
293
            case 'registry_officer':
294
                if ($sex === 'M') {
295
                    return I18N::translateContext('MALE', 'Registry officer');
296
                }
297
298
                if ($sex === 'F') {
299
                    return I18N::translateContext('FEMALE', 'Registry officer');
300
                }
301
302
                return I18N::translate('Registry officer');
303
304
            case 'seller':
305
                if ($sex === 'M') {
306
                    return I18N::translateContext('MALE', 'Seller');
307
                }
308
309
                if ($sex === 'F') {
310
                    return I18N::translateContext('FEMALE', 'Seller');
311
                }
312
313
                return I18N::translate('Seller');
314
315
            case 'servant':
316
                if ($sex === 'M') {
317
                    return I18N::translateContext('MALE', 'Servant');
318
                }
319
320
                if ($sex === 'F') {
321
                    return I18N::translateContext('FEMALE', 'Servant');
322
                }
323
324
                return I18N::translate('Servant');
325
326
            case 'slave':
327
                if ($sex === 'M') {
328
                    return I18N::translateContext('MALE', 'Slave');
329
                }
330
331
                if ($sex === 'F') {
332
                    return I18N::translateContext('FEMALE', 'Slave');
333
                }
334
335
                return I18N::translate('Slave');
336
337
            case 'ward':
338
                if ($sex === 'M') {
339
                    return I18N::translateContext('MALE', 'Ward');
340
                }
341
342
                if ($sex === 'F') {
343
                    return I18N::translateContext('FEMALE', 'Ward');
344
                }
345
346
                return I18N::translate('Ward');
347
348
            case 'witness':
349
                // Do we need separate male/female translations for this?
350
                return I18N::translate('Witness');
351
352
            default:
353
                return I18N::translate($type);
354
        }
355
    }
356
357
    /**
358
     * A list of all possible values for RELA
359
     *
360
     * @param GedcomRecord|null $record
361
     *
362
     * @return array<string>
363
     */
364
    public static function getValues(GedcomRecord $record = null): array
365
    {
366
        $values = [];
367
        foreach (self::TYPES as $type) {
368
            $values[$type] = self::getValue($type, $record);
369
        }
370
        uasort($values, '\Fisharebest\Webtrees\I18N::strcasecmp');
371
372
        return $values;
373
    }
374
}
375