Lines 97.05% 33 / 34
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 3 / 3 100.00% 1 / 1 1
 generate 96.00% 24 / 25 0.00% 0 / 1 3
 [phpMyFAQ\Export] create 100.00% 4 / 4 100.00% 1 / 1 4
 [phpMyFAQ\Export] getExportTimestamp 100.00% 2 / 2 100.00% 1 / 1 2
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}

Inherited from phpMyFAQ\Export

45    public static function create(
46        Faq $faq,
47        Category $category,
48        Configuration $configuration,
49        string $mode = 'pdf',
50    ): Pdf|Json {
51        return match ($mode) {
52            'json' => new Json($faq, $category, $configuration),
53            'pdf' => new Pdf($faq, $category, $configuration),
54            default => throw new Exception(message: 'Export not implemented!'),
55        };
56    }
61    public static function getExportTimestamp(): string
62    {
63        $requestTime = Request::createFromGlobals()->server->get(key: 'REQUEST_TIME');
64
65        return date(format: 'Y-m-d-H-i-s', timestamp: is_numeric($requestTime) ? (int) $requestTime : time());
66    }