xref: /webtrees/app/Factories/SlugFactory.php (revision 194b0938bfa145814c8ade07169d1464fa285e2a)
1*194b0938SGreg Roach<?php
2*194b0938SGreg Roach
3*194b0938SGreg Roach/**
4*194b0938SGreg Roach * webtrees: online genealogy
5*194b0938SGreg Roach * Copyright (C) 2021 webtrees development team
6*194b0938SGreg Roach * This program is free software: you can redistribute it and/or modify
7*194b0938SGreg Roach * it under the terms of the GNU General Public License as published by
8*194b0938SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*194b0938SGreg Roach * (at your option) any later version.
10*194b0938SGreg Roach * This program is distributed in the hope that it will be useful,
11*194b0938SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*194b0938SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*194b0938SGreg Roach * GNU General Public License for more details.
14*194b0938SGreg Roach * You should have received a copy of the GNU General Public License
15*194b0938SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*194b0938SGreg Roach */
17*194b0938SGreg Roach
18*194b0938SGreg Roachdeclare(strict_types=1);
19*194b0938SGreg Roach
20*194b0938SGreg Roachnamespace Fisharebest\Webtrees\Factories;
21*194b0938SGreg Roach
22*194b0938SGreg Roachuse Fisharebest\Webtrees\Contracts\SlugFactoryInterface;
23*194b0938SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
24*194b0938SGreg Roachuse Transliterator;
25*194b0938SGreg Roach
26*194b0938SGreg Roachuse function extension_loaded;
27*194b0938SGreg Roachuse function preg_replace;
28*194b0938SGreg Roachuse function strip_tags;
29*194b0938SGreg Roachuse function trim;
30*194b0938SGreg Roach
31*194b0938SGreg Roach/**
32*194b0938SGreg Roach * Make a slug to be used in the URL of a GedcomRecord.
33*194b0938SGreg Roach */
34*194b0938SGreg Roachclass SlugFactory implements SlugFactoryInterface
35*194b0938SGreg Roach{
36*194b0938SGreg Roach    private ?Transliterator $transliterator;
37*194b0938SGreg Roach
38*194b0938SGreg Roach    public function __construct()
39*194b0938SGreg Roach    {
40*194b0938SGreg Roach        if (extension_loaded('intl')) {
41*194b0938SGreg Roach            $ids = Transliterator::listIDs();
42*194b0938SGreg Roach
43*194b0938SGreg Roach            if (in_array('Any-Latin', $ids, true) && in_array('Latin-ASCII', $ids, true)) {
44*194b0938SGreg Roach                $this->transliterator = Transliterator::create('Any-Latin;Latin-ASCII');
45*194b0938SGreg Roach            }
46*194b0938SGreg Roach        }
47*194b0938SGreg Roach    }
48*194b0938SGreg Roach
49*194b0938SGreg Roach    /**
50*194b0938SGreg Roach     * @param GedcomRecord $record
51*194b0938SGreg Roach     *
52*194b0938SGreg Roach     * @return string|null
53*194b0938SGreg Roach     */
54*194b0938SGreg Roach    public function make(GedcomRecord $record): ?string
55*194b0938SGreg Roach    {
56*194b0938SGreg Roach        $slug = strip_tags($record->fullName());
57*194b0938SGreg Roach
58*194b0938SGreg Roach        if ($this->transliterator instanceof Transliterator) {
59*194b0938SGreg Roach            $slug = $this->transliterator->transliterate($slug);
60*194b0938SGreg Roach        }
61*194b0938SGreg Roach
62*194b0938SGreg Roach        $slug = preg_replace('/[^A-Za-z0-9]+/', '-', $slug);
63*194b0938SGreg Roach        $slug = trim($slug, '-');
64*194b0938SGreg Roach
65*194b0938SGreg Roach        if ($slug !== '') {
66*194b0938SGreg Roach            return $slug;
67*194b0938SGreg Roach        }
68*194b0938SGreg Roach
69*194b0938SGreg Roach        return null;
70*194b0938SGreg Roach    }
71*194b0938SGreg Roach}
72