Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
TagsHelper
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 renderTagList
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 renderSearchTag
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
2
 getTaggingIds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTaggingIds
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 renderRelatedTag
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Helper class for phpMyFAQ tags.
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  2013-2026 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      2013-12-26
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Helper;
21
22use phpMyFAQ\Filter;
23use phpMyFAQ\Strings;
24
25/**
26 * Class TagsHelper
27 * @package phpMyFAQ\Helper
28 */
29class TagsHelper extends AbstractHelper
30{
31    /**
32     * @var int[] Array of Tag IDs
33     */
34    private ?array $taggingIds = null;
35
36    /**
37     * Renders the tag list.
38     *
39     * @param array<int, string> $tags Array of tags.
40     */
41    public function renderTagList(array $tags): string
42    {
43        $tagList = '';
44        foreach ($tags as $tagId => $tagName) {
45            $tagList .= $this->renderSearchTag((int) $tagId, $tagName);
46        }
47
48        return $tagList;
49    }
50
51    /**
52     * Renders a search tag.
53     *
54     * @param int    $tagId   The ID of the tag
55     * @param string $tagName The tag name
56     */
57    public function renderSearchTag(int $tagId, string $tagName): string
58    {
59        $currentIds = array_map(static fn(int $taggingId): string => (string) $taggingId, $this->getTaggingIds());
60        $taggingIds = str_replace((string) $tagId, replace: '', subject: $currentIds);
61        $taggingIds = str_replace(' ', replace: '', subject: $taggingIds);
62        $taggingIds = str_replace(',,', replace: ',', subject: $taggingIds);
63        $taggingIds = trim(implode(separator: ',', array: $taggingIds), characters: ',');
64
65        return $taggingIds !== ''
66            ? sprintf(
67                '<a class="btn btn-outline-primary m-1" href="./search.html?tagging_id=%s">%s '
68                . '<i aria-hidden="true" class="bi bi-dash-square"></i></a> ',
69                $taggingIds,
70                Strings::htmlentities($tagName),
71            )
72            : sprintf(
73                '<a class="btn btn-outline-primary m-1" href="./search.html?search=%s">%s '
74                . '<i aria-hidden="true" class="bi bi-dash-square"></i></a> ',
75                Strings::htmlentities($tagName),
76                Strings::htmlentities($tagName),
77            );
78    }
79
80    /**
81     * Returns all tag IDs as an array.
82     *
83     * @return int[]
84     */
85    public function getTaggingIds(): array
86    {
87        return $this->taggingIds ?? [];
88    }
89
90    /**
91     * Sets the tag IDs.
92     *
93     * @param array<array-key, int|string> $taggingIds The tag IDs as array
94     */
95    public function setTaggingIds(array $taggingIds): void
96    {
97        $this->taggingIds = array_map(
98            static fn(int|string $tagId): int => (int) $tagId,
99            array_filter(
100                $taggingIds,
101                static fn(int|string $tagId): bool => (bool) Filter::filterVar($tagId, FILTER_VALIDATE_INT),
102            ),
103        );
104    }
105
106    /**
107     * Renders the related tag.
108     *
109     * @param int     $tagId     The given Tag ID.
110     * @param string  $tagName   The name of the tag.
111     * @param int     $relevance The relevance of the tag.
112     */
113    public function renderRelatedTag(int $tagId, string $tagName, int $relevance): string
114    {
115        return sprintf(
116            '<a class="btn btn-outline-primary m-1" href="./search.html?tagging_id=%s">%s %s '
117            . '<span class="badge bg-info">%d</span></a>',
118            implode(',', $this->getTaggingIds()) . ',' . $tagId,
119            '<i aria-hidden="true" class="bi bi-plus-square"></i> ',
120            Strings::htmlentities($tagName),
121            $relevance,
122        );
123    }
124}