xref: /webtrees/app/Factories/SurnameTraditionFactory.php (revision d11be7027e34e3121be11cc025421873364403f9)
17e128bbfSGreg Roach<?php
27e128bbfSGreg Roach
37e128bbfSGreg Roach/**
47e128bbfSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
67e128bbfSGreg Roach * This program is free software: you can redistribute it and/or modify
77e128bbfSGreg Roach * it under the terms of the GNU General Public License as published by
87e128bbfSGreg Roach * the Free Software Foundation, either version 3 of the License, or
97e128bbfSGreg Roach * (at your option) any later version.
107e128bbfSGreg Roach * This program is distributed in the hope that it will be useful,
117e128bbfSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
127e128bbfSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
137e128bbfSGreg Roach * GNU General Public License for more details.
147e128bbfSGreg Roach * You should have received a copy of the GNU General Public License
157e128bbfSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
167e128bbfSGreg Roach */
177e128bbfSGreg Roach
187e128bbfSGreg Roachdeclare(strict_types=1);
197e128bbfSGreg Roach
207e128bbfSGreg Roachnamespace Fisharebest\Webtrees\Factories;
217e128bbfSGreg Roach
227e128bbfSGreg Roachuse Fisharebest\Webtrees\Contracts\SurnameTraditionFactoryInterface;
237e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\DefaultSurnameTradition;
247e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\IcelandicSurnameTradition;
257e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\LithuanianSurnameTradition;
267e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\MatrilinealSurnameTradition;
277e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PaternalSurnameTradition;
287e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PatrilinealSurnameTradition;
297e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PolishSurnameTradition;
307e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PortugueseSurnameTradition;
317e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\SpanishSurnameTradition;
327e128bbfSGreg Roachuse Fisharebest\Webtrees\SurnameTradition\SurnameTraditionInterface;
337e128bbfSGreg Roach
347e128bbfSGreg Roach/**
357e128bbfSGreg Roach * Create a surname tradition.
367e128bbfSGreg Roach */
377e128bbfSGreg Roachclass SurnameTraditionFactory implements SurnameTraditionFactoryInterface
387e128bbfSGreg Roach{
397e128bbfSGreg Roach    /** @var array<SurnameTraditionInterface> */
407e128bbfSGreg Roach    private array $surname_traditions = [];
417e128bbfSGreg Roach
427e128bbfSGreg Roach    /**
437e128bbfSGreg Roach     * Register the surname traditions.
447e128bbfSGreg Roach     */
457e128bbfSGreg Roach    public function __construct()
467e128bbfSGreg Roach    {
477e128bbfSGreg Roach        $this->register(self::PATERNAL, new PaternalSurnameTradition());
487e128bbfSGreg Roach        $this->register(self::PATRILINEAL, new PatrilinealSurnameTradition());
497e128bbfSGreg Roach        $this->register(self::MATRILINEAL, new MatrilinealSurnameTradition());
507e128bbfSGreg Roach        $this->register(self::PORTUGUESE, new PortugueseSurnameTradition());
517e128bbfSGreg Roach        $this->register(self::SPANISH, new SpanishSurnameTradition());
527e128bbfSGreg Roach        $this->register(self::POLISH, new PolishSurnameTradition());
537e128bbfSGreg Roach        $this->register(self::LITHUANIAN, new LithuanianSurnameTradition());
547e128bbfSGreg Roach        $this->register(self::ICELANDIC, new IcelandicSurnameTradition());
557e128bbfSGreg Roach        $this->register(self::DEFAULT, new DefaultSurnameTradition());
567e128bbfSGreg Roach    }
577e128bbfSGreg Roach
587e128bbfSGreg Roach    /**
597e128bbfSGreg Roach     * A list of supported surname traditions and their names.
607e128bbfSGreg Roach     *
617e128bbfSGreg Roach     * @return array<string,string>
627e128bbfSGreg Roach     */
637e128bbfSGreg Roach    public function list(): array
647e128bbfSGreg Roach    {
657e128bbfSGreg Roach        $fn = static fn (SurnameTraditionInterface $surname_tradition): string => $surname_tradition->name() . ' — ' . $surname_tradition->description();
667e128bbfSGreg Roach
677e128bbfSGreg Roach        return array_map($fn, $this->surname_traditions);
687e128bbfSGreg Roach    }
697e128bbfSGreg Roach
707e128bbfSGreg Roach    /**
717e128bbfSGreg Roach     * Create a named surname tradition.
727e128bbfSGreg Roach     *
737e128bbfSGreg Roach     * @param string $name
747e128bbfSGreg Roach     *
757e128bbfSGreg Roach     * @return SurnameTraditionInterface
767e128bbfSGreg Roach     */
777e128bbfSGreg Roach    public function make(string $name): SurnameTraditionInterface
787e128bbfSGreg Roach    {
797e128bbfSGreg Roach        return $this->surname_traditions[$name] ?? new DefaultSurnameTradition();
807e128bbfSGreg Roach    }
817e128bbfSGreg Roach
827e128bbfSGreg Roach    /**
837e128bbfSGreg Roach     * @param string                    $name
847e128bbfSGreg Roach     * @param SurnameTraditionInterface $surname_tradition
857e128bbfSGreg Roach     *
867e128bbfSGreg Roach     * @return void
877e128bbfSGreg Roach     */
887e128bbfSGreg Roach    public function register(string $name, SurnameTraditionInterface $surname_tradition): void
897e128bbfSGreg Roach    {
907e128bbfSGreg Roach        $this->surname_traditions[$name] = $surname_tradition;
917e128bbfSGreg Roach    }
927e128bbfSGreg Roach}
93