xref: /webtrees/app/SurnameTradition/PatrilinealSurnameTradition.php (revision db3aeb3eb846bb233f34557aedb1559aadf9275a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\SurnameTradition;
19
20/**
21 * Children take their father’s surname.
22 */
23class PatrilinealSurnameTradition extends DefaultSurnameTradition
24{
25    /**
26     * What names are given to a new child
27     *
28     * @param string $father_name A GEDCOM NAME
29     * @param string $mother_name A GEDCOM NAME
30     * @param string $child_sex   M, F or U
31     *
32     * @return string[] Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
33     */
34    public function newChildNames(string $father_name, string $mother_name, string $child_sex): array
35    {
36        if (preg_match(self::REGEX_SPFX_SURN, $father_name, $match)) {
37            return array_filter([
38                'NAME' => $match['NAME'],
39                'SPFX' => $match['SPFX'],
40                'SURN' => $match['SURN'],
41            ]);
42        }
43
44        return [
45            'NAME' => '//',
46        ];
47    }
48
49    /**
50     * What names are given to a new parent
51     *
52     * @param string $child_name A GEDCOM NAME
53     * @param string $parent_sex M, F or U
54     *
55     * @return string[] Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
56     */
57    public function newParentNames(string $child_name, string $parent_sex): array
58    {
59        if ($parent_sex === 'M' && preg_match(self::REGEX_SPFX_SURN, $child_name, $match)) {
60            return array_filter([
61                'NAME' => $match['NAME'],
62                'SPFX' => $match['SPFX'],
63                'SURN' => $match['SURN'],
64            ]);
65        }
66
67        return [
68            'NAME' => '//',
69        ];
70    }
71
72    /**
73     * @param string   $name        A name
74     * @param string[] $inflections A list of inflections
75     *
76     * @return string An inflected name
77     */
78    protected function inflect($name, $inflections): string
79    {
80        foreach ($inflections as $from => $to) {
81            $name = preg_replace('~' . $from . '~u', $to, $name);
82        }
83
84        return $name;
85    }
86}
87