xref: /webtrees/app/SurnameTradition/PatrilinealSurnameTradition.php (revision 842a873c0b92c28d4af3fa9db0e9156057005569)
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.
24 */
25class PatrilinealSurnameTradition extends DefaultSurnameTradition
26{
27    /**
28     * What names are given to a new child
29     *
30     * @param string $father_name A GEDCOM NAME
31     * @param string $mother_name A GEDCOM NAME
32     * @param string $child_sex   M, F or U
33     *
34     * @return string[] Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
35     */
36    public function newChildNames(string $father_name, string $mother_name, string $child_sex): array
37    {
38        if (preg_match(self::REGEX_SPFX_SURN, $father_name, $match)) {
39            return array_filter([
40                'NAME' => $match['NAME'],
41                'SPFX' => $match['SPFX'],
42                'SURN' => $match['SURN'],
43            ]);
44        }
45
46        return [
47            'NAME' => '//',
48        ];
49    }
50
51    /**
52     * What names are given to a new parent
53     *
54     * @param string $child_name A GEDCOM NAME
55     * @param string $parent_sex M, F or U
56     *
57     * @return string[] Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
58     */
59    public function newParentNames(string $child_name, string $parent_sex): array
60    {
61        if ($parent_sex === 'M' && preg_match(self::REGEX_SPFX_SURN, $child_name, $match)) {
62            return array_filter([
63                'NAME' => $match['NAME'],
64                'SPFX' => $match['SPFX'],
65                'SURN' => $match['SURN'],
66            ]);
67        }
68
69        return [
70            'NAME' => '//',
71        ];
72    }
73
74    /**
75     * @param string   $name        A name
76     * @param string[] $inflections A list of inflections
77     *
78     * @return string An inflected name
79     */
80    protected function inflect($name, $inflections): string
81    {
82        foreach ($inflections as $from => $to) {
83            $name = preg_replace('~' . $from . '~u', $to, $name);
84        }
85
86        return $name;
87    }
88}
89