xref: /webtrees/app/SurnameTradition/SpanishSurnameTradition.php (revision 67994fb087e1b24564a780e4ae8aeff801733e35)
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 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\SurnameTradition;
19
20/**
21 * Children take one surname from the father and one surname from the mother.
22 *
23 * Mother: Maria /AAAA/ /BBBB/
24 * Father: Jose  /CCCC/ /DDDD/
25 * Child:  Pablo /CCCC/ /AAAA/
26 */
27class SpanishSurnameTradition extends DefaultSurnameTradition implements SurnameTraditionInterface
28{
29    /**
30     * What names are given to a new child
31     *
32     * @param string $father_name A GEDCOM NAME
33     * @param string $mother_name A GEDCOM NAME
34     * @param string $child_sex   M, F or U
35     *
36     * @return string[] Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
37     */
38    public function newChildNames(string $father_name, string $mother_name, string $child_sex): array
39    {
40        if (preg_match(self::REGEX_SURNS, $father_name, $match_father)) {
41            $father_surname = $match_father['SURN1'];
42        } else {
43            $father_surname = '';
44        }
45
46        if (preg_match(self::REGEX_SURNS, $mother_name, $match_mother)) {
47            $mother_surname = $match_mother['SURN1'];
48        } else {
49            $mother_surname = '';
50        }
51
52        return [
53            'NAME' => '/' . $father_surname . '/ /' . $mother_surname . '/',
54            'SURN' => trim($father_surname . ',' . $mother_surname, ','),
55        ];
56    }
57
58    /**
59     * What names are given to a new parent
60     *
61     * @param string $child_name A GEDCOM NAME
62     * @param string $parent_sex M, F or U
63     *
64     * @return string[] Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
65     */
66    public function newParentNames(string $child_name, string $parent_sex): array
67    {
68        if (preg_match(self::REGEX_SURNS, $child_name, $match)) {
69            switch ($parent_sex) {
70                case 'M':
71                    return [
72                        'NAME' => '/' . $match['SURN1'] . '/ //',
73                        'SURN' => $match['SURN1'],
74                    ];
75                case 'F':
76                    return [
77                        'NAME' => '/' . $match['SURN2'] . '/ //',
78                        'SURN' => $match['SURN2'],
79                    ];
80            }
81        }
82
83        return [
84            'NAME' => '// //',
85        ];
86    }
87
88    /**
89     * What names are given to a new spouse
90     *
91     * @param string $spouse_name A GEDCOM NAME
92     * @param string $spouse_sex  M, F or U
93     *
94     * @return string[] Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
95     */
96    public function newSpouseNames(string $spouse_name, string $spouse_sex): array
97    {
98        return [
99            'NAME' => '// //',
100        ];
101    }
102}
103