xref: /webtrees/app/SurnameTradition/PatrilinealSurnameTradition.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1323788f4SGreg Roach<?php
23976b470SGreg Roach
3323788f4SGreg Roach/**
4323788f4SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6323788f4SGreg Roach * This program is free software: you can redistribute it and/or modify
7323788f4SGreg Roach * it under the terms of the GNU General Public License as published by
8323788f4SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9323788f4SGreg Roach * (at your option) any later version.
10323788f4SGreg Roach * This program is distributed in the hope that it will be useful,
11323788f4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12323788f4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13323788f4SGreg Roach * GNU General Public License for more details.
14323788f4SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16323788f4SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20323788f4SGreg Roachnamespace Fisharebest\Webtrees\SurnameTradition;
21323788f4SGreg Roach
22cb7a42eaSGreg Roachuse Fisharebest\Webtrees\Individual;
23cb7a42eaSGreg Roach
24323788f4SGreg Roach/**
25323788f4SGreg Roach * Children take their father’s surname.
26323788f4SGreg Roach */
275206405dSRico Sonntagclass PatrilinealSurnameTradition extends DefaultSurnameTradition
28c1010edaSGreg Roach{
29323788f4SGreg Roach    /**
30cb7a42eaSGreg Roach     * What name is given to a new child
31323788f4SGreg Roach     *
32cb7a42eaSGreg Roach     * @param Individual|null $father
33cb7a42eaSGreg Roach     * @param Individual|null $mother
34cb7a42eaSGreg Roach     * @param string          $sex
35323788f4SGreg Roach     *
3601ffdfd0SGreg Roach     * @return array<int,string>
37323788f4SGreg Roach     */
38cb7a42eaSGreg Roach    public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array
39c1010edaSGreg Roach    {
40cb7a42eaSGreg Roach        if (preg_match(self::REGEX_SPFX_SURN, $this->extractName($father), $match)) {
41cb7a42eaSGreg Roach            $name = $match['NAME'];
42cb7a42eaSGreg Roach            $spfx = $match['SPFX'];
43cb7a42eaSGreg Roach            $surn = $match['SURN'];
44b2ce94c6SRico Sonntag
4513abd6f3SGreg Roach            return [
46cb7a42eaSGreg Roach                $this->buildName($name, ['TYPE' => 'birth', 'SPFX' => $spfx, 'SURN' => $surn]),
4713abd6f3SGreg Roach            ];
48323788f4SGreg Roach        }
49323788f4SGreg Roach
50cb7a42eaSGreg Roach        return parent::newChildNames($father, $mother, $sex);
51cb7a42eaSGreg Roach    }
52cb7a42eaSGreg Roach
53323788f4SGreg Roach    /**
54cb7a42eaSGreg Roach     * What name is given to a new parent
55323788f4SGreg Roach     *
56cb7a42eaSGreg Roach     * @param Individual $child
57cb7a42eaSGreg Roach     * @param string     $sex
58323788f4SGreg Roach     *
5901ffdfd0SGreg Roach     * @return array<int,string>
60323788f4SGreg Roach     */
61cb7a42eaSGreg Roach    public function newParentNames(Individual $child, string $sex): array
62c1010edaSGreg Roach    {
63cb7a42eaSGreg Roach        if ($sex === 'M' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($child), $match)) {
64cb7a42eaSGreg Roach            $name = $match['NAME'];
65cb7a42eaSGreg Roach            $spfx = $match['SPFX'];
66cb7a42eaSGreg Roach            $surn = $match['SURN'];
67b2ce94c6SRico Sonntag
6813abd6f3SGreg Roach            return [
69cb7a42eaSGreg Roach                $this->buildName($name, ['TYPE' => 'birth', 'SPFX' => $spfx, 'SURN' => $surn]),
7013abd6f3SGreg Roach            ];
71323788f4SGreg Roach        }
72323788f4SGreg Roach
73cb7a42eaSGreg Roach        return parent::newParentNames($child, $sex);
74cb7a42eaSGreg Roach    }
75cb7a42eaSGreg Roach
76323788f4SGreg Roach    /**
77323788f4SGreg Roach     * @param string               $name        A name
7824f2a3afSGreg Roach     * @param array<string,string> $inflections A list of inflections
79323788f4SGreg Roach     *
80323788f4SGreg Roach     * @return string An inflected name
81323788f4SGreg Roach     */
8224f2a3afSGreg Roach    protected function inflect(string $name, array $inflections): string
83c1010edaSGreg Roach    {
84323788f4SGreg Roach        foreach ($inflections as $from => $to) {
85323788f4SGreg Roach            $name = preg_replace('~' . $from . '~u', $to, $name);
86323788f4SGreg Roach        }
87323788f4SGreg Roach
88323788f4SGreg Roach        return $name;
89323788f4SGreg Roach    }
90323788f4SGreg Roach}
91