xref: /webtrees/app/Html.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1cd937586SGreg Roach<?php
2*3976b470SGreg Roach
3cd937586SGreg Roach/**
4cd937586SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6cd937586SGreg Roach * This program is free software: you can redistribute it and/or modify
7cd937586SGreg Roach * it under the terms of the GNU General Public License as published by
8cd937586SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9cd937586SGreg Roach * (at your option) any later version.
10cd937586SGreg Roach * This program is distributed in the hope that it will be useful,
11cd937586SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12cd937586SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13cd937586SGreg Roach * GNU General Public License for more details.
14cd937586SGreg Roach * You should have received a copy of the GNU General Public License
15cd937586SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16cd937586SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees;
20cd937586SGreg Roach
21cd937586SGreg Roach/**
22cd937586SGreg Roach * Class Html - Add HTML markup to elements consistently.
23cd937586SGreg Roach */
24c1010edaSGreg Roachclass Html
25c1010edaSGreg Roach{
26cd937586SGreg Roach    /**
2715d603e7SGreg Roach     * Convert an array of HTML attributes to an HTML string.
2815d603e7SGreg Roach     *
296c1eebecSGreg Roach     * @param mixed[] $attributes
3015d603e7SGreg Roach     *
3115d603e7SGreg Roach     * @return string
3215d603e7SGreg Roach     */
338f53f488SRico Sonntag    public static function attributes(array $attributes): string
34c1010edaSGreg Roach    {
3515d603e7SGreg Roach        $html = [];
3615d603e7SGreg Roach        foreach ($attributes as $key => $value) {
376c1eebecSGreg Roach            if (is_string($value)) {
3857887794SGreg Roach                $html[] = e($key) . '="' . e($value) . '"';
396c1eebecSGreg Roach            } elseif (is_int($value)) {
40e364afe4SGreg Roach                $html[] = e($key) . '="' . $value . '"';
4115d603e7SGreg Roach            } elseif ($value !== false) {
4257887794SGreg Roach                $html[] = e($key);
4315d603e7SGreg Roach            }
4415d603e7SGreg Roach        }
4515d603e7SGreg Roach
4615d603e7SGreg Roach        return implode(' ', $html);
4715d603e7SGreg Roach    }
481e582591SGreg Roach
491e582591SGreg Roach    /**
501e582591SGreg Roach     * Encode a URL.
511e582591SGreg Roach     *
52c347c1e5SGreg Roach     * @param string $path
53bafb644fSGreg Roach     * @param array  $data
541e582591SGreg Roach     *
551e582591SGreg Roach     * @return string
561e582591SGreg Roach     */
578f53f488SRico Sonntag    public static function url($path, array $data): string
58c1010edaSGreg Roach    {
59e364afe4SGreg Roach        $path = str_replace(' ', '%20', $path);
60530749aaSGreg Roach
6146c324acSGreg Roach        return $path . '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
621e582591SGreg Roach    }
631e582591SGreg Roach
6415d603e7SGreg Roach    /**
65cd937586SGreg Roach     * Filenames are (almost?) always LTR, even on RTL systems.
66cd937586SGreg Roach     *
67cd937586SGreg Roach     * @param string $filename
68cd937586SGreg Roach     *
69cd937586SGreg Roach     * @return string
70cd937586SGreg Roach     */
718f53f488SRico Sonntag    public static function filename($filename): string
72c1010edaSGreg Roach    {
7357887794SGreg Roach        return '<samp class="filename" dir="ltr">' . e($filename) . '</samp>';
74cd937586SGreg Roach    }
75cd937586SGreg Roach}
76