Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
Mime | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
fromFileName | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core; |
6 | |
7 | use PeServer\Core\Errors\ErrorHandler; |
8 | use PeServer\Core\Throws\ArgumentException; |
9 | use PeServer\Core\Throws\FileNotFoundException; |
10 | |
11 | /** |
12 | * まいむ。 |
13 | * |
14 | * あれこれ使いまわすやつの入力ミスを減らすために定義してるだけ。 |
15 | * 大量に定義して管理するようなことはしない。 |
16 | */ |
17 | abstract class Mime |
18 | { |
19 | #region define |
20 | |
21 | public const TEXT = 'text/plain'; |
22 | public const HTML = 'text/html'; |
23 | public const JSON = 'application/json'; |
24 | public const GZ = 'application/gzip'; |
25 | public const ZIP = 'application/zip'; |
26 | public const STREAM = 'application/octet-stream'; |
27 | public const FORM = 'application/x-www-form-urlencoded'; |
28 | public const ICON = 'image/vnd.microsoft.icon'; |
29 | public const SQLITE3 = 'application/x-sqlite3'; |
30 | public const SVG = 'image/svg+xml'; |
31 | |
32 | #endregion |
33 | |
34 | #region function |
35 | |
36 | /** |
37 | * ファイルからMIMEを取得。 |
38 | * |
39 | * `mime_content_type` ラッパー。 |
40 | * |
41 | * @param string $fileName |
42 | * @return non-empty-string |
43 | * @throws ArgumentException |
44 | * @throws FileNotFoundException |
45 | * @see https://php.net/manual/function.mime-content-type.php |
46 | */ |
47 | public static function fromFileName(string $fileName): string |
48 | { |
49 | if (Text::isNullOrWhiteSpace($fileName)) { |
50 | throw new ArgumentException($fileName); |
51 | } |
52 | |
53 | $result = ErrorHandler::trap(fn () => mime_content_type($fileName)); |
54 | if (!$result->success || Text::isNullOrEmpty($result->value)) { |
55 | throw new FileNotFoundException($fileName); |
56 | } |
57 | |
58 | return $result->value; |
59 | } |
60 | |
61 | #endregion |
62 | } |