Skip to content

Commit

Permalink
Merge pull request #24 from thephpleague/feature/reverse-lookup
Browse files Browse the repository at this point in the history
[Feature] Reverse lookup
  • Loading branch information
frankdejonge authored Aug 5, 2023
2 parents c7f2872 + 7c26dd1 commit 545d4b4
Show file tree
Hide file tree
Showing 11 changed files with 1,162 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/quality-assurance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '7.2', '7.3', '7.4', '8.0' ]
php: [ '7.4', '8.0', '8.1', '8.2' ]
composer-flags: [ '' ]
upgrade-aws-sdk: [ 'no' ]
phpunit-flags: [ '--coverage-text' ]
include:
- php: '7.2'
- php: '7.4'
composer-flags: '--prefer-lowest'
phpunit-flags: '--no-coverage'
steps:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"phpstan": "vendor/bin/phpstan analyse -l 6 src"
},
"require": {
"php": "^7.2 || ^8.0",
"php": "^7.4 || ^8.0",
"ext-fileinfo": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.5.8 || ^9.3",
"phpunit/phpunit": "^8.5.8 || ^9.3 || ^10.0",
"phpstan/phpstan": "^0.12.68",
"friendsofphp/php-cs-fixer": "^3.2"
},
Expand All @@ -28,7 +28,7 @@
},
"config": {
"platform": {
"php": "7.2.0"
"php": "7.4.0"
}
}
}
14 changes: 14 additions & 0 deletions src/ExtensionLookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace League\MimeTypeDetection;

interface ExtensionLookup
{
public function lookupExtension(string $mimetype): ?string;

/**
* @return string[]
*/
public function lookupAllExtensions(string $mimetype): array;
}
16 changes: 15 additions & 1 deletion src/ExtensionMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use const PATHINFO_EXTENSION;

class ExtensionMimeTypeDetector implements MimeTypeDetector
class ExtensionMimeTypeDetector implements MimeTypeDetector, ExtensionLookup
{
/**
* @var ExtensionToMimeTypeMap
Expand Down Expand Up @@ -39,4 +39,18 @@ public function detectMimeTypeFromBuffer(string $contents): ?string
{
return null;
}

public function lookupExtension(string $mimetype): ?string
{
return $this->extensions instanceof ExtensionLookup
? $this->extensions->lookupExtension($mimetype)
: null;
}

public function lookupAllExtensions(string $mimetype): array
{
return $this->extensions instanceof ExtensionLookup
? $this->extensions->lookupAllExtensions($mimetype)
: [];
}
}
3 changes: 2 additions & 1 deletion src/ExtensionMimeTypeDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ExtensionMimeTypeDetectorTest extends TestCase
{
/**
* @test
*
* @dataProvider expectedLookupResults
*/
public function looking_up_mimetype(string $path, ?string $expectedMimeType): void
Expand All @@ -35,7 +36,7 @@ public function detecting_from_bugger_always_returns_null(): void
$this->assertNull($mimeType);
}

public function expectedLookupResults(): Generator
public static function expectedLookupResults(): Generator
{
yield ['thing.jpg', 'image/jpeg'];
yield ['file.svg', 'image/svg+xml'];
Expand Down
16 changes: 15 additions & 1 deletion src/FinfoMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use const PATHINFO_EXTENSION;
use finfo;

class FinfoMimeTypeDetector implements MimeTypeDetector
class FinfoMimeTypeDetector implements MimeTypeDetector, ExtensionLookup
{
private const INCONCLUSIVE_MIME_TYPES = [
'application/x-empty',
Expand Down Expand Up @@ -89,4 +89,18 @@ private function takeSample(string $contents): string

return (string) substr($contents, 0, $this->bufferSampleSize);
}

public function lookupExtension(string $mimetype): ?string
{
return $this->extensionMap instanceof ExtensionLookup
? $this->extensionMap->lookupExtension($mimetype)
: null;
}

public function lookupAllExtensions(string $mimetype): array
{
return $this->extensionMap instanceof ExtensionLookup
? $this->extensionMap->lookupAllExtensions($mimetype)
: [];
}
}
Loading

0 comments on commit 545d4b4

Please sign in to comment.