xref: /webtrees/app/SurnameTradition/PaternalSurnameTradition.php (revision 2ebcf907ed34213f816592af04e6c160335d6311)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\SurnameTradition;
21
22use Fisharebest\Webtrees\Individual;
23
24/**
25 * Children take their father’s surname. Wives take their husband’s surname.
26 */
27class PaternalSurnameTradition extends PatrilinealSurnameTradition
28{
29    /**
30     * Does this surname tradition change surname at marriage?
31     *
32     * @return bool
33     */
34    public function hasMarriedNames(): bool
35    {
36        return true;
37    }
38
39    /**
40     * What name is given to a new parent
41     *
42     * @param Individual $child
43     * @param string                           $sex
44     *
45     * @return array<int,string>
46     */
47    public function newParentNames(Individual $child, string $sex): array
48    {
49        if ($sex === 'F' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($child), $match)) {
50            $name = $match['NAME'];
51            $spfx = $match['SPFX'];
52            $surn = $match['SURN'];
53
54            return [
55                $this->buildName('//', ['TYPE' => 'birth']),
56                $this->buildName($name, ['TYPE' => 'married', 'SPFX' => $spfx, 'SURN' => $surn]),
57            ];
58        }
59
60        return parent::newParentNames($child, $sex);
61    }
62
63    /**
64     * What names are given to a new spouse
65     *
66     * @param Individual $spouse
67     * @param string                           $sex
68     *
69     * @return array<int,string>
70     */
71    public function newSpouseNames(Individual $spouse, string $sex): array
72    {
73        if ($sex === 'F' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($spouse), $match)) {
74            $name = $match['NAME'];
75            $spfx = $match['SPFX'];
76            $surn = $match['SURN'];
77
78            return [
79                $this->buildName('//', ['TYPE' => 'birth']),
80                $this->buildName($name, ['TYPE' => 'married', 'SPFX' => $spfx, 'SURN' => $surn]),
81            ];
82        }
83
84        return parent::newSpouseNames($spouse, $sex);
85    }
86}
87