xref: /webtrees/app/Html.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees;
20
21/**
22 * Class Html - Add HTML markup to elements consistently.
23 */
24class Html
25{
26    /**
27     * Convert an array of HTML attributes to an HTML string.
28     *
29     * @param mixed[] $attributes
30     *
31     * @return string
32     */
33    public static function attributes(array $attributes): string
34    {
35        $html = [];
36        foreach ($attributes as $key => $value) {
37            if (is_string($value)) {
38                $html[] = e($key) . '="' . e($value) . '"';
39            } elseif (is_int($value)) {
40                $html[] = e($key) . '="' . $value . '"';
41            } elseif ($value !== false) {
42                $html[] = e($key);
43            }
44        }
45
46        return implode(' ', $html);
47    }
48
49    /**
50     * Encode a URL.
51     *
52     * @param string $path
53     * @param array  $data
54     *
55     * @return string
56     */
57    public static function url($path, array $data): string
58    {
59        $path = str_replace(' ', '%20', $path);
60
61        return $path . '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
62    }
63
64    /**
65     * Filenames are (almost?) always LTR, even on RTL systems.
66     *
67     * @param string $filename
68     *
69     * @return string
70     */
71    public static function filename($filename): string
72    {
73        return '<samp class="filename" dir="ltr">' . e($filename) . '</samp>';
74    }
75}
76