xref: /webtrees/app/SurnameTradition/IcelandicSurnameTradition.php (revision 2978efea0c934a7f830579d20c3e8661e837b748)
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\SurnameTradition;
21
22use Fisharebest\Webtrees\Elements\NameType;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25
26/**
27 * Children take a patronym instead of a surname.
28 *
29 * Sons get their father’s given name plus “sson”
30 * Daughters get their father’s given name plus “sdottir”
31 */
32class IcelandicSurnameTradition extends DefaultSurnameTradition
33{
34    /**
35     * The name of this surname tradition
36     *
37     * @return string
38     */
39    public function name(): string
40    {
41        return I18N::translateContext('Surname tradition', 'Icelandic');
42    }
43
44    /**
45     * A short description of this surname tradition
46     *
47     * @return string
48     */
49    public function description(): string
50    {
51        /* I18N: In the Icelandic surname tradition, ... */
52        return I18N::translate('Children take a patronym instead of a surname.');
53    }
54
55    /**
56     * A default/empty name
57     *
58     * @return string
59     */
60    public function defaultName(): string
61    {
62        return '';
63    }
64
65    /**
66     * What name is given to a new child
67     *
68     * @param Individual|null $father
69     * @param Individual|null $mother
70     * @param string          $sex
71     *
72     * @return array<int,string>
73     */
74    public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array
75    {
76        if (preg_match(self::REGEX_GIVN, $this->extractName($father), $match) === 1) {
77            switch ($sex) {
78                case 'M':
79                    $givn = $match['GIVN'] . 'sson';
80
81                    return [
82                        $this->buildName($givn, ['TYPE' => NameType::VALUE_BIRTH, 'GIVN' => $givn]),
83                    ];
84
85                case 'F':
86                    $givn = $match['GIVN'] . 'sdottir';
87
88                    return [
89                        $this->buildName($givn, ['TYPE' => NameType::VALUE_BIRTH, 'GIVN' => $givn]),
90                    ];
91            }
92        }
93
94        return [
95            $this->buildName('', ['TYPE' => NameType::VALUE_BIRTH]),
96        ];
97    }
98
99    /**
100     * What name is given to a new parent
101     *
102     * @param Individual $child
103     * @param string     $sex
104     *
105     * @return array<int,string>
106     */
107    public function newParentNames(Individual $child, string $sex): array
108    {
109        if ($sex === 'M' && preg_match('~(?<GIVN>[^ /]+)(:?sson)$~', $this->extractName($child), $match) === 1) {
110            return [
111                $this->buildName($match['GIVN'], ['TYPE' => NameType::VALUE_BIRTH, 'GIVN' => $match['GIVN']]),
112            ];
113        }
114
115        if ($sex === 'F' && preg_match('~(?<GIVN>[^ /]+)(:?sdottir)$~', $this->extractName($child), $match) === 1) {
116            return [
117                $this->buildName($match['GIVN'], ['TYPE' => NameType::VALUE_BIRTH, 'GIVN' => $match['GIVN']]),
118            ];
119        }
120
121        return [
122            $this->buildName('', ['TYPE' => NameType::VALUE_BIRTH]),
123        ];
124    }
125
126    /**
127     * What names are given to a new spouse
128     *
129     * @param Individual $spouse
130     * @param string     $sex
131     *
132     * @return array<int,string>
133     */
134    public function newSpouseNames(Individual $spouse, string $sex): array
135    {
136        return [
137            $this->buildName('', ['TYPE' => NameType::VALUE_BIRTH]),
138        ];
139    }
140}
141