xref: /webtrees/app/Html.php (revision bb1b9730fa108eac14848a334410c0d5b3358355)
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
21use function http_build_query;
22
23use const PHP_QUERY_RFC3986;
24
25/**
26 * Class Html - Add HTML markup to elements consistently.
27 */
28class Html
29{
30    /**
31     * Convert an array of HTML attributes to an HTML string.
32     *
33     * @param mixed[] $attributes
34     *
35     * @return string
36     */
37    public static function attributes(array $attributes): string
38    {
39        $html = [];
40        foreach ($attributes as $key => $value) {
41            if (is_string($value)) {
42                $html[] = e($key) . '="' . e($value) . '"';
43            } elseif (is_int($value)) {
44                $html[] = e($key) . '="' . $value . '"';
45            } elseif ($value !== false) {
46                $html[] = e($key);
47            }
48        }
49
50        return implode(' ', $html);
51    }
52
53    /**
54     * Encode a URL.
55     *
56     * @param string $path
57     * @param array  $data
58     *
59     * @return string
60     */
61    public static function url($path, array $data): string
62    {
63        $path = str_replace(' ', '%20', $path);
64
65        if (!empty($data)) {
66            $path .= '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
67        }
68
69        return $path;
70    }
71
72    /**
73     * Filenames are (almost?) always LTR, even on RTL systems.
74     *
75     * @param string $filename
76     *
77     * @return string
78     */
79    public static function filename($filename): string
80    {
81        return '<samp class="filename" dir="ltr">' . e($filename) . '</samp>';
82    }
83}
84