xref: /webtrees/app/SurnameTradition/PortugueseSurnameTradition.php (revision 0ec666a5adbb6e2ef4b6718d83fad4b2260317dc)
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
22use Fisharebest\Webtrees\Individual;
23
24/**
25 * Children take one surname from the mother and one surname from the father.
26 *
27 * Mother: Maria /AAAA/ /BBBB/
28 * Father: Jose  /CCCC/ /DDDD/
29 * Child:  Pablo /DDDD/ /BBBB/
30 */
31class PortugueseSurnameTradition extends DefaultSurnameTradition
32{
33    /**
34     * What name is given to a new child
35     *
36     * @param Individual|null $father
37     * @param Individual|null $mother
38     * @param string          $sex
39     *
40     * @return array<string>
41     */
42    public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array
43    {
44        if (preg_match(self::REGEX_SURNS, $this->extractName($father), $match_father)) {
45            $father_surname = $match_father['SURN2'];
46        } else {
47            $father_surname = '';
48        }
49
50        if (preg_match(self::REGEX_SURNS, $this->extractName($mother), $match_mother)) {
51            $mother_surname = $match_mother['SURN2'];
52        } else {
53            $mother_surname = '';
54        }
55
56        return [
57            $this->buildName('/' . $father_surname . '/ /' . $mother_surname . '/', [
58                'TYPE' => 'birth',
59                'SURN' => trim($father_surname . ',' . $mother_surname, ','),
60            ]),
61        ];
62    }
63
64    /**
65     * What name is given to a new parent
66     *
67     * @param Individual $child
68     * @param string     $sex
69     *
70     * @return array<string>
71     */
72    public function newParentNames(Individual $child, string $sex): array
73    {
74        if (preg_match(self::REGEX_SURNS, $this->extractName($child), $match)) {
75            switch ($sex) {
76                case 'M':
77                    return [
78                        $this->buildName('// /' . $match['SURN1'] . '/', [
79                            'TYPE' => 'birth',
80                            'SURN' => $match['SURN1'],
81                        ]),
82                    ];
83
84                case 'F':
85                    return [
86                        $this->buildName('// /' . $match['SURN2'] . '/', [
87                            'TYPE' => 'birth',
88                            'SURN' => $match['SURN2'],
89                        ]),
90                    ];
91            }
92        }
93
94        return [
95            $this->buildName('// //', ['TYPE' => 'birth']),
96        ];
97    }
98
99    /**
100     * What names are given to a new spouse
101     *
102     * @param Individual $spouse
103     * @param string     $sex
104     *
105     * @return array<string>
106     */
107    public function newSpouseNames(Individual $spouse, string $sex): array
108    {
109        return [
110            $this->buildName('// //', ['TYPE' => 'birth']),
111        ];
112    }
113}
114