xref: /webtrees/app/Html.php (revision 59597b37d69e8147c3f4a27643e9c8edaa2a0592)
1cd937586SGreg Roach<?php
23976b470SGreg Roach
3cd937586SGreg Roach/**
4cd937586SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6cd937586SGreg Roach * This program is free software: you can redistribute it and/or modify
7cd937586SGreg Roach * it under the terms of the GNU General Public License as published by
8cd937586SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9cd937586SGreg Roach * (at your option) any later version.
10cd937586SGreg Roach * This program is distributed in the hope that it will be useful,
11cd937586SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12cd937586SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13cd937586SGreg Roach * GNU General Public License for more details.
14cd937586SGreg Roach * You should have received a copy of the GNU General Public License
15cd937586SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16cd937586SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21cd937586SGreg Roach
221dc950abSGreg Roachuse function http_build_query;
231dc950abSGreg Roach
241dc950abSGreg Roachuse const PHP_QUERY_RFC3986;
251dc950abSGreg Roach
26cd937586SGreg Roach/**
27cd937586SGreg Roach * Class Html - Add HTML markup to elements consistently.
28cd937586SGreg Roach */
29c1010edaSGreg Roachclass Html
30c1010edaSGreg Roach{
31cd937586SGreg Roach    /**
3215d603e7SGreg Roach     * Convert an array of HTML attributes to an HTML string.
3315d603e7SGreg Roach     *
346c1eebecSGreg Roach     * @param mixed[] $attributes
3515d603e7SGreg Roach     *
3615d603e7SGreg Roach     * @return string
3715d603e7SGreg Roach     */
388f53f488SRico Sonntag    public static function attributes(array $attributes): string
39c1010edaSGreg Roach    {
4015d603e7SGreg Roach        $html = [];
4115d603e7SGreg Roach        foreach ($attributes as $key => $value) {
426c1eebecSGreg Roach            if (is_string($value)) {
4357887794SGreg Roach                $html[] = e($key) . '="' . e($value) . '"';
446c1eebecSGreg Roach            } elseif (is_int($value)) {
45e364afe4SGreg Roach                $html[] = e($key) . '="' . $value . '"';
4615d603e7SGreg Roach            } elseif ($value !== false) {
4757887794SGreg Roach                $html[] = e($key);
4815d603e7SGreg Roach            }
4915d603e7SGreg Roach        }
5015d603e7SGreg Roach
5115d603e7SGreg Roach        return implode(' ', $html);
5215d603e7SGreg Roach    }
531e582591SGreg Roach
541e582591SGreg Roach    /**
551e582591SGreg Roach     * Encode a URL.
561e582591SGreg Roach     *
57c347c1e5SGreg Roach     * @param string  $path
58*59597b37SGreg Roach     * @param mixed[] $data
591e582591SGreg Roach     *
601e582591SGreg Roach     * @return string
611e582591SGreg Roach     */
628f53f488SRico Sonntag    public static function url($path, array $data): string
63c1010edaSGreg Roach    {
64e364afe4SGreg Roach        $path = str_replace(' ', '%20', $path);
65530749aaSGreg Roach
661dc950abSGreg Roach        if (!empty($data)) {
671dc950abSGreg Roach            $path .= '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
681dc950abSGreg Roach        }
691dc950abSGreg Roach
701dc950abSGreg Roach        return $path;
711e582591SGreg Roach    }
721e582591SGreg Roach
7315d603e7SGreg Roach    /**
74cd937586SGreg Roach     * Filenames are (almost?) always LTR, even on RTL systems.
75cd937586SGreg Roach     *
76cd937586SGreg Roach     * @param string $filename
77cd937586SGreg Roach     *
78cd937586SGreg Roach     * @return string
79cd937586SGreg Roach     */
808f53f488SRico Sonntag    public static function filename($filename): string
81c1010edaSGreg Roach    {
8257887794SGreg Roach        return '<samp class="filename" dir="ltr">' . e($filename) . '</samp>';
83cd937586SGreg Roach    }
84cd937586SGreg Roach}
85