1323788f4SGreg Roach<?php 23976b470SGreg Roach 3323788f4SGreg Roach/** 4323788f4SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 6323788f4SGreg Roach * This program is free software: you can redistribute it and/or modify 7323788f4SGreg Roach * it under the terms of the GNU General Public License as published by 8323788f4SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9323788f4SGreg Roach * (at your option) any later version. 10323788f4SGreg Roach * This program is distributed in the hope that it will be useful, 11323788f4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12323788f4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13323788f4SGreg Roach * GNU General Public License for more details. 14323788f4SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16323788f4SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 20323788f4SGreg Roachnamespace Fisharebest\Webtrees\SurnameTradition; 21323788f4SGreg Roach 227c29ac65SGreg Roachuse Fisharebest\Webtrees\Elements\NameType; 23*7e128bbfSGreg Roachuse Fisharebest\Webtrees\I18N; 24cb7a42eaSGreg Roachuse Fisharebest\Webtrees\Individual; 25cb7a42eaSGreg Roach 26323788f4SGreg Roach/** 27323788f4SGreg Roach * Children take their father’s surname. 28323788f4SGreg Roach */ 295206405dSRico Sonntagclass PatrilinealSurnameTradition extends DefaultSurnameTradition 30c1010edaSGreg Roach{ 31323788f4SGreg Roach /** 32*7e128bbfSGreg Roach * The name of this surname tradition 33*7e128bbfSGreg Roach * 34*7e128bbfSGreg Roach * @return string 35*7e128bbfSGreg Roach */ 36*7e128bbfSGreg Roach public function name(): string 37*7e128bbfSGreg Roach { 38*7e128bbfSGreg Roach /* I18N: A system where children take their father’s surname */ 39*7e128bbfSGreg Roach return I18N::translate('patrilineal'); 40*7e128bbfSGreg Roach } 41*7e128bbfSGreg Roach 42*7e128bbfSGreg Roach /** 43*7e128bbfSGreg Roach * A short description of this surname tradition 44*7e128bbfSGreg Roach * 45*7e128bbfSGreg Roach * @return string 46*7e128bbfSGreg Roach */ 47*7e128bbfSGreg Roach public function description(): string 48*7e128bbfSGreg Roach { 49*7e128bbfSGreg Roach /* I18N: In the patrilineal surname tradition, ... */ 50*7e128bbfSGreg Roach return I18N::translate('Children take their father’s surname.'); 51*7e128bbfSGreg Roach } 52*7e128bbfSGreg Roach 53*7e128bbfSGreg Roach /** 54cb7a42eaSGreg Roach * What name is given to a new child 55323788f4SGreg Roach * 56cb7a42eaSGreg Roach * @param Individual|null $father 57cb7a42eaSGreg Roach * @param Individual|null $mother 58cb7a42eaSGreg Roach * @param string $sex 59323788f4SGreg Roach * 6001ffdfd0SGreg Roach * @return array<int,string> 61323788f4SGreg Roach */ 62cb7a42eaSGreg Roach public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array 63c1010edaSGreg Roach { 64cb7a42eaSGreg Roach if (preg_match(self::REGEX_SPFX_SURN, $this->extractName($father), $match)) { 65cb7a42eaSGreg Roach $name = $match['NAME']; 66cb7a42eaSGreg Roach $spfx = $match['SPFX']; 67cb7a42eaSGreg Roach $surn = $match['SURN']; 68b2ce94c6SRico Sonntag 6913abd6f3SGreg Roach return [ 7088a03560SGreg Roach $this->buildName($name, ['TYPE' => NameType::VALUE_BIRTH, 'SPFX' => $spfx, 'SURN' => $surn]), 7113abd6f3SGreg Roach ]; 72323788f4SGreg Roach } 73323788f4SGreg Roach 74cb7a42eaSGreg Roach return parent::newChildNames($father, $mother, $sex); 75cb7a42eaSGreg Roach } 76cb7a42eaSGreg Roach 77323788f4SGreg Roach /** 78cb7a42eaSGreg Roach * What name is given to a new parent 79323788f4SGreg Roach * 80cb7a42eaSGreg Roach * @param Individual $child 81cb7a42eaSGreg Roach * @param string $sex 82323788f4SGreg Roach * 8301ffdfd0SGreg Roach * @return array<int,string> 84323788f4SGreg Roach */ 85cb7a42eaSGreg Roach public function newParentNames(Individual $child, string $sex): array 86c1010edaSGreg Roach { 87cb7a42eaSGreg Roach if ($sex === 'M' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($child), $match)) { 88cb7a42eaSGreg Roach $name = $match['NAME']; 89cb7a42eaSGreg Roach $spfx = $match['SPFX']; 90cb7a42eaSGreg Roach $surn = $match['SURN']; 91b2ce94c6SRico Sonntag 9213abd6f3SGreg Roach return [ 9388a03560SGreg Roach $this->buildName($name, ['TYPE' => NameType::VALUE_BIRTH, 'SPFX' => $spfx, 'SURN' => $surn]), 9413abd6f3SGreg Roach ]; 95323788f4SGreg Roach } 96323788f4SGreg Roach 97cb7a42eaSGreg Roach return parent::newParentNames($child, $sex); 98cb7a42eaSGreg Roach } 99cb7a42eaSGreg Roach 100323788f4SGreg Roach /** 101323788f4SGreg Roach * @param string $name A name 10224f2a3afSGreg Roach * @param array<string,string> $inflections A list of inflections 103323788f4SGreg Roach * 104323788f4SGreg Roach * @return string An inflected name 105323788f4SGreg Roach */ 10624f2a3afSGreg Roach protected function inflect(string $name, array $inflections): string 107c1010edaSGreg Roach { 108323788f4SGreg Roach foreach ($inflections as $from => $to) { 109323788f4SGreg Roach $name = preg_replace('~' . $from . '~u', $to, $name); 110323788f4SGreg Roach } 111323788f4SGreg Roach 112323788f4SGreg Roach return $name; 113323788f4SGreg Roach } 114323788f4SGreg Roach} 115