xref: /webtrees/app/Html.php (revision d11be7027e34e3121be11cc025421873364403f9)
1cd937586SGreg Roach<?php
23976b470SGreg Roach
3cd937586SGreg Roach/**
4cd937586SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16cd937586SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21cd937586SGreg Roach
22dec352c1SGreg Roachuse function e;
231dc950abSGreg Roachuse function http_build_query;
24dec352c1SGreg Roachuse function implode;
25dec352c1SGreg Roachuse function is_int;
26dec352c1SGreg Roachuse function is_string;
27dec352c1SGreg Roachuse function strtr;
281dc950abSGreg Roach
291dc950abSGreg Roachuse const PHP_QUERY_RFC3986;
301dc950abSGreg Roach
31cd937586SGreg Roach/**
32cd937586SGreg Roach * Class Html - Add HTML markup to elements consistently.
33cd937586SGreg Roach */
34c1010edaSGreg Roachclass Html
35c1010edaSGreg Roach{
36cd937586SGreg Roach    /**
3715d603e7SGreg Roach     * Convert an array of HTML attributes to an HTML string.
3815d603e7SGreg Roach     *
397dca5265SGreg Roach     * @param array<string,string|int|bool> $attributes
4015d603e7SGreg Roach     *
4115d603e7SGreg Roach     * @return string
4215d603e7SGreg Roach     */
438f53f488SRico Sonntag    public static function attributes(array $attributes): string
44c1010edaSGreg Roach    {
4515d603e7SGreg Roach        $html = [];
4615d603e7SGreg Roach        foreach ($attributes as $key => $value) {
476c1eebecSGreg Roach            if (is_string($value)) {
4857887794SGreg Roach                $html[] = e($key) . '="' . e($value) . '"';
496c1eebecSGreg Roach            } elseif (is_int($value)) {
50e364afe4SGreg Roach                $html[] = e($key) . '="' . $value . '"';
517dca5265SGreg Roach            } elseif ($value === true) {
527dca5265SGreg Roach                $html[] = e($key) . '="' . e($key) . '"';
5315d603e7SGreg Roach            }
5415d603e7SGreg Roach        }
5515d603e7SGreg Roach
5615d603e7SGreg Roach        return implode(' ', $html);
5715d603e7SGreg Roach    }
581e582591SGreg Roach
591e582591SGreg Roach    /**
601e582591SGreg Roach     * Encode a URL.
611e582591SGreg Roach     *
62c347c1e5SGreg Roach     * @param string                                    $path
6376d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $data
641e582591SGreg Roach     *
651e582591SGreg Roach     * @return string
661e582591SGreg Roach     */
6724f2a3afSGreg Roach    public static function url(string $path, array $data): string
68c1010edaSGreg Roach    {
69dec352c1SGreg Roach        $path = strtr($path, [' ' => '%20']);
70530749aaSGreg Roach
7154c1ab5eSGreg Roach        if ($data !== []) {
721dc950abSGreg Roach            $path .= '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986);
731dc950abSGreg Roach        }
741dc950abSGreg Roach
751dc950abSGreg Roach        return $path;
761e582591SGreg Roach    }
771e582591SGreg Roach
7815d603e7SGreg Roach    /**
79cd937586SGreg Roach     * Filenames are (almost?) always LTR, even on RTL systems.
80cd937586SGreg Roach     *
81cd937586SGreg Roach     * @param string $filename
82cd937586SGreg Roach     *
83cd937586SGreg Roach     * @return string
84cd937586SGreg Roach     */
8524f2a3afSGreg Roach    public static function filename(string $filename): string
86c1010edaSGreg Roach    {
8757887794SGreg Roach        return '<samp class="filename" dir="ltr">' . e($filename) . '</samp>';
88cd937586SGreg Roach    }
89cd937586SGreg Roach}
90