xref: /webtrees/app/Factories/SurnameTraditionFactory.php (revision 7e128bbfeb9f0f479efa344744b4446bc62c45c4)
1*7e128bbfSGreg Roach<?php
2*7e128bbfSGreg Roach
3*7e128bbfSGreg Roach/**
4*7e128bbfSGreg Roach * webtrees: online genealogy
5*7e128bbfSGreg Roach * Copyright (C) 2022 webtrees development team
6*7e128bbfSGreg Roach * This program is free software: you can redistribute it and/or modify
7*7e128bbfSGreg Roach * it under the terms of the GNU General Public License as published by
8*7e128bbfSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*7e128bbfSGreg Roach * (at your option) any later version.
10*7e128bbfSGreg Roach * This program is distributed in the hope that it will be useful,
11*7e128bbfSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*7e128bbfSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*7e128bbfSGreg Roach * GNU General Public License for more details.
14*7e128bbfSGreg Roach * You should have received a copy of the GNU General Public License
15*7e128bbfSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*7e128bbfSGreg Roach */
17*7e128bbfSGreg Roach
18*7e128bbfSGreg Roachdeclare(strict_types=1);
19*7e128bbfSGreg Roach
20*7e128bbfSGreg Roachnamespace Fisharebest\Webtrees\Factories;
21*7e128bbfSGreg Roach
22*7e128bbfSGreg Roachuse Fisharebest\Webtrees\Contracts\SurnameTraditionFactoryInterface;
23*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\DefaultSurnameTradition;
24*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\IcelandicSurnameTradition;
25*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\LithuanianSurnameTradition;
26*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\MatrilinealSurnameTradition;
27*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PaternalSurnameTradition;
28*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PatrilinealSurnameTradition;
29*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PolishSurnameTradition;
30*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PortugueseSurnameTradition;
31*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\SpanishSurnameTradition;
32*7e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\SurnameTraditionInterface;
33*7e128bbfSGreg Roach
34*7e128bbfSGreg Roach/**
35*7e128bbfSGreg Roach * Create a surname tradition.
36*7e128bbfSGreg Roach */
37*7e128bbfSGreg Roachclass SurnameTraditionFactory implements SurnameTraditionFactoryInterface
38*7e128bbfSGreg Roach{
39*7e128bbfSGreg Roach    /** @var array<SurnameTraditionInterface> */
40*7e128bbfSGreg Roach    private array $surname_traditions = [];
41*7e128bbfSGreg Roach
42*7e128bbfSGreg Roach    /**
43*7e128bbfSGreg Roach     * Register the surname traditions.
44*7e128bbfSGreg Roach     */
45*7e128bbfSGreg Roach    public function __construct()
46*7e128bbfSGreg Roach    {
47*7e128bbfSGreg Roach        $this->register(self::PATERNAL, new PaternalSurnameTradition());
48*7e128bbfSGreg Roach        $this->register(self::PATRILINEAL, new PatrilinealSurnameTradition());
49*7e128bbfSGreg Roach        $this->register(self::MATRILINEAL, new MatrilinealSurnameTradition());
50*7e128bbfSGreg Roach        $this->register(self::PORTUGUESE, new PortugueseSurnameTradition());
51*7e128bbfSGreg Roach        $this->register(self::SPANISH, new SpanishSurnameTradition());
52*7e128bbfSGreg Roach        $this->register(self::POLISH, new PolishSurnameTradition());
53*7e128bbfSGreg Roach        $this->register(self::LITHUANIAN, new LithuanianSurnameTradition());
54*7e128bbfSGreg Roach        $this->register(self::ICELANDIC, new IcelandicSurnameTradition());
55*7e128bbfSGreg Roach        $this->register(self::DEFAULT, new DefaultSurnameTradition());
56*7e128bbfSGreg Roach    }
57*7e128bbfSGreg Roach
58*7e128bbfSGreg Roach    /**
59*7e128bbfSGreg Roach     * A list of supported surname traditions and their names.
60*7e128bbfSGreg Roach     *
61*7e128bbfSGreg Roach     * @return array<string,string>
62*7e128bbfSGreg Roach     */
63*7e128bbfSGreg Roach    public function list(): array
64*7e128bbfSGreg Roach    {
65*7e128bbfSGreg Roach        $fn = static fn (SurnameTraditionInterface $surname_tradition): string => $surname_tradition->name() . ' — ' . $surname_tradition->description();
66*7e128bbfSGreg Roach
67*7e128bbfSGreg Roach        return array_map($fn, $this->surname_traditions);
68*7e128bbfSGreg Roach    }
69*7e128bbfSGreg Roach
70*7e128bbfSGreg Roach    /**
71*7e128bbfSGreg Roach     * Create a named surname tradition.
72*7e128bbfSGreg Roach     *
73*7e128bbfSGreg Roach     * @param string $name
74*7e128bbfSGreg Roach     *
75*7e128bbfSGreg Roach     * @return SurnameTraditionInterface
76*7e128bbfSGreg Roach     */
77*7e128bbfSGreg Roach    public function make(string $name): SurnameTraditionInterface
78*7e128bbfSGreg Roach    {
79*7e128bbfSGreg Roach        return $this->surname_traditions[$name] ?? new DefaultSurnameTradition();
80*7e128bbfSGreg Roach    }
81*7e128bbfSGreg Roach
82*7e128bbfSGreg Roach    /**
83*7e128bbfSGreg Roach     * @param string                    $name
84*7e128bbfSGreg Roach     * @param SurnameTraditionInterface $surname_tradition
85*7e128bbfSGreg Roach     *
86*7e128bbfSGreg Roach     * @return void
87*7e128bbfSGreg Roach     */
88*7e128bbfSGreg Roach    public function register(string $name, SurnameTraditionInterface $surname_tradition): void
89*7e128bbfSGreg Roach    {
90*7e128bbfSGreg Roach        $this->surname_traditions[$name] = $surname_tradition;
91*7e128bbfSGreg Roach    }
92*7e128bbfSGreg Roach}
93