Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
56 / 56
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AttachmentHelper
100.00% covered (success)
100.00%
56 / 56
100.00% covered (success)
100.00%
2 / 2
14
100.00% covered (success)
100.00%
1 / 1
 getAttachmentList
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 mapMimeTypeToIcon
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
12
1<?php
2
3/**
4 * Attachment helper 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\Helper
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2019-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     2019-12-30
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Helper;
21
22use phpMyFAQ\Attachment\AbstractAttachment;
23
24/**
25 * Class AttachmentHelper
26 * @package phpMyFAQ\Helper
27 */
28class AttachmentHelper
29{
30    /**
31     * Returns a list of attached files.
32     *
33     * @param AbstractAttachment[] $attachmentList
34     * @return array<int, array<string, string>>
35     */
36    public function getAttachmentList(array $attachmentList): array
37    {
38        if ($attachmentList === []) {
39            return [];
40        }
41
42        return array_map(fn(AbstractAttachment $attachment): array => [
43            'icon' => $this->mapMimeTypeToIcon($attachment->getMimeType()),
44            'url' => $attachment->buildUrl(),
45            'filename' => $attachment->getFilename(),
46        ], array_values($attachmentList));
47    }
48
49    private function mapMimeTypeToIcon(string $mimeType): string
50    {
51        return match ($mimeType) {
52            'application/zip' => 'file-archive-o',
53            'audio/basic',
54            'audio/midi',
55            'audio/mpeg',
56            'audio/x-aiff',
57            'audio/x-mpegurl',
58            'audio/x-pn-realaudio',
59            'audio/x-pn-realaudio-plugin',
60            'audio/x-realaudio',
61            'audio/x-wav',
62                => 'file-audio-o',
63            'application/xhtml+xml', 'text/xml' => 'file-code-o',
64            'application/vnd.ms-excel',
65            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
66                => 'file-excel-o',
67            'image/bmp',
68            'image/gif',
69            'image/ief',
70            'image/jpeg',
71            'image/png',
72            'image/tiff',
73            'image/vnd.djvu',
74            'image/vnd.wap.wbmp',
75            'image/x-cmu-raster',
76            'image/x-portable-anymap',
77            'image/x-portable-bitmap',
78            'image/x-portable-graymap',
79            'image/x-portable-pixmap',
80            'image/x-rgb',
81            'image/x-xbitmap',
82            'image/x-xpixmap',
83            'image/x-xwindowdump',
84                => 'file-image-o',
85            'application/vnd.ms-powerpoint',
86            'application/vnd.openxmlformats-officedocument.presentationml.presentation',
87                => 'file-powerpoint-o',
88            'application/pdf' => 'file-pdf-o',
89            'text/plain', 'text/richtext', 'text/rtf' => 'file-text-o',
90            'application/msword',
91            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
92                => 'file-word-o',
93            'video/mpeg',
94            'video/quicktime',
95            'video/vnd.mpegurl',
96            'video/x-msvideo',
97            'video/x-sgi-movie',
98                => 'file-video-o',
99            default => 'file-o',
100        };
101    }
102}