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