diff --git a/src/Filesystem/Mime.php b/src/Filesystem/Mime.php index c7344a485a..2f48331c18 100644 --- a/src/Filesystem/Mime.php +++ b/src/Filesystem/Mime.php @@ -270,36 +270,35 @@ public static function toExtension(string|null $mime = null): string|false /** * Returns all available extensions for a given MIME type */ - public static function toExtensions(string|null $mime = null, bool $matchWildcards = false): array - { - $extensions = []; - $testMime = fn (string $v) => static::matches($v, $mime); + public static function toExtensions( + string|null $mime = null, + bool $matchWildcards = false + ): array { + // get all extensions + $extensions = array_keys(static::$types); + + // filter extensions for given MIME type + $extensions = A::filter( + $extensions, + function ($extension) use ($mime, $matchWildcards) { + // get corresponding MIME types as array + $mimes = A::wrap(static::$types[$extension]); - foreach (static::$types as $key => $value) { - if (is_array($value) === true) { if ($matchWildcards === true) { - if (A::some($value, $testMime)) { - $extensions[] = $key; - } - } else { - if (in_array($mime, $value) === true) { - $extensions[] = $key; - } - } - } else { - if ($matchWildcards === true) { - if ($testMime($value) === true) { - $extensions[] = $key; - } - } else { - if ($value === $mime) { - $extensions[] = $key; - } + // check if at least one MIME type with wildcards matches + return A::some( + $mimes, + fn (string $v): bool => static::matches($v, $mime) + ); } + + // check if at least one MIME type matches exactly + return in_array($mime, $mimes); } - } + ); - return $extensions; + // renumber array with consecutive keys + return array_values($extensions); } /** diff --git a/tests/Filesystem/MimeTest.php b/tests/Filesystem/MimeTest.php index b0025cb956..35b274a942 100644 --- a/tests/Filesystem/MimeTest.php +++ b/tests/Filesystem/MimeTest.php @@ -130,21 +130,31 @@ public function testToExtensions() $extensions = Mime::toExtensions('text/css'); $this->assertSame(['css'], $extensions); + } - // test matchWildcards: false (default value) + /** + * @covers ::toExtensions + */ + public function testToExtensionsMatchWildcards() + { + // matchWildcards: false (default value) $extensions = Mime::toExtensions('image/*'); $this->assertCount(0, $extensions); - // test matchWildcards: true + // matchWildcards: true $extensions = Mime::toExtensions('image/*', true); - // use we only check for a positive and negative subset instead of a complete list, - // this should make sure the test doesn't break when a new image type is added - $shouldContain = ['jpg', 'jpeg', 'gif', 'png']; - $shouldNotContain = ['js', 'pdf', 'zip', 'docx']; - foreach ($shouldContain as $ext) { + + // we only check for a positive and negative subset + // instead of a complete list to make sure the test + // doesn't break when a new image type is added + + // should contain + foreach (['jpg', 'jpeg', 'gif', 'png'] as $ext) { $this->assertContains($ext, $extensions); } - foreach ($shouldNotContain as $ext) { + + // should not contain + foreach (['js', 'pdf', 'zip', 'docx'] as $ext) { $this->assertNotContains($ext, $extensions); } }