Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filesystem\Mime: more readable if logic #6493

Merged
merged 4 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions src/Filesystem/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,36 +270,34 @@ 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);

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;
}
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]);

if ($matchWildcards) {
distantnative marked this conversation as resolved.
Show resolved Hide resolved
// 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;
return array_values($extensions);
distantnative marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
26 changes: 18 additions & 8 deletions tests/Filesystem/MimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Loading