xref: /webtrees/app/Factories/SurnameTraditionFactory.php (revision 2ada9ea4cb12abae4cb4824ae5606c52b5b0be74)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Factories;
21
22use Fisharebest\Webtrees\Contracts\SurnameTraditionFactoryInterface;
23use Fisharebest\Webtrees\SurnameTradition\DefaultSurnameTradition;
24use Fisharebest\Webtrees\SurnameTradition\IcelandicSurnameTradition;
25use Fisharebest\Webtrees\SurnameTradition\LithuanianSurnameTradition;
26use Fisharebest\Webtrees\SurnameTradition\MatrilinealSurnameTradition;
27use Fisharebest\Webtrees\SurnameTradition\PaternalSurnameTradition;
28use Fisharebest\Webtrees\SurnameTradition\PatrilinealSurnameTradition;
29use Fisharebest\Webtrees\SurnameTradition\PolishSurnameTradition;
30use Fisharebest\Webtrees\SurnameTradition\PortugueseSurnameTradition;
31use Fisharebest\Webtrees\SurnameTradition\SpanishSurnameTradition;
32use Fisharebest\Webtrees\SurnameTradition\SurnameTraditionInterface;
33
34/**
35 * Create a surname tradition.
36 */
37class SurnameTraditionFactory implements SurnameTraditionFactoryInterface
38{
39    /** @var array<SurnameTraditionInterface> */
40    private array $surname_traditions = [];
41
42    /**
43     * Register the surname traditions.
44     */
45    public function __construct()
46    {
47        $this->register(self::PATERNAL, new PaternalSurnameTradition());
48        $this->register(self::PATRILINEAL, new PatrilinealSurnameTradition());
49        $this->register(self::MATRILINEAL, new MatrilinealSurnameTradition());
50        $this->register(self::PORTUGUESE, new PortugueseSurnameTradition());
51        $this->register(self::SPANISH, new SpanishSurnameTradition());
52        $this->register(self::POLISH, new PolishSurnameTradition());
53        $this->register(self::LITHUANIAN, new LithuanianSurnameTradition());
54        $this->register(self::ICELANDIC, new IcelandicSurnameTradition());
55        $this->register(self::DEFAULT, new DefaultSurnameTradition());
56    }
57
58    /**
59     * A list of supported surname traditions and their names.
60     *
61     * @return array<string,string>
62     */
63    public function list(): array
64    {
65        $fn = static fn (SurnameTraditionInterface $surname_tradition): string => $surname_tradition->name() . ' — ' . $surname_tradition->description();
66
67        return array_map($fn, $this->surname_traditions);
68    }
69
70    /**
71     * Create a named surname tradition.
72     *
73     * @param string $name
74     *
75     * @return SurnameTraditionInterface
76     */
77    public function make(string $name): SurnameTraditionInterface
78    {
79        return $this->surname_traditions[$name] ?? new DefaultSurnameTradition();
80    }
81
82    /**
83     * @param string                    $name
84     * @param SurnameTraditionInterface $surname_tradition
85     *
86     * @return void
87     */
88    public function register(string $name, SurnameTraditionInterface $surname_tradition): void
89    {
90        $this->surname_traditions[$name] = $surname_tradition;
91    }
92}
93