Passed
Push — dev ( 9fabb1...8976f1 )
by Greg
06:13
created

IcelandicSurnameTradition::defaultName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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\SurnameTradition;
21
22
use Fisharebest\Webtrees\Individual;
23
24
/**
25
 * Children take a patronym instead of a surname.
26
 *
27
 * Sons get their father’s given name plus “sson”
28
 * Daughters get their father’s given name plus “sdottir”
29
 */
30
class IcelandicSurnameTradition extends DefaultSurnameTradition
31
{
32
    /**
33
     * A default/empty name
34
     *
35
     * @return string
36
     */
37
    public function defaultName(): string
38
    {
39
        return '';
40
    }
41
42
    /**
43
     * What name is given to a new child
44
     *
45
     * @param Individual|null $father
46
     * @param Individual|null $mother
47
     * @param string          $sex
48
     *
49
     * @return array<int,string>
50
     */
51
    public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array
52
    {
53
        if (preg_match(self::REGEX_GIVN, $this->extractName($father), $match)) {
54
            switch ($sex) {
55
                case 'M':
56
                    $givn = $match['GIVN'] . 'sson';
57
58
                    return [
59
                        $this->buildName($givn, ['TYPE' => 'birth', 'GIVN' => $givn]),
60
                    ];
61
62
                case 'F':
63
                    $givn = $match['GIVN'] . 'sdottir';
64
65
                    return [
66
                        $this->buildName($givn, ['TYPE' => 'birth', 'GIVN' => $givn]),
67
                    ];
68
            }
69
        }
70
71
        return [
72
            $this->buildName('', ['TYPE' => 'birth']),
73
        ];
74
    }
75
76
    /**
77
     * What name is given to a new parent
78
     *
79
     * @param Individual $child
80
     * @param string     $sex
81
     *
82
     * @return array<int,string>
83
     */
84
    public function newParentNames(Individual $child, string $sex): array
85
    {
86
        if ($sex === 'M' && preg_match('~(?<GIVN>[^ /]+)(:?sson)$~', $this->extractName($child), $match)) {
87
            return [
88
                $this->buildName($match['GIVN'], ['TYPE' => 'birth', 'GIVN' => $match['GIVN']]),
89
            ];
90
        }
91
92
        if ($sex === 'F' && preg_match('~(?<GIVN>[^ /]+)(:?sdottir)$~', $this->extractName($child), $match)) {
93
            return [
94
                $this->buildName($match['GIVN'], ['TYPE' => 'birth', 'GIVN' => $match['GIVN']]),
95
            ];
96
        }
97
98
        return [
99
            $this->buildName('', ['TYPE' => 'birth']),
100
        ];
101
    }
102
103
    /**
104
     * What names are given to a new spouse
105
     *
106
     * @param Individual $spouse
107
     * @param string     $sex
108
     *
109
     * @return array<int,string>
110
     */
111
    public function newSpouseNames(Individual $spouse, string $sex): array
112
    {
113
        return [
114
            $this->buildName('', ['TYPE' => 'birth']),
115
        ];
116
    }
117
}
118