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