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