• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

68publishers / image-storage / 15325708403

29 May 2025 02:05PM UTC coverage: 91.908% (-0.4%) from 92.317%
15325708403

push

github

tg666
Removed manual calling of `Collector::save()` in tests (`pcov` doesn't need it)

1363 of 1483 relevant lines covered (91.91%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/src/Helper/SupportedType.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\ImageStorage\Helper;
6

7
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
8
use function array_key_exists;
9
use function array_keys;
10
use function array_search;
11
use function array_unique;
12
use function array_values;
13
use function in_array;
14

15
final class SupportedType
16
{
17
    /** @var array<string, string> */
18
    private static array $supportedTypes = [
19
        'gif' => 'image/gif',
20
        'jpg' => 'image/jpeg',
21
        'jpeg' => 'image/jpeg',
22
        'pjpg' => 'image/jpeg',
23
        'png' => 'image/png',
24
        'webp' => 'image/webp',
25
        'avif' => 'image/avif',
26
    ];
27

28
    /** @var array{0: string, 1:string} */
29
    private static array $default = ['jpg', 'image/jpeg'];
30

31
    private function __construct() {}
×
32

33
    /**
34
     * @return array<string>
35
     */
36
    public static function getSupportedTypes(): array
37
    {
38
        return array_values(array_unique(self::$supportedTypes));
1✔
39
    }
40

41
    /**
42
     * @return array<string>
43
     */
44
    public static function getSupportedExtensions(): array
45
    {
46
        return array_keys(self::$supportedTypes);
1✔
47
    }
48

49
    public static function getDefaultExtension(): string
50
    {
51
        return self::$default[0];
1✔
52
    }
53

54
    public static function getDefaultType(): string
55
    {
56
        return self::$default[1];
1✔
57
    }
58

59
    public static function isTypeSupported(string $type): bool
60
    {
61
        return in_array($type, self::$supportedTypes, true);
1✔
62
    }
63

64
    public static function isExtensionSupported(string $extension): bool
65
    {
66
        return array_key_exists($extension, self::$supportedTypes);
1✔
67
    }
68

69
    /**
70
     * @throws InvalidArgumentException
71
     */
72
    public static function getTypeByExtension(string $extension): string
73
    {
74
        if (!self::isExtensionSupported($extension)) {
1✔
75
            throw new InvalidArgumentException(sprintf(
1✔
76
                'Extension .%s is not supported.',
1✔
77
                $extension,
78
            ));
79
        }
80

81
        return self::$supportedTypes[$extension];
1✔
82
    }
83

84
    /**
85
     * @throws InvalidArgumentException
86
     */
87
    public static function getExtensionByType(string $type): string
88
    {
89
        if (!self::isTypeSupported($type)) {
1✔
90
            throw new InvalidArgumentException(sprintf(
1✔
91
                'Mime type %s is not supported.',
1✔
92
                $type,
93
            ));
94
        }
95

96
        return (string) array_search($type, self::$supportedTypes, true);
1✔
97
    }
98

99
    /**
100
     * file extensions as keys and MimeTypes as values
101
     *
102
     * @param array<string, string> $types
103
     */
104
    public static function setSupportedTypes(array $types): void
105
    {
106
        self::$supportedTypes = $types;
1✔
107
    }
1✔
108

109
    public static function setDefault(string $extension, string $type): void
110
    {
111
        self::$default = [$extension, $type];
1✔
112
    }
1✔
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc