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