Passed
Push — main ( 4118eb...a5c812 )
by Greg
07:41
created

SurnameTraditionFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Factories;
21
22
use Fisharebest\Webtrees\Contracts\SurnameTraditionFactoryInterface;
23
use Fisharebest\Webtrees\SurnameTradition\DefaultSurnameTradition;
24
use Fisharebest\Webtrees\SurnameTradition\IcelandicSurnameTradition;
25
use Fisharebest\Webtrees\SurnameTradition\LithuanianSurnameTradition;
26
use Fisharebest\Webtrees\SurnameTradition\MatrilinealSurnameTradition;
27
use Fisharebest\Webtrees\SurnameTradition\PaternalSurnameTradition;
28
use Fisharebest\Webtrees\SurnameTradition\PatrilinealSurnameTradition;
29
use Fisharebest\Webtrees\SurnameTradition\PolishSurnameTradition;
30
use Fisharebest\Webtrees\SurnameTradition\PortugueseSurnameTradition;
31
use Fisharebest\Webtrees\SurnameTradition\SpanishSurnameTradition;
32
use Fisharebest\Webtrees\SurnameTradition\SurnameTraditionInterface;
33
34
/**
35
 * Create a surname tradition.
36
 */
37
class 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