xref: /webtrees/app/Html.php (revision 873953697c930fadbf3243d2b8c0029fd684da0e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * Class Html - Add HTML markup to elements consistently.
20 */
21class Html
22{
23    /**
24     * Convert an array of HTML attributes to an HTML string.
25     *
26     * @param array $attributes
27     *
28     * @return string
29     */
30    public static function attributes(array $attributes): string
31    {
32        $html = [];
33        foreach ($attributes as $key => $value) {
34            if (is_string($value) || is_integer($value)) {
35                $html[] = e($key) . '="' . e($value) . '"';
36            } elseif ($value !== false) {
37                $html[] = e($key);
38            }
39        }
40
41        return implode(' ', $html);
42    }
43
44    /**
45     * Encode a URL.
46     *
47     * @param string $path
48     * @param array  $data
49     *
50     * @return string
51     */
52    public static function url($path, array $data): string
53    {
54        $path = strtr($path, ' ', '%20');
55
56        return $path . '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
57    }
58
59    /**
60     * Filenames are (almost?) always LTR, even on RTL systems.
61     *
62     * @param string $filename
63     *
64     * @return string
65     */
66    public static function filename($filename): string
67    {
68        return '<samp class="filename" dir="ltr">' . e($filename) . '</samp>';
69    }
70}
71