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