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