1194b0938SGreg Roach<?php 2194b0938SGreg Roach 3194b0938SGreg Roach/** 4194b0938SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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; 2710e06497SGreg Roachuse function in_array; 28194b0938SGreg Roachuse function preg_replace; 29194b0938SGreg Roachuse function strip_tags; 30194b0938SGreg Roachuse function trim; 31194b0938SGreg Roach 32194b0938SGreg Roach/** 33194b0938SGreg Roach * Make a slug to be used in the URL of a GedcomRecord. 34194b0938SGreg Roach */ 35194b0938SGreg Roachclass SlugFactory implements SlugFactoryInterface 36194b0938SGreg Roach{ 37*1ff45046SGreg Roach private Transliterator|null $transliterator = null; 38194b0938SGreg Roach 39194b0938SGreg Roach public function __construct() 40194b0938SGreg Roach { 41194b0938SGreg Roach if (extension_loaded('intl')) { 42194b0938SGreg Roach $ids = Transliterator::listIDs(); 43194b0938SGreg Roach 4474be9061SGreg Roach if ($ids !== false && in_array('Any-Latin', $ids, true) && in_array('Latin-ASCII', $ids, true)) { 45194b0938SGreg Roach $this->transliterator = Transliterator::create('Any-Latin;Latin-ASCII'); 46194b0938SGreg Roach } 47194b0938SGreg Roach } 48194b0938SGreg Roach } 49194b0938SGreg Roach 50194b0938SGreg Roach /** 51194b0938SGreg Roach * @param GedcomRecord $record 52194b0938SGreg Roach * 5319033cfeSGreg Roach * @return string 54194b0938SGreg Roach */ 5519033cfeSGreg Roach public function make(GedcomRecord $record): string 56194b0938SGreg Roach { 57194b0938SGreg Roach $slug = strip_tags($record->fullName()); 58194b0938SGreg Roach 59194b0938SGreg Roach if ($this->transliterator instanceof Transliterator) { 60194b0938SGreg Roach $slug = $this->transliterator->transliterate($slug); 614502a888SGreg Roach 624502a888SGreg Roach if ($slug === false) { 6319033cfeSGreg Roach return ''; 644502a888SGreg Roach } 65194b0938SGreg Roach } 66194b0938SGreg Roach 67194b0938SGreg Roach $slug = preg_replace('/[^A-Za-z0-9]+/', '-', $slug); 68194b0938SGreg Roach 6919033cfeSGreg Roach return trim($slug, '-'); 70194b0938SGreg Roach } 71194b0938SGreg Roach} 72