xref: /webtrees/app/SurnameTradition/PolishSurnameTradition.php (revision 0d047a8c74753c7558a60f4789c838996d6fae8b)
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
22/**
23 * Children take their father’s surname. Wives take their husband’s surname. Surnames are inflected to indicate an individual’s sex.
24 */
25class PolishSurnameTradition extends PaternalSurnameTradition
26{
27    // Inflect a surname for females
28    private const INFLECT_FEMALE = [
29        'cki\b'  => 'cka',
30        'dzki\b' => 'dzka',
31        'ski\b'  => 'ska',
32        'żki\b'  => 'żka',
33    ];
34
35    // Inflect a surname for males
36    private const INFLECT_MALE = [
37        'cka\b'  => 'cki',
38        'dzka\b' => 'dzki',
39        'ska\b'  => 'ski',
40        'żka\b'  => 'żki',
41    ];
42
43    /**
44     * What names are given to a new child
45     *
46     * @param string $father_name A GEDCOM NAME
47     * @param string $mother_name A GEDCOM NAME
48     * @param string $child_sex   M, F or U
49     *
50     * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
51     */
52    public function newChildNames(string $father_name, string $mother_name, string $child_sex): array
53    {
54        if (preg_match(self::REGEX_SURN, $father_name, $match)) {
55            if ($child_sex === 'F') {
56                return array_filter([
57                    'NAME' => $this->inflect($match['NAME'], self::INFLECT_FEMALE),
58                    'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE),
59                ]);
60            }
61
62            return array_filter([
63                'NAME' => $this->inflect($match['NAME'], self::INFLECT_MALE),
64                'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE),
65            ]);
66        }
67
68        return [
69            'NAME' => '//',
70        ];
71    }
72
73    /**
74     * What names are given to a new parent
75     *
76     * @param string $child_name A GEDCOM NAME
77     * @param string $parent_sex M, F or U
78     *
79     * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
80     */
81    public function newParentNames(string $child_name, string $parent_sex): array
82    {
83        if ($parent_sex === 'M' && preg_match(self::REGEX_SURN, $child_name, $match)) {
84            return array_filter([
85                'NAME' => $this->inflect($match['NAME'], self::INFLECT_MALE),
86                'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE),
87            ]);
88        }
89
90        return [
91            'NAME' => '//',
92        ];
93    }
94
95    /**
96     * What names are given to a new spouse
97     *
98     * @param string $spouse_name A GEDCOM NAME
99     * @param string $spouse_sex  M, F or U
100     *
101     * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.)
102     */
103    public function newSpouseNames(string $spouse_name, string $spouse_sex): array
104    {
105        if ($spouse_sex === 'F' && preg_match(self::REGEX_SURN, $spouse_name, $match)) {
106            return [
107                'NAME'   => '//',
108                '_MARNM' => $this->inflect($match['NAME'], self::INFLECT_FEMALE),
109            ];
110        }
111
112        return [
113            'NAME' => '//',
114        ];
115    }
116}
117