Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateLinkTwigExtension
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 categoryLink
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 faqLink
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Twig extension to return the URLs for categories and FAQs.
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\Template
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2024-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     2024-08-18
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Twig\Extensions;
21
22use phpMyFAQ\Category;
23use phpMyFAQ\Configuration;
24use phpMyFAQ\Faq;
25use phpMyFAQ\Link;
26use phpMyFAQ\Link\Util\TitleSlugifier;
27use Twig\Attribute\AsTwigFilter;
28use Twig\Attribute\AsTwigFunction;
29use Twig\Extension\AbstractExtension;
30
31class CreateLinkTwigExtension extends AbstractExtension
32{
33    #[AsTwigFilter(name: 'categoryLink')]
34    #[AsTwigFunction(name: 'categoryLink')]
35    public static function categoryLink(int $categoryId): string
36    {
37        $configuration = Configuration::getConfigurationInstance();
38
39        $category = new Category($configuration);
40        $categoryEntity = $category->getCategoryData($categoryId);
41
42        $urlString = '%scategory/%d/%s.html';
43        $url = sprintf(
44            $urlString,
45            $configuration->getDefaultUrl(),
46            $categoryId,
47            TitleSlugifier::slug($categoryEntity->getName()),
48        );
49
50        $link = new Link($url, $configuration);
51        $link->setTitle($categoryEntity->getName());
52
53        return $link->toString();
54    }
55
56    #[AsTwigFilter(name: 'faqLink')]
57    #[AsTwigFunction(name: 'faqLink')]
58    public static function faqLink(int $categoryId, int $faqId, string $faqLanguage): string
59    {
60        $configuration = Configuration::getConfigurationInstance();
61
62        $faq = new Faq($configuration);
63        $question = $faq->getQuestion($faqId);
64
65        $urlString = '%scontent/%d/%d/%s/%s.html';
66        $url = sprintf(
67            $urlString,
68            $configuration->getDefaultUrl(),
69            $categoryId,
70            $faqId,
71            $faqLanguage,
72            TitleSlugifier::slug($question),
73        );
74
75        $link = new Link($url, $configuration);
76        $link->setTitle($question);
77
78        return $link->toString();
79    }
80}