xref: /webtrees/app/SurnameTradition/PatrilinealSurnameTradition.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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 their father’s surname.
28 */
29class PatrilinealSurnameTradition extends DefaultSurnameTradition
30{
31    /**
32     * The name of this surname tradition
33     *
34     * @return string
35     */
36    public function name(): string
37    {
38        /* I18N: A system where children take their father’s surname */
39        return I18N::translate('patrilineal');
40    }
41
42    /**
43     * A short description of this surname tradition
44     *
45     * @return string
46     */
47    public function description(): string
48    {
49        /* I18N: In the patrilineal surname tradition, ... */
50        return I18N::translate('Children take their father’s surname.');
51    }
52
53    /**
54     * What name is given to a new child
55     *
56     * @param Individual|null $father
57     * @param Individual|null $mother
58     * @param string          $sex
59     *
60     * @return array<int,string>
61     */
62    public function newChildNames(Individual|null $father, Individual|null $mother, string $sex): array
63    {
64        if (preg_match(self::REGEX_SPFX_SURN, $this->extractName($father), $match) === 1) {
65            $name = $match['NAME'];
66            $spfx = $match['SPFX'];
67            $surn = $match['SURN'];
68
69            return [
70                $this->buildName($name, ['TYPE' => NameType::VALUE_BIRTH, 'SPFX' => $spfx, 'SURN' => $surn]),
71            ];
72        }
73
74        return parent::newChildNames($father, $mother, $sex);
75    }
76
77    /**
78     * What name is given to a new parent
79     *
80     * @param Individual $child
81     * @param string     $sex
82     *
83     * @return array<int,string>
84     */
85    public function newParentNames(Individual $child, string $sex): array
86    {
87        if ($sex === 'M' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($child), $match) === 1) {
88            $name = $match['NAME'];
89            $spfx = $match['SPFX'];
90            $surn = $match['SURN'];
91
92            return [
93                $this->buildName($name, ['TYPE' => NameType::VALUE_BIRTH, 'SPFX' => $spfx, 'SURN' => $surn]),
94            ];
95        }
96
97        return parent::newParentNames($child, $sex);
98    }
99
100    /**
101     * @param string               $name        A name
102     * @param array<string,string> $inflections A list of inflections
103     *
104     * @return string An inflected name
105     */
106    protected function inflect(string $name, array $inflections): string
107    {
108        foreach ($inflections as $from => $to) {
109            $name = preg_replace('~' . $from . '~u', $to, $name);
110        }
111
112        return $name;
113    }
114}
115