xref: /webtrees/app/SurnameTradition/DefaultSurnameTradition.php (revision 01ffdfd0eecd9ab07c810118a9c56e450f5d4323)
1323788f4SGreg Roach<?php
23976b470SGreg Roach
3323788f4SGreg Roach/**
4323788f4SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
22cb7a42eaSGreg Roachuse Fisharebest\Webtrees\Fact;
23cb7a42eaSGreg Roachuse Fisharebest\Webtrees\Individual;
24cb7a42eaSGreg Roach
25cb7a42eaSGreg Roachuse function array_filter;
26cb7a42eaSGreg Roachuse function array_keys;
27cb7a42eaSGreg Roachuse function array_map;
28cb7a42eaSGreg Roachuse function implode;
29cb7a42eaSGreg Roachuse function in_array;
30cb7a42eaSGreg Roach
31323788f4SGreg Roach/**
32323788f4SGreg Roach * All family members keep their original surname
33323788f4SGreg Roach */
34c1010edaSGreg Roachclass DefaultSurnameTradition implements SurnameTraditionInterface
35c1010edaSGreg Roach{
36323788f4SGreg Roach    /** Extract a GIVN from a NAME */
3716d6367aSGreg Roach    protected const REGEX_GIVN = '~^(?<GIVN>[^/ ]+)~';
38323788f4SGreg Roach
39323788f4SGreg Roach    /** Extract a SPFX and SURN from a NAME */
4016d6367aSGreg Roach    protected const REGEX_SPFX_SURN = '~(?<NAME>/(?<SPFX>[a-z’\']{0,4}(?: [a-z’\']{1,4})*) ?(?<SURN>[^/]*)/)~';
41323788f4SGreg Roach
42323788f4SGreg Roach    /** Extract a simple SURN from a NAME */
4316d6367aSGreg Roach    protected const REGEX_SURN = '~(?<NAME>/(?<SURN>[^/]+)/)~';
44323788f4SGreg Roach
45323788f4SGreg Roach    /** Extract two Spanish/Portuguese SURNs from a NAME */
4616d6367aSGreg Roach    protected const REGEX_SURNS = '~/(?<SURN1>[^ /]+)(?: | y |/ /|/ y /)(?<SURN2>[^ /]+)/~';
47323788f4SGreg Roach
48323788f4SGreg Roach    /**
49323788f4SGreg Roach     * Does this surname tradition change surname at marriage?
50323788f4SGreg Roach     *
51323788f4SGreg Roach     * @return bool
52323788f4SGreg Roach     */
538f53f488SRico Sonntag    public function hasMarriedNames(): bool
54c1010edaSGreg Roach    {
55323788f4SGreg Roach        return false;
56323788f4SGreg Roach    }
57323788f4SGreg Roach
58323788f4SGreg Roach    /**
59c1ec7145SGreg Roach     * Does this surname tradition use surnames?
60c1ec7145SGreg Roach     *
61c1ec7145SGreg Roach     * @return bool
62c1ec7145SGreg Roach     */
638f53f488SRico Sonntag    public function hasSurnames(): bool
64c1010edaSGreg Roach    {
65c1ec7145SGreg Roach        return true;
66c1ec7145SGreg Roach    }
67c1ec7145SGreg Roach
68c1ec7145SGreg Roach    /**
69cb7a42eaSGreg Roach     * What name is given to a new child
70323788f4SGreg Roach     *
71cb7a42eaSGreg Roach     * @param Individual|null $father
72cb7a42eaSGreg Roach     * @param Individual|null $mother
73cb7a42eaSGreg Roach     * @param string          $sex
74323788f4SGreg Roach     *
75*01ffdfd0SGreg Roach     * @return array<int,string>
76323788f4SGreg Roach     */
77cb7a42eaSGreg Roach    public function newChildNames(?Individual $father, ?Individual $mother, string $sex): array
78c1010edaSGreg Roach    {
7913abd6f3SGreg Roach        return [
80cb7a42eaSGreg Roach            $this->buildName('//', ['TYPE' => 'birth']),
8113abd6f3SGreg Roach        ];
82323788f4SGreg Roach    }
83323788f4SGreg Roach
84323788f4SGreg Roach    /**
85cb7a42eaSGreg Roach     * What name is given to a new parent
86323788f4SGreg Roach     *
87cb7a42eaSGreg Roach     * @param Individual $child
88cb7a42eaSGreg Roach     * @param string     $sex
89323788f4SGreg Roach     *
90*01ffdfd0SGreg Roach     * @return array<int,string>
91323788f4SGreg Roach     */
92cb7a42eaSGreg Roach    public function newParentNames(Individual $child, string $sex): array
93c1010edaSGreg Roach    {
9413abd6f3SGreg Roach        return [
95cb7a42eaSGreg Roach            $this->buildName('//', ['TYPE' => 'birth']),
9613abd6f3SGreg Roach        ];
97323788f4SGreg Roach    }
98323788f4SGreg Roach
99323788f4SGreg Roach    /**
100323788f4SGreg Roach     * What names are given to a new spouse
101323788f4SGreg Roach     *
102cb7a42eaSGreg Roach     * @param Individual $spouse
103cb7a42eaSGreg Roach     * @param string     $sex
104323788f4SGreg Roach     *
105*01ffdfd0SGreg Roach     * @return array<int,string>
106323788f4SGreg Roach     */
107cb7a42eaSGreg Roach    public function newSpouseNames(Individual $spouse, string $sex): array
108c1010edaSGreg Roach    {
10913abd6f3SGreg Roach        return [
110cb7a42eaSGreg Roach            $this->buildName('//', ['TYPE' => 'birth']),
11113abd6f3SGreg Roach        ];
112323788f4SGreg Roach    }
113cb7a42eaSGreg Roach
114cb7a42eaSGreg Roach    /**
115cb7a42eaSGreg Roach     * Build a GEDCOM name record
116cb7a42eaSGreg Roach     *
117cb7a42eaSGreg Roach     * @param string               $name
118cb7a42eaSGreg Roach     * @param array<string,string> $parts
119cb7a42eaSGreg Roach     *
120cb7a42eaSGreg Roach     * @return string
121cb7a42eaSGreg Roach     */
122cb7a42eaSGreg Roach    protected function buildName(string $name, array $parts): string
123cb7a42eaSGreg Roach    {
124cb7a42eaSGreg Roach        $parts = array_filter($parts);
125cb7a42eaSGreg Roach
126cb7a42eaSGreg Roach        $parts = array_map(
127cb7a42eaSGreg Roach            fn (string $tag, string $value): string => "\n2 " . $tag . ' ' . $value,
128cb7a42eaSGreg Roach            array_keys($parts),
129cb7a42eaSGreg Roach            $parts
130cb7a42eaSGreg Roach        );
131cb7a42eaSGreg Roach
132cb7a42eaSGreg Roach        if ($name === '') {
133cb7a42eaSGreg Roach            return '1 NAME' . implode($parts);
134cb7a42eaSGreg Roach        }
135cb7a42eaSGreg Roach
136cb7a42eaSGreg Roach        return '1 NAME ' . $name . implode($parts);
137cb7a42eaSGreg Roach    }
138cb7a42eaSGreg Roach
139cb7a42eaSGreg Roach    /**
140cb7a42eaSGreg Roach     * Extract an individual's name.
141cb7a42eaSGreg Roach     *
142cb7a42eaSGreg Roach     * @param Individual|null $individual
143cb7a42eaSGreg Roach     *
144cb7a42eaSGreg Roach     * @return string
145cb7a42eaSGreg Roach     */
146cb7a42eaSGreg Roach    protected function extractName(?Individual $individual): string
147cb7a42eaSGreg Roach    {
148cb7a42eaSGreg Roach        if ($individual instanceof Individual) {
149cb7a42eaSGreg Roach            $fact = $individual
150cb7a42eaSGreg Roach                ->facts(['NAME'])
151cb7a42eaSGreg Roach                ->first(fn (Fact $fact): bool => in_array($fact->attribute('TYPE'), ['', 'birth', 'change'], true));
152cb7a42eaSGreg Roach
153cb7a42eaSGreg Roach            if ($fact instanceof Fact) {
154cb7a42eaSGreg Roach                return $fact->value();
155cb7a42eaSGreg Roach            }
156cb7a42eaSGreg Roach        }
157cb7a42eaSGreg Roach
158cb7a42eaSGreg Roach        return '';
159cb7a42eaSGreg Roach    }
160323788f4SGreg Roach}
161