xref: /webtrees/app/Html.php (revision cc5ab399cc3c8f78158c8e3027cb0ecf038648dc)
1cd937586SGreg Roach<?php
2cd937586SGreg Roach/**
3cd937586SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
5cd937586SGreg Roach * This program is free software: you can redistribute it and/or modify
6cd937586SGreg Roach * it under the terms of the GNU General Public License as published by
7cd937586SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8cd937586SGreg Roach * (at your option) any later version.
9cd937586SGreg Roach * This program is distributed in the hope that it will be useful,
10cd937586SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11cd937586SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12cd937586SGreg Roach * GNU General Public License for more details.
13cd937586SGreg Roach * You should have received a copy of the GNU General Public License
14cd937586SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15cd937586SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17cd937586SGreg Roach
18cd937586SGreg Roach/**
19cd937586SGreg Roach * Class Html - Add HTML markup to elements consistently.
20cd937586SGreg Roach */
21cd937586SGreg Roachclass Html {
22cd937586SGreg Roach	/**
2315d603e7SGreg Roach	 * Escape a string for inclusion within HTML.
2415d603e7SGreg Roach	 *
2515d603e7SGreg Roach	 * @param $string
2615d603e7SGreg Roach	 *
2715d603e7SGreg Roach	 * @return string
2815d603e7SGreg Roach	 */
29*cc5ab399SGreg Roach	public static function escape($string) {
3015d603e7SGreg Roach		return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
3115d603e7SGreg Roach	}
3215d603e7SGreg Roach
3315d603e7SGreg Roach	/**
3415d603e7SGreg Roach	 * Convert an array of HTML attributes to an HTML string.
3515d603e7SGreg Roach	 *
3615d603e7SGreg Roach	 * @param array $attributes
3715d603e7SGreg Roach	 *
3815d603e7SGreg Roach	 * @return string
3915d603e7SGreg Roach	 */
4015d603e7SGreg Roach	public static function attributes(array $attributes) {
4115d603e7SGreg Roach		$html = [];
4215d603e7SGreg Roach		foreach ($attributes as $key => $value) {
4315d603e7SGreg Roach			if (is_string($value) || is_integer($value)) {
4415d603e7SGreg Roach				$html[] = self::escape($key) . '="' . self::escape($value) . '"';
4515d603e7SGreg Roach			} elseif ($value !== false) {
4615d603e7SGreg Roach				$html[] = self::escape($key);
4715d603e7SGreg Roach			}
4815d603e7SGreg Roach		}
4915d603e7SGreg Roach
5015d603e7SGreg Roach		return implode(' ', $html);
5115d603e7SGreg Roach	}
5215d603e7SGreg Roach	/**
53cd937586SGreg Roach	 * Filenames are (almost?) always LTR, even on RTL systems.
54cd937586SGreg Roach	 *
55cd937586SGreg Roach	 * @param string $filename
56cd937586SGreg Roach	 *
57cd937586SGreg Roach	 * @return string
58cd937586SGreg Roach	 */
59cd937586SGreg Roach	public static function filename($filename) {
6015d603e7SGreg Roach		return '<samp class="filename" dir="ltr">' . self::escape($filename) . '</samp>';
61cd937586SGreg Roach	}
62cd937586SGreg Roach}
63