Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.55% covered (success)
96.55%
28 / 29
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Json
96.43% covered (success)
96.43%
27 / 28
50.00% covered (danger)
50.00%
1 / 2
4
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 generate
96.00% covered (success)
96.00%
24 / 25
0.00% covered (danger)
0.00%
0 / 1
3
1<?php
2
3/**
4 * JSON Export class for phpMyFAQ.
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 2015-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     2015-12-29
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Export;
21
22use phpMyFAQ\Category;
23use phpMyFAQ\Configuration;
24use phpMyFAQ\Date;
25use phpMyFAQ\Export;
26use phpMyFAQ\Faq;
27use phpMyFAQ\Strings;
28
29define(constant_name: 'FAQ_QUERY_TYPE_EXPORT_JSON', value: 'faq_export_json');
30
31/**
32 * Class JSON
33 *
34 * @package phpMyFAQ\Export
35 */
36class Json extends Export
37{
38    /**
39     * Constructor.
40     *
41     * @param Faq           $faq           FaqHelper object
42     * @param Category      $category      Entity object
43     * @param Configuration $configuration Configuration
44     */
45    public function __construct(Faq $faq, Category $category, Configuration $configuration)
46    {
47        $this->faq = $faq;
48        $this->category = $category;
49        $this->config = $configuration;
50    }
51
52    /**
53     * Generates the export.
54     *
55     * @param int    $categoryId Entity Id
56     * @param bool   $downwards If true, downwards, otherwise upward ordering
57     * @param string $language Language
58     * @throws \JsonException
59     */
60    public function generate(int $categoryId = 0, bool $downwards = true, string $language = ''): string
61    {
62        $generated = [];
63
64        // Initialize categories
65        $this->category->transform($categoryId);
66
67        $faqData = $this->faq->get(
68            queryType: 'faq_export_json',
69            categoryId: $categoryId,
70            downwards: $downwards,
71            lang: $language,
72        );
73
74        foreach ($faqData as $data) {
75            if (!is_array($data)) {
76                continue;
77            }
78
79            $generated[] = [
80                'faq' => [
81                    'id' => (int) ($data['id'] ?? 0),
82                    'language' => (string) ($data['lang'] ?? ''),
83                    'category' => $this->category->getPath((int) ($data['category_id'] ?? 0), separator: ' >> '),
84                    'keywords' => (string) ($data['keywords'] ?? ''),
85                    'question' => strip_tags((string) ($data['topic'] ?? '')),
86                    'answer' => Strings::htmlspecialchars((string) ($data['content'] ?? '')),
87                    'author' => (string) ($data['author_name'] ?? ''),
88                    'last_modified' => Date::createIsoDate((string) ($data['lastmodified'] ?? '')),
89                ],
90            ];
91        }
92
93        header(header: 'Content-type: application/json');
94
95        return json_encode($generated, JSON_THROW_ON_ERROR);
96    }
97}