1194b0938SGreg Roach<?php 2194b0938SGreg Roach 3194b0938SGreg Roach/** 4194b0938SGreg Roach * webtrees: online genealogy 5194b0938SGreg Roach * Copyright (C) 2021 webtrees development team 6194b0938SGreg Roach * This program is free software: you can redistribute it and/or modify 7194b0938SGreg Roach * it under the terms of the GNU General Public License as published by 8194b0938SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9194b0938SGreg Roach * (at your option) any later version. 10194b0938SGreg Roach * This program is distributed in the hope that it will be useful, 11194b0938SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12194b0938SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13194b0938SGreg Roach * GNU General Public License for more details. 14194b0938SGreg Roach * You should have received a copy of the GNU General Public License 15194b0938SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16194b0938SGreg Roach */ 17194b0938SGreg Roach 18194b0938SGreg Roachdeclare(strict_types=1); 19194b0938SGreg Roach 20194b0938SGreg Roachnamespace Fisharebest\Webtrees\Factories; 21194b0938SGreg Roach 22194b0938SGreg Roachuse Fisharebest\Webtrees\Contracts\SlugFactoryInterface; 23194b0938SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 24194b0938SGreg Roachuse Transliterator; 25194b0938SGreg Roach 26194b0938SGreg Roachuse function extension_loaded; 27194b0938SGreg Roachuse function preg_replace; 28194b0938SGreg Roachuse function strip_tags; 29194b0938SGreg Roachuse function trim; 30194b0938SGreg Roach 31194b0938SGreg Roach/** 32194b0938SGreg Roach * Make a slug to be used in the URL of a GedcomRecord. 33194b0938SGreg Roach */ 34194b0938SGreg Roachclass SlugFactory implements SlugFactoryInterface 35194b0938SGreg Roach{ 36194b0938SGreg Roach private ?Transliterator $transliterator; 37194b0938SGreg Roach 38194b0938SGreg Roach public function __construct() 39194b0938SGreg Roach { 40194b0938SGreg Roach if (extension_loaded('intl')) { 41194b0938SGreg Roach $ids = Transliterator::listIDs(); 42194b0938SGreg Roach 43194b0938SGreg Roach if (in_array('Any-Latin', $ids, true) && in_array('Latin-ASCII', $ids, true)) { 44194b0938SGreg Roach $this->transliterator = Transliterator::create('Any-Latin;Latin-ASCII'); 45194b0938SGreg Roach } 46194b0938SGreg Roach } 47194b0938SGreg Roach } 48194b0938SGreg Roach 49194b0938SGreg Roach /** 50194b0938SGreg Roach * @param GedcomRecord $record 51194b0938SGreg Roach * 52194b0938SGreg Roach * @return string|null 53194b0938SGreg Roach */ 54194b0938SGreg Roach public function make(GedcomRecord $record): ?string 55194b0938SGreg Roach { 56194b0938SGreg Roach $slug = strip_tags($record->fullName()); 57194b0938SGreg Roach 58194b0938SGreg Roach if ($this->transliterator instanceof Transliterator) { 59194b0938SGreg Roach $slug = $this->transliterator->transliterate($slug); 60*4502a888SGreg Roach 61*4502a888SGreg Roach if ($slug === false) { 62*4502a888SGreg Roach return null; 63*4502a888SGreg Roach } 64194b0938SGreg Roach } 65194b0938SGreg Roach 66194b0938SGreg Roach $slug = preg_replace('/[^A-Za-z0-9]+/', '-', $slug); 67194b0938SGreg Roach $slug = trim($slug, '-'); 68194b0938SGreg Roach 69194b0938SGreg Roach if ($slug !== '') { 70194b0938SGreg Roach return $slug; 71194b0938SGreg Roach } 72194b0938SGreg Roach 73194b0938SGreg Roach return null; 74194b0938SGreg Roach } 75194b0938SGreg Roach} 76