Passed
Push — main ( 3dede0...c13598 )
by Greg
06:38
created

RoleInEvent::canonical()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 18
rs 9.9666
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\Elements;
21
22
use Fisharebest\Webtrees\I18N;
23
24
use function array_key_exists;
25
use function str_ends_with;
26
use function str_starts_with;
27
use function strtoupper;
28
29
/**
30
 * ROLE_IN_EVENT := {Size=1:15}
31
 * [ CHIL | HUSB | WIFE | MOTH | FATH | SPOU | (<ROLE_DESCRIPTOR>) ]
32
 * Indicates what role this person played in the event that is being cited in this context. For
33
 * example, if you cite a child's birth record as the source of the mother's name, the value for
34
 * this field is "MOTH." If you describe the groom of a marriage, the role is "HUSB." If the role
35
 * is something different than one of the six relationship role tags listed above then enclose the
36
 * role name within matching parentheses.
37
 */
38
class RoleInEvent extends AbstractElement
39
{
40
    protected const MAXIMUM_LENGTH = 15;
41
42
    /**
43
     * Convert a value to a canonical form.
44
     *
45
     * @param string $value
46
     *
47
     * @return string
48
     */
49
    public function canonical(string $value): string
50
    {
51
        $value = parent::canonical($value);
52
        $upper = strtoupper($value);
53
54
        if (array_key_exists($upper, $this->values())) {
55
            return $upper;
56
        }
57
58
        if (!str_starts_with($value, '(')) {
59
            $value = '(' . $value;
60
        }
61
62
        if (!str_ends_with($value, ')')) {
63
            return $value . ')';
64
        }
65
66
        return $value;
67
    }
68
69
    /**
70
     * A list of controlled values for this element
71
     *
72
     * @return array<int|string,string>
73
     */
74
    public function values(): array
75
    {
76
        return [
77
            ''     => '',
78
            'CHIL' => I18N::translate('child'),
79
            'HUSB' => I18N::translate('husband'),
80
            'WIFE' => I18N::translate('wife'),
81
            'MOTH' => I18N::translate('mother'),
82
            'FATH' => I18N::translate('father'),
83
            'SPOU' => I18N::translate('spouse'),
84
        ];
85
    }
86
}
87