xref: /webtrees/app/SurnameTradition/PaternalSurnameTradition.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
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
22/**
23 * Children take their father’s surname. Wives take their husband’s surname.
24 */
25class PaternalSurnameTradition extends PatrilinealSurnameTradition
26{
27    /**
28     * Does this surname tradition change surname at marriage?
29     *
30     * @return bool
31     */
32    public function hasMarriedNames(): bool
33    {
34        return true;
35    }
36
37    /**
38     * What names are given to a new parent
39     *
40     * @param string $child_name A GEDCOM NAME
41     * @param string $parent_sex M, F or U
42     *
43     * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
44     */
45    public function newParentNames(string $child_name, string $parent_sex): array
46    {
47        if (preg_match(self::REGEX_SPFX_SURN, $child_name, $match)) {
48            switch ($parent_sex) {
49                case 'M':
50                    return array_filter([
51                        'NAME' => $match['NAME'],
52                        'SPFX' => $match['SPFX'],
53                        'SURN' => $match['SURN'],
54                    ]);
55                case 'F':
56                    return [
57                        'NAME'   => '//',
58                        '_MARNM' => '/' . trim($match['SPFX'] . ' ' . $match['SURN']) . '/',
59                    ];
60            }
61        }
62
63        return [
64            'NAME' => '//',
65        ];
66    }
67
68    /**
69     * What names are given to a new spouse
70     *
71     * @param string $spouse_name A GEDCOM NAME
72     * @param string $spouse_sex  M, F or U
73     *
74     * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
75     */
76    public function newSpouseNames(string $spouse_name, string $spouse_sex): array
77    {
78        if ($spouse_sex === 'F' && preg_match(self::REGEX_SURN, $spouse_name, $match)) {
79            return [
80                'NAME'   => '//',
81                '_MARNM' => $match['NAME'],
82            ];
83        }
84
85        return [
86            'NAME' => '//',
87        ];
88    }
89}
90