diff options
| author | Greg Roach <greg@subaqua.co.uk> | 2021-06-04 09:47:31 +0100 |
|---|---|---|
| committer | Greg Roach <greg@subaqua.co.uk> | 2021-06-04 19:54:41 +0100 |
| commit | cb7a42eae1efabac96d9d7693151fe0421b6717b (patch) | |
| tree | 377965c6d7e32fea6118a4f9fda815ed5692c8b2 /app | |
| parent | 2d111ef4d7fa5c4d9d27b419b54419f20e0c57e9 (diff) | |
| download | webtrees-cb7a42eae1efabac96d9d7693151fe0421b6717b.tar.gz webtrees-cb7a42eae1efabac96d9d7693151fe0421b6717b.tar.bz2 webtrees-cb7a42eae1efabac96d9d7693151fe0421b6717b.zip | |
Update surname tradition code to use TYPE=married instead of _MARNM
Diffstat (limited to 'app')
| -rw-r--r-- | app/Http/RequestHandlers/AddChildToFamilyPage.php | 10 | ||||
| -rw-r--r-- | app/Http/RequestHandlers/AddChildToIndividualPage.php | 29 | ||||
| -rw-r--r-- | app/Http/RequestHandlers/AddParentToIndividualPage.php | 11 | ||||
| -rw-r--r-- | app/Http/RequestHandlers/AddSpouseToFamilyPage.php | 15 | ||||
| -rw-r--r-- | app/Http/RequestHandlers/AddSpouseToIndividualPage.php | 11 | ||||
| -rw-r--r-- | app/SurnameTradition/DefaultSurnameTradition.php | 92 | ||||
| -rw-r--r-- | app/SurnameTradition/IcelandicSurnameTradition.php | 68 | ||||
| -rw-r--r-- | app/SurnameTradition/LithuanianSurnameTradition.php | 79 | ||||
| -rw-r--r-- | app/SurnameTradition/MatrilinealSurnameTradition.php | 60 | ||||
| -rw-r--r-- | app/SurnameTradition/PaternalSurnameTradition.php | 61 | ||||
| -rw-r--r-- | app/SurnameTradition/PatrilinealSurnameTradition.php | 60 | ||||
| -rw-r--r-- | app/SurnameTradition/PolishSurnameTradition.php | 83 | ||||
| -rw-r--r-- | app/SurnameTradition/PortugueseSurnameTradition.php | 63 | ||||
| -rw-r--r-- | app/SurnameTradition/SpanishSurnameTradition.php | 63 | ||||
| -rw-r--r-- | app/SurnameTradition/SurnameTraditionInterface.php | 32 |
15 files changed, 446 insertions, 291 deletions
diff --git a/app/Http/RequestHandlers/AddChildToFamilyPage.php b/app/Http/RequestHandlers/AddChildToFamilyPage.php index f1eb504726..ee3dc3645f 100644 --- a/app/Http/RequestHandlers/AddChildToFamilyPage.php +++ b/app/Http/RequestHandlers/AddChildToFamilyPage.php @@ -24,6 +24,7 @@ use Fisharebest\Webtrees\Fact; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Registry; +use Fisharebest\Webtrees\SurnameTradition; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -62,9 +63,16 @@ class AddChildToFamilyPage implements RequestHandlerInterface // Create a dummy individual, so that we can create new/empty facts. $element = Registry::elementFactory()->make('INDI:NAME'); $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); - $facts = [ + + // Default names facts. + $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION')); + $names = $surname_tradition->newChildNames($family->husband(), $family->wife(), $sex); + $name_facts = array_map(fn (string $gedcom): Fact => new Fact($gedcom, $dummy, ''), $names); + + $facts = [ 'i' => [ new Fact('1 SEX ' . $sex, $dummy, ''), + ...$name_facts, new Fact('1 NAME ' . $element->default($tree), $dummy, ''), new Fact('1 BIRT', $dummy, ''), new Fact('1 DEAT', $dummy, ''), diff --git a/app/Http/RequestHandlers/AddChildToIndividualPage.php b/app/Http/RequestHandlers/AddChildToIndividualPage.php index 662d165b87..09e2fa43b5 100644 --- a/app/Http/RequestHandlers/AddChildToIndividualPage.php +++ b/app/Http/RequestHandlers/AddChildToIndividualPage.php @@ -24,11 +24,13 @@ use Fisharebest\Webtrees\Fact; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Registry; +use Fisharebest\Webtrees\SurnameTradition; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use function array_map; use function assert; use function is_string; use function route; @@ -57,12 +59,31 @@ class AddChildToIndividualPage implements RequestHandlerInterface $individual = Auth::checkIndividualAccess($individual, true); // Create a dummy individual, so that we can create new/empty facts. - $element = Registry::elementFactory()->make('INDI:NAME'); - $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); - $facts = [ + $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); + + // Default names facts. + $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION')); + + switch ($individual->sex()) { + case 'M': + $names = $surname_tradition->newChildNames($individual, null, 'U'); + break; + + case 'F': + $names = $surname_tradition->newChildNames(null, $individual, 'U'); + break; + + default: + $names = $surname_tradition->newChildNames(null, null, 'U'); + break; + } + + $name_facts = array_map(fn (string $gedcom): Fact => new Fact($gedcom, $dummy, ''), $names); + + $facts = [ 'i' => [ new Fact('1 SEX ', $dummy, ''), - new Fact('1 NAME ' . $element->default($tree), $dummy, ''), + ...$name_facts, new Fact('1 BIRT', $dummy, ''), new Fact('1 DEAT', $dummy, ''), ], diff --git a/app/Http/RequestHandlers/AddParentToIndividualPage.php b/app/Http/RequestHandlers/AddParentToIndividualPage.php index d44f29ebea..9b8e6d4f28 100644 --- a/app/Http/RequestHandlers/AddParentToIndividualPage.php +++ b/app/Http/RequestHandlers/AddParentToIndividualPage.php @@ -24,11 +24,13 @@ use Fisharebest\Webtrees\Fact; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Registry; +use Fisharebest\Webtrees\SurnameTradition; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use function array_map; use function assert; use function is_string; use function route; @@ -60,12 +62,17 @@ class AddParentToIndividualPage implements RequestHandlerInterface $individual = Auth::checkIndividualAccess($individual, true); // Create a dummy individual, so that we can create new/empty facts. - $element = Registry::elementFactory()->make('INDI:NAME'); $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); + + // Default names facts. + $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION')); + $names = $surname_tradition->newParentNames($individual, $sex); + $name_facts = array_map(fn (string $gedcom): Fact => new Fact($gedcom, $dummy, ''), $names); + $facts = [ 'i' => [ new Fact('1 SEX ' . $sex, $dummy, ''), - new Fact('1 NAME ' . $element->default($tree), $dummy, ''), + ...$name_facts, new Fact('1 BIRT', $dummy, ''), new Fact('1 DEAT', $dummy, ''), ], diff --git a/app/Http/RequestHandlers/AddSpouseToFamilyPage.php b/app/Http/RequestHandlers/AddSpouseToFamilyPage.php index 9086fa6e69..9442e3119e 100644 --- a/app/Http/RequestHandlers/AddSpouseToFamilyPage.php +++ b/app/Http/RequestHandlers/AddSpouseToFamilyPage.php @@ -23,12 +23,15 @@ use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Fact; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; +use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Registry; +use Fisharebest\Webtrees\SurnameTradition; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use function array_map; use function assert; use function is_string; use function route; @@ -63,10 +66,18 @@ class AddSpouseToFamilyPage implements RequestHandlerInterface $element = Registry::elementFactory()->make('INDI:NAME'); $dummyi = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); $dummyf = Registry::familyFactory()->new('', '0 @@ FAM', null, $tree); - $facts = [ + + // Default names facts. + $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION')); + $spouse = $family->spouses()->first(); + assert($spouse instanceof Individual); + $names = $surname_tradition->newSpouseNames($spouse, $sex); + $name_facts = array_map(fn (string $gedcom): Fact => new Fact($gedcom, $dummyi, ''), $names); + + $facts = [ 'i' => [ new Fact('1 SEX ' . $sex, $dummyi, ''), - new Fact('1 NAME ' . $element->default($tree), $dummyi, ''), + ...$name_facts, new Fact('1 BIRT', $dummyi, ''), new Fact('1 DEAT', $dummyi, ''), ], diff --git a/app/Http/RequestHandlers/AddSpouseToIndividualPage.php b/app/Http/RequestHandlers/AddSpouseToIndividualPage.php index 09ecedfa6a..80d61b5907 100644 --- a/app/Http/RequestHandlers/AddSpouseToIndividualPage.php +++ b/app/Http/RequestHandlers/AddSpouseToIndividualPage.php @@ -24,11 +24,13 @@ use Fisharebest\Webtrees\Fact; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Registry; +use Fisharebest\Webtrees\SurnameTradition; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use function array_map; use function assert; use function is_string; use function route; @@ -64,13 +66,18 @@ class AddSpouseToIndividualPage implements RequestHandlerInterface // Create a dummy individual, so that we can create new/empty facts. $sex = self::OPPOSITE_SEX[$individual->sex()] ?? 'U'; - $element = Registry::elementFactory()->make('INDI:NAME'); $dummyi = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); $dummyf = Registry::familyFactory()->new('', '0 @@ FAM', null, $tree); + + // Default names facts. + $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION')); + $names = $surname_tradition->newSpouseNames($individual, $sex); + $name_facts = array_map(fn (string $gedcom): Fact => new Fact($gedcom, $dummyi, ''), $names); + $facts = [ 'i' => [ new Fact('1 SEX ' . $sex, $dummyi, ''), - new Fact('1 NAME ' . $element->default($tree), $dummyi, ''), + ...$name_facts, new Fact('1 BIRT', $dummyi, ''), new Fact('1 DEAT', $dummyi, ''), ], diff --git a/app/SurnameTradition/DefaultSurnameTradition.php b/app/SurnameTradition/DefaultSurnameTradition.php index 01e10a0696..a812f8a752 100644 --- a/app/SurnameTradition/DefaultSurnameTradition.php +++ b/app/SurnameTradition/DefaultSurnameTradition.php @@ -19,6 +19,15 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Fact; +use Fisharebest\Webtrees\Individual; + +use function array_filter; +use function array_keys; +use function array_map; +use function implode; +use function in_array; + /** * All family members keep their original surname */ @@ -57,48 +66,95 @@ class DefaultSurnameTradition implements SurnameTraditionInterface } /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { return [ - 'NAME' => '//', + $this->buildName('//', ['TYPE' => 'birth']), ]; } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { return [ - 'NAME' => '//', + $this->buildName('//', ['TYPE' => 'birth']), ]; } /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array + public function newSpouseNames(Individual $spouse, string $sex): array { return [ - 'NAME' => '//', + $this->buildName('//', ['TYPE' => 'birth']), ]; } + + /** + * Build a GEDCOM name record + * + * @param string $name + * @param array<string,string> $parts + * + * @return string + */ + protected function buildName(string $name, array $parts): string + { + $parts = array_filter($parts); + + $parts = array_map( + fn (string $tag, string $value): string => "\n2 " . $tag . ' ' . $value, + array_keys($parts), + $parts + ); + + if ($name === '') { + return '1 NAME' . implode($parts); + } + + return '1 NAME ' . $name . implode($parts); + } + + /** + * Extract an individual's name. + * + * @param Individual|null $individual + * + * @return string + */ + protected function extractName(?Individual $individual): string + { + if ($individual instanceof Individual) { + $fact = $individual + ->facts(['NAME']) + ->first(fn (Fact $fact): bool => in_array($fact->attribute('TYPE'), ['', 'birth', 'change'], true)); + + if ($fact instanceof Fact) { + return $fact->value(); + } + } + + return ''; + } } diff --git a/app/SurnameTradition/IcelandicSurnameTradition.php b/app/SurnameTradition/IcelandicSurnameTradition.php index 6b02b46ea9..ffe3b2aaa1 100644 --- a/app/SurnameTradition/IcelandicSurnameTradition.php +++ b/app/SurnameTradition/IcelandicSurnameTradition.php @@ -19,6 +19,8 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Children take a patronym instead of a surname. * @@ -38,62 +40,78 @@ class IcelandicSurnameTradition extends DefaultSurnameTradition } /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { - if (preg_match(self::REGEX_GIVN, $father_name, $father_match)) { - switch ($child_sex) { + if (preg_match(self::REGEX_GIVN, $this->extractName($father), $match)) { + switch ($sex) { case 'M': + $givn = $match['GIVN'] . 'sson'; + return [ - 'NAME' => $father_match['GIVN'] . 'sson', + $this->buildName($givn, ['TYPE' => 'birth', 'GIVN' => $givn]), ]; + case 'F': + $givn = $match['GIVN'] . 'sdottir'; + return [ - 'NAME' => $father_match['GIVN'] . 'sdottir', + $this->buildName($givn, ['TYPE' => 'birth', 'GIVN' => $givn]), ]; } } - return []; + return [ + $this->buildName('', ['TYPE' => 'birth']), + ]; } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if ($parent_sex === 'M' && preg_match('~(?<GIVN>[^ /]+)(:?sson|sdottir)$~', $child_name, $child_match)) { + if ($sex === 'M' && preg_match('~(?<GIVN>[^ /]+)(:?sson)$~', $this->extractName($child), $match)) { + return [ + $this->buildName($match['GIVN'], ['TYPE' => 'birth', 'GIVN' => $match['GIVN']]), + ]; + } + + if ($sex === 'F' && preg_match('~(?<GIVN>[^ /]+)(:?sdottir)$~', $this->extractName($child), $match)) { return [ - 'NAME' => $child_match['GIVN'], - 'GIVN' => $child_match['GIVN'], + $this->buildName($match['GIVN'], ['TYPE' => 'birth', 'GIVN' => $match['GIVN']]), ]; } - return []; + return [ + $this->buildName('', ['TYPE' => 'birth']), + ]; } /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array + public function newSpouseNames(Individual $spouse, string $sex): array { - return []; + return [ + $this->buildName('', ['TYPE' => 'birth']), + ]; } } diff --git a/app/SurnameTradition/LithuanianSurnameTradition.php b/app/SurnameTradition/LithuanianSurnameTradition.php index ebecd52b31..fb01920a89 100644 --- a/app/SurnameTradition/LithuanianSurnameTradition.php +++ b/app/SurnameTradition/LithuanianSurnameTradition.php @@ -19,6 +19,8 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Lithuanian — Children take their father’s surname. Wives take their husband’s surname. Surnames are inflected to indicate an individual’s sex and marital status. */ @@ -51,76 +53,81 @@ class LithuanianSurnameTradition extends PaternalSurnameTradition ]; /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { - if (preg_match(self::REGEX_SURN, $father_name, $match)) { - if ($child_sex === 'F') { - return array_filter([ - 'NAME' => $this->inflect($match['NAME'], self::INFLECT_DAUGHTER), - 'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE), - ]); + if (preg_match(self::REGEX_SURN, $this->extractName($father), $match)) { + if ($sex === 'F') { + $name = $this->inflect($match['NAME'], self::INFLECT_DAUGHTER); + $surn = $this->inflect($match['SURN'], self::INFLECT_MALE); + } else { + $name = $match['NAME']; + $surn = $match['SURN']; } - return array_filter([ - 'NAME' => $match['NAME'], - 'SURN' => $match['SURN'], - ]); + return [ + $this->buildName($name, ['TYPE' => 'birth', 'SURN' => $surn]), + ]; } return [ - 'NAME' => '//', + $this->buildName('//', ['TYPE' => 'birth']), ]; } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if ($parent_sex === 'M' && preg_match(self::REGEX_SURN, $child_name, $match)) { - return array_filter([ - 'NAME' => $this->inflect($match['NAME'], self::INFLECT_MALE), - 'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE), - ]); + if ($sex === 'M' && preg_match(self::REGEX_SURN, $this->extractName($child), $match)) { + $name = $this->inflect($match['NAME'], self::INFLECT_MALE); + $surn = $this->inflect($match['SURN'], self::INFLECT_MALE); + + return [ + $this->buildName($name, ['TYPE' => 'birth', 'SURN' => $surn]), + ]; } return [ - 'NAME' => '//', + $this->buildName('//', ['TYPE' => 'birth']), ]; } /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array + public function newSpouseNames(Individual $spouse, string $sex): array { - if ($spouse_sex === 'F' && preg_match(self::REGEX_SURN, $spouse_name, $match)) { + if ($sex === 'F' && preg_match(self::REGEX_SURN, $this->extractName($spouse), $match)) { + $name = $this->inflect($match['NAME'], self::INFLECT_WIFE); + $surn = $this->inflect($match['SURN'], self::INFLECT_MALE); + return [ - 'NAME' => '//', - '_MARNM' => $this->inflect($match['NAME'], self::INFLECT_WIFE), + $this->buildName('//', ['TYPE' => 'birth']), + $this->buildName($name, ['TYPE' => 'married', 'SURN' => $surn]), ]; } return [ - 'NAME' => '//', + $this->buildName('//', ['TYPE' => 'birth']), ]; } } diff --git a/app/SurnameTradition/MatrilinealSurnameTradition.php b/app/SurnameTradition/MatrilinealSurnameTradition.php index 3f69957dce..7737facb94 100644 --- a/app/SurnameTradition/MatrilinealSurnameTradition.php +++ b/app/SurnameTradition/MatrilinealSurnameTradition.php @@ -19,55 +19,57 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Children take their mother’s surname. */ class MatrilinealSurnameTradition extends DefaultSurnameTradition { /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { - if (preg_match(self::REGEX_SPFX_SURN, $mother_name, $match)) { - return array_filter([ - 'NAME' => $match['NAME'], - 'SPFX' => $match['SPFX'], - 'SURN' => $match['SURN'], - ]); + if (preg_match(self::REGEX_SPFX_SURN, $this->extractName($mother), $match)) { + $name = $match['NAME']; + $spfx = $match['SPFX']; + $surn = $match['SURN']; + + return [ + $this->buildName($name, ['TYPE' => 'birth', 'SPFX' => $spfx, 'SURN' => $surn]), + ]; } - return [ - 'NAME' => '//', - ]; + return parent::newChildNames($father, $mother, $sex); } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if ($parent_sex === 'F' && preg_match(self::REGEX_SPFX_SURN, $child_name, $match)) { - return array_filter([ - 'NAME' => $match['NAME'], - 'SPFX' => $match['SPFX'], - 'SURN' => $match['SURN'], - ]); + if ($sex === 'F' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($child), $match)) { + $name = $match['NAME']; + $spfx = $match['SPFX']; + $surn = $match['SURN']; + + return [ + $this->buildName($name, ['TYPE' => 'birth', 'SPFX' => $spfx, 'SURN' => $surn]), + ]; } - return [ - 'NAME' => '//', - ]; + return parent::newParentNames($child, $sex); } } diff --git a/app/SurnameTradition/PaternalSurnameTradition.php b/app/SurnameTradition/PaternalSurnameTradition.php index 232d96eaf5..a1297ce199 100644 --- a/app/SurnameTradition/PaternalSurnameTradition.php +++ b/app/SurnameTradition/PaternalSurnameTradition.php @@ -19,6 +19,8 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Children take their father’s surname. Wives take their husband’s surname. */ @@ -35,55 +37,50 @@ class PaternalSurnameTradition extends PatrilinealSurnameTradition } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if (preg_match(self::REGEX_SPFX_SURN, $child_name, $match)) { - switch ($parent_sex) { - case 'M': - return array_filter([ - 'NAME' => $match['NAME'], - 'SPFX' => $match['SPFX'], - 'SURN' => $match['SURN'], - ]); - case 'F': - return [ - 'NAME' => '//', - '_MARNM' => '/' . trim($match['SPFX'] . ' ' . $match['SURN']) . '/', - ]; - } + if ($sex === 'F' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($child), $match)) { + $name = $match['NAME']; + $spfx = $match['SPFX']; + $surn = $match['SURN']; + + return [ + $this->buildName('//', ['TYPE' => 'birth']), + $this->buildName($name, ['TYPE' => 'married', 'SPFX' => $spfx, 'SURN' => $surn]), + ]; } - return [ - 'NAME' => '//', - ]; + return parent::newParentNames($child, $sex); } /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array + public function newSpouseNames(Individual $spouse, string $sex): array { - if ($spouse_sex === 'F' && preg_match(self::REGEX_SURN, $spouse_name, $match)) { + if ($sex === 'F' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($spouse), $match)) { + $name = $match['NAME']; + $spfx = $match['SPFX']; + $surn = $match['SURN']; + return [ - 'NAME' => '//', - '_MARNM' => $match['NAME'], + $this->buildName('//', ['TYPE' => 'birth']), + $this->buildName($name, ['TYPE' => 'married', 'SPFX' => $spfx, 'SURN' => $surn]), ]; } - return [ - 'NAME' => '//', - ]; + return parent::newSpouseNames($spouse, $sex); } } diff --git a/app/SurnameTradition/PatrilinealSurnameTradition.php b/app/SurnameTradition/PatrilinealSurnameTradition.php index 0eebd0f556..5f436e747b 100644 --- a/app/SurnameTradition/PatrilinealSurnameTradition.php +++ b/app/SurnameTradition/PatrilinealSurnameTradition.php @@ -19,56 +19,58 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Children take their father’s surname. */ class PatrilinealSurnameTradition extends DefaultSurnameTradition { /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { - if (preg_match(self::REGEX_SPFX_SURN, $father_name, $match)) { - return array_filter([ - 'NAME' => $match['NAME'], - 'SPFX' => $match['SPFX'], - 'SURN' => $match['SURN'], - ]); + if (preg_match(self::REGEX_SPFX_SURN, $this->extractName($father), $match)) { + $name = $match['NAME']; + $spfx = $match['SPFX']; + $surn = $match['SURN']; + + return [ + $this->buildName($name, ['TYPE' => 'birth', 'SPFX' => $spfx, 'SURN' => $surn]), + ]; } - return [ - 'NAME' => '//', - ]; + return parent::newChildNames($father, $mother, $sex); } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if ($parent_sex === 'M' && preg_match(self::REGEX_SPFX_SURN, $child_name, $match)) { - return array_filter([ - 'NAME' => $match['NAME'], - 'SPFX' => $match['SPFX'], - 'SURN' => $match['SURN'], - ]); + if ($sex === 'M' && preg_match(self::REGEX_SPFX_SURN, $this->extractName($child), $match)) { + $name = $match['NAME']; + $spfx = $match['SPFX']; + $surn = $match['SURN']; + + return [ + $this->buildName($name, ['TYPE' => 'birth', 'SPFX' => $spfx, 'SURN' => $surn]), + ]; } - return [ - 'NAME' => '//', - ]; + return parent::newParentNames($child, $sex); } /** diff --git a/app/SurnameTradition/PolishSurnameTradition.php b/app/SurnameTradition/PolishSurnameTradition.php index e48224e7cc..ffcd211ce1 100644 --- a/app/SurnameTradition/PolishSurnameTradition.php +++ b/app/SurnameTradition/PolishSurnameTradition.php @@ -19,6 +19,8 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Children take their father’s surname. Wives take their husband’s surname. Surnames are inflected to indicate an individual’s sex. */ @@ -41,76 +43,73 @@ class PolishSurnameTradition extends PaternalSurnameTradition ]; /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { - if (preg_match(self::REGEX_SURN, $father_name, $match)) { - if ($child_sex === 'F') { - return array_filter([ - 'NAME' => $this->inflect($match['NAME'], self::INFLECT_FEMALE), - 'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE), - ]); + if (preg_match(self::REGEX_SURN, $this->extractName($father), $match)) { + if ($sex === 'F') { + $name = $this->inflect($match['NAME'], self::INFLECT_FEMALE); + } else { + $name = $this->inflect($match['NAME'], self::INFLECT_MALE); } - return array_filter([ - 'NAME' => $this->inflect($match['NAME'], self::INFLECT_MALE), - 'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE), - ]); + $surn = $this->inflect($match['SURN'], self::INFLECT_MALE); + + return [$this->buildName($name, ['TYPE' => 'birth', 'SURN' => $surn])]; } - return [ - 'NAME' => '//', - ]; + return [$this->buildName('//', ['TYPE' => 'birth'])]; } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if ($parent_sex === 'M' && preg_match(self::REGEX_SURN, $child_name, $match)) { - return array_filter([ - 'NAME' => $this->inflect($match['NAME'], self::INFLECT_MALE), - 'SURN' => $this->inflect($match['SURN'], self::INFLECT_MALE), - ]); + if ($sex === 'M' && preg_match(self::REGEX_SURN, $this->extractName($child), $match)) { + $name = $this->inflect($match['NAME'], self::INFLECT_MALE); + $surn = $this->inflect($match['SURN'], self::INFLECT_MALE); + + return [ + $this->buildName($name, ['TYPE' => 'birth', 'SURN' => $surn]), + ]; } - return [ - 'NAME' => '//', - ]; + return [$this->buildName('//', ['TYPE' => 'birth'])]; } /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array + public function newSpouseNames(Individual $spouse, string $sex): array { - if ($spouse_sex === 'F' && preg_match(self::REGEX_SURN, $spouse_name, $match)) { + if ($sex === 'F' && preg_match(self::REGEX_SURN, $this->extractName($spouse), $match)) { + $name = $this->inflect($match['NAME'], self::INFLECT_FEMALE); + $surn = $this->inflect($match['SURN'], self::INFLECT_MALE); + return [ - 'NAME' => '//', - '_MARNM' => $this->inflect($match['NAME'], self::INFLECT_FEMALE), + $this->buildName('//', ['TYPE' => 'birth']), + $this->buildName($name, ['TYPE' => 'married', 'SURN' => $surn]), ]; } - return [ - 'NAME' => '//', - ]; + return [$this->buildName('//', ['TYPE' => 'birth'])]; } } diff --git a/app/SurnameTradition/PortugueseSurnameTradition.php b/app/SurnameTradition/PortugueseSurnameTradition.php index ebc40f8d33..f78a428ff0 100644 --- a/app/SurnameTradition/PortugueseSurnameTradition.php +++ b/app/SurnameTradition/PortugueseSurnameTradition.php @@ -19,6 +19,8 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Children take one surname from the mother and one surname from the father. * @@ -29,76 +31,83 @@ namespace Fisharebest\Webtrees\SurnameTradition; class PortugueseSurnameTradition extends DefaultSurnameTradition { /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { - if (preg_match(self::REGEX_SURNS, $father_name, $match_father)) { + if (preg_match(self::REGEX_SURNS, $this->extractName($father), $match_father)) { $father_surname = $match_father['SURN2']; } else { $father_surname = ''; } - if (preg_match(self::REGEX_SURNS, $mother_name, $match_mother)) { + if (preg_match(self::REGEX_SURNS, $this->extractName($mother), $match_mother)) { $mother_surname = $match_mother['SURN2']; } else { $mother_surname = ''; } return [ - 'NAME' => '/' . $father_surname . '/ /' . $mother_surname . '/', - 'SURN' => trim($father_surname . ',' . $mother_surname, ','), + $this->buildName('/' . $father_surname . '/ /' . $mother_surname . '/', [ + 'TYPE' => 'birth', + 'SURN' => trim($father_surname . ',' . $mother_surname, ','), + ]), ]; } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if (preg_match(self::REGEX_SURNS, $child_name, $match)) { - switch ($parent_sex) { + if (preg_match(self::REGEX_SURNS, $this->extractName($child), $match)) { + switch ($sex) { case 'M': return [ - 'NAME' => '// /' . $match['SURN1'] . '/', - 'SURN' => $match['SURN1'], + $this->buildName('// /' . $match['SURN1'] . '/', [ + 'TYPE' => 'birth', + 'SURN' => $match['SURN1'], + ]), ]; + case 'F': return [ - 'NAME' => '// /' . $match['SURN2'] . '/', - 'SURN' => $match['SURN2'], + $this->buildName('// /' . $match['SURN2'] . '/', [ + 'TYPE' => 'birth', + 'SURN' => $match['SURN2'], + ]), ]; } } return [ - 'NAME' => '// //', + $this->buildName('// //', ['TYPE' => 'birth']), ]; } /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array + public function newSpouseNames(Individual $spouse, string $sex): array { return [ - 'NAME' => '// //', + $this->buildName('// //', ['TYPE' => 'birth']), ]; } } diff --git a/app/SurnameTradition/SpanishSurnameTradition.php b/app/SurnameTradition/SpanishSurnameTradition.php index b732cde395..81780b2533 100644 --- a/app/SurnameTradition/SpanishSurnameTradition.php +++ b/app/SurnameTradition/SpanishSurnameTradition.php @@ -19,6 +19,8 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Children take one surname from the father and one surname from the mother. * @@ -29,76 +31,83 @@ namespace Fisharebest\Webtrees\SurnameTradition; class SpanishSurnameTradition extends DefaultSurnameTradition { /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array { - if (preg_match(self::REGEX_SURNS, $father_name, $match_father)) { + if (preg_match(self::REGEX_SURNS, $this->extractName($father), $match_father)) { $father_surname = $match_father['SURN1']; } else { $father_surname = ''; } - if (preg_match(self::REGEX_SURNS, $mother_name, $match_mother)) { + if (preg_match(self::REGEX_SURNS, $this->extractName($mother), $match_mother)) { $mother_surname = $match_mother['SURN1']; } else { $mother_surname = ''; } return [ - 'NAME' => '/' . $father_surname . '/ /' . $mother_surname . '/', - 'SURN' => trim($father_surname . ',' . $mother_surname, ','), + $this->buildName('/' . $father_surname . '/ /' . $mother_surname . '/', [ + 'TYPE' => 'birth', + 'SURN' => trim($father_surname . ',' . $mother_surname, ','), + ]), ]; } /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array + public function newParentNames(Individual $child, string $sex): array { - if (preg_match(self::REGEX_SURNS, $child_name, $match)) { - switch ($parent_sex) { + if (preg_match(self::REGEX_SURNS, $this->extractName($child), $match)) { + switch ($sex) { case 'M': return [ - 'NAME' => '/' . $match['SURN1'] . '/ //', - 'SURN' => $match['SURN1'], + $this->buildName('/' . $match['SURN1'] . '/ //', [ + 'TYPE' => 'birth', + 'SURN' => $match['SURN1'], + ]), ]; + case 'F': return [ - 'NAME' => '/' . $match['SURN2'] . '/ //', - 'SURN' => $match['SURN2'], + $this->buildName('/' . $match['SURN2'] . '/ //', [ + 'TYPE' => 'birth', + 'SURN' => $match['SURN2'], + ]), ]; } } return [ - 'NAME' => '// //', + $this->buildName('// //', ['TYPE' => 'birth']), ]; } /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array + public function newSpouseNames(Individual $spouse, string $sex): array { return [ - 'NAME' => '// //', + $this->buildName('// //', ['TYPE' => 'birth']), ]; } } diff --git a/app/SurnameTradition/SurnameTraditionInterface.php b/app/SurnameTradition/SurnameTraditionInterface.php index 92a9ef6191..a36647b05e 100644 --- a/app/SurnameTradition/SurnameTraditionInterface.php +++ b/app/SurnameTradition/SurnameTraditionInterface.php @@ -19,6 +19,8 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\SurnameTradition; +use Fisharebest\Webtrees\Individual; + /** * Various cultures have different traditions for the use of surnames within families. * By providing defaults for new individuals, we can speed up data entry and reduce errors. @@ -40,33 +42,33 @@ interface SurnameTraditionInterface public function hasSurnames(): bool; /** - * What names are given to a new child + * What name is given to a new child * - * @param string $father_name A GEDCOM NAME - * @param string $mother_name A GEDCOM NAME - * @param string $child_sex M, F or U + * @param Individual|null $father + * @param Individual|null $mother + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newChildNames(string $father_name, string $mother_name, string $child_sex): array; + public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array; /** - * What names are given to a new parent + * What name is given to a new parent * - * @param string $child_name A GEDCOM NAME - * @param string $parent_sex M, F or U + * @param Individual $child + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newParentNames(string $child_name, string $parent_sex): array; + public function newParentNames(Individual $child, string $sex): array; /** * What names are given to a new spouse * - * @param string $spouse_name A GEDCOM NAME - * @param string $spouse_sex M, F or U + * @param Individual $spouse + * @param string $sex * - * @return array<string,string> Associative array of GEDCOM name parts (SURN, _MARNM, etc.) + * @return array<string> */ - public function newSpouseNames(string $spouse_name, string $spouse_sex): array; + public function newSpouseNames(Individual $spouse, string $sex): array; } |
