xref: /webtrees/app/Elements/RoleInEvent.php (revision e873f434551745f888937263ff89e80db3b0f785)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Elements;
21
22use Fisharebest\Webtrees\I18N;
23
24use function array_key_exists;
25use function str_ends_with;
26use function str_starts_with;
27use 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 */
38class RoleInEvent extends AbstractElement
39{
40    protected const int 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