xref: /webtrees/app/Html.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
1cd937586SGreg Roach<?php
2cd937586SGreg Roach/**
3cd937586SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5cd937586SGreg Roach * This program is free software: you can redistribute it and/or modify
6cd937586SGreg Roach * it under the terms of the GNU General Public License as published by
7cd937586SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8cd937586SGreg Roach * (at your option) any later version.
9cd937586SGreg Roach * This program is distributed in the hope that it will be useful,
10cd937586SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11cd937586SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12cd937586SGreg Roach * GNU General Public License for more details.
13cd937586SGreg Roach * You should have received a copy of the GNU General Public License
14cd937586SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15cd937586SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19cd937586SGreg Roach
20cd937586SGreg Roach/**
21cd937586SGreg Roach * Class Html - Add HTML markup to elements consistently.
22cd937586SGreg Roach */
23c1010edaSGreg Roachclass Html
24c1010edaSGreg Roach{
25cd937586SGreg Roach    /**
2615d603e7SGreg Roach     * Convert an array of HTML attributes to an HTML string.
2715d603e7SGreg Roach     *
286c1eebecSGreg Roach     * @param mixed[] $attributes
2915d603e7SGreg Roach     *
3015d603e7SGreg Roach     * @return string
3115d603e7SGreg Roach     */
328f53f488SRico Sonntag    public static function attributes(array $attributes): string
33c1010edaSGreg Roach    {
3415d603e7SGreg Roach        $html = [];
3515d603e7SGreg Roach        foreach ($attributes as $key => $value) {
366c1eebecSGreg Roach            if (is_string($value)) {
3757887794SGreg Roach                $html[] = e($key) . '="' . e($value) . '"';
386c1eebecSGreg Roach            } elseif (is_int($value)) {
396c1eebecSGreg Roach                $html[] = e($key) . '="' . (string) $value . '"';
4015d603e7SGreg Roach            } elseif ($value !== false) {
4157887794SGreg Roach                $html[] = e($key);
4215d603e7SGreg Roach            }
4315d603e7SGreg Roach        }
4415d603e7SGreg Roach
4515d603e7SGreg Roach        return implode(' ', $html);
4615d603e7SGreg Roach    }
471e582591SGreg Roach
481e582591SGreg Roach    /**
491e582591SGreg Roach     * Encode a URL.
501e582591SGreg Roach     *
51c347c1e5SGreg Roach     * @param string $path
52bafb644fSGreg Roach     * @param array  $data
531e582591SGreg Roach     *
541e582591SGreg Roach     * @return string
551e582591SGreg Roach     */
568f53f488SRico Sonntag    public static function url($path, array $data): string
57c1010edaSGreg Roach    {
58530749aaSGreg Roach        $path = strtr($path, ' ', '%20');
59530749aaSGreg Roach
6046c324acSGreg Roach        return $path . '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
611e582591SGreg Roach    }
621e582591SGreg Roach
6315d603e7SGreg Roach    /**
64cd937586SGreg Roach     * Filenames are (almost?) always LTR, even on RTL systems.
65cd937586SGreg Roach     *
66cd937586SGreg Roach     * @param string $filename
67cd937586SGreg Roach     *
68cd937586SGreg Roach     * @return string
69cd937586SGreg Roach     */
708f53f488SRico Sonntag    public static function filename($filename): string
71c1010edaSGreg Roach    {
7257887794SGreg Roach        return '<samp class="filename" dir="ltr">' . e($filename) . '</samp>';
73cd937586SGreg Roach    }
74cd937586SGreg Roach}
75