1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2021 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\SlugFactoryInterface; |
23
|
|
|
use Fisharebest\Webtrees\GedcomRecord; |
24
|
|
|
use Transliterator; |
25
|
|
|
|
26
|
|
|
use function extension_loaded; |
27
|
|
|
use function preg_replace; |
28
|
|
|
use function strip_tags; |
29
|
|
|
use function trim; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Make a slug to be used in the URL of a GedcomRecord. |
33
|
|
|
*/ |
34
|
|
|
class SlugFactory implements SlugFactoryInterface |
35
|
|
|
{ |
36
|
|
|
/** @var Transliterator|null $transliterator */ |
37
|
|
|
private $transliterator; |
38
|
|
|
|
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
if (extension_loaded('intl')) { |
42
|
|
|
$ids = Transliterator::listIDs(); |
43
|
|
|
|
44
|
|
|
if (in_array('Any-Latin', $ids, true) && in_array('Latin-ASCII', $ids, true)) { |
45
|
|
|
$this->transliterator = Transliterator::create('Any-Latin;Latin-ASCII'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param GedcomRecord $record |
52
|
|
|
* |
53
|
|
|
* @return string|null |
54
|
|
|
*/ |
55
|
|
|
public function make(GedcomRecord $record): ?string |
56
|
|
|
{ |
57
|
|
|
$slug = strip_tags($record->fullName()); |
58
|
|
|
|
59
|
|
|
if ($this->transliterator instanceof Transliterator) { |
60
|
|
|
$slug = $this->transliterator->transliterate($slug); |
61
|
|
|
|
62
|
|
|
if ($slug === false) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$slug = preg_replace('/[^A-Za-z0-9]+/', '-', $slug); |
68
|
|
|
$slug = trim($slug, '-'); |
69
|
|
|
|
70
|
|
|
if ($slug !== '') { |
71
|
|
|
return $slug; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|