Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
SearchStrategy
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
1 / 1
 build
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3/**
4 * Building search URL segments for phpMyFAQ Link rewriting.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2025 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2025-11-08
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Link\Strategy;
21
22use phpMyFAQ\Link;
23
24final class SearchStrategy implements StrategyInterface
25{
26    /**
27     * @param array<string,string> $params
28     */
29    public function build(array $params, Link $link): string
30    {
31        $hasSearch = array_key_exists(Link::LINK_GET_ACTION_SEARCH, $params);
32        $isTag = !$hasSearch && array_key_exists(Link::LINK_GET_TAGGING_ID, $params);
33
34        $url = '';
35        if ($isTag) {
36            $url = Link::LINK_TAGS . $params[Link::LINK_GET_TAGGING_ID];
37            if (array_key_exists(Link::LINK_GET_PAGE, $params)) {
38                $url .= Link::LINK_HTML_SLASH . $params[Link::LINK_GET_PAGE];
39            }
40
41            $url .= Link::LINK_SLASH . $link->getSEOTitle() . Link::LINK_HTML_EXTENSION;
42        }
43
44        if (!$isTag) {
45            $url = Link::LINK_HTML_SEARCH;
46            if ($hasSearch) {
47                $url .=
48                    Link::LINK_SEARCHPART_SEPARATOR
49                    . Link::LINK_GET_ACTION_SEARCH
50                    . '='
51                    . (string) $params[Link::LINK_GET_ACTION_SEARCH];
52                if (array_key_exists(Link::LINK_GET_PAGE, $params)) {
53                    $url .= Link::LINK_AMPERSAND . Link::LINK_GET_PAGE . '=' . (string) $params[Link::LINK_GET_PAGE];
54                }
55            }
56        }
57
58        if (array_key_exists(Link::LINK_GET_LANGS, $params)) {
59            $url .= Link::LINK_AMPERSAND . Link::LINK_GET_LANGS . '=' . (string) $params[Link::LINK_GET_LANGS];
60        }
61
62        return $url;
63    }
64}