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