Skip to content

Commit

Permalink
Merge pull request #6493 from getkirby/v5/refactor/cs-mime
Browse files Browse the repository at this point in the history
`Filesystem\Mime`: more readable if logic
  • Loading branch information
distantnative authored Jun 22, 2024
2 parents 8982b6f + f8a08cc commit c8bfe23
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
49 changes: 24 additions & 25 deletions src/Filesystem/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
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

0 comments on commit c8bfe23

Please sign in to comment.