diff --git a/src/MimeDb.php b/src/MimeDb.php index dfaabf3..1bb5606 100644 --- a/src/MimeDb.php +++ b/src/MimeDb.php @@ -20,6 +20,13 @@ */ class MimeDb { + /** + * Instance + * + * @var horstoeko\mimedb\MimeDb + */ + protected static $singleton = null; + /** * The internal mime database * @@ -27,6 +34,20 @@ class MimeDb */ protected $mimeDatabase = []; + /** + * Create instance (Singleton pattern) + * + * @return MimeDb + */ + public static function singleton(): MimeDb + { + if (is_null(static::$singleton)) { + static::$singleton = new static(); + } + + return static::$singleton; + } + /** * Load mime db * @@ -72,12 +93,29 @@ function ($mimetypeDefinition) { } /** - * Find by file extension + * Find first mimetype by file extension * * @param string $lookuoFileExtension * @return string|null */ public function findType(string $lookuoFileExtension): ?string + { + $mimeTypes = $this->findTypeAll($lookuoFileExtension); + + if (is_null($mimeTypes)) { + return null; + } + + return $mimeTypes[0]; + } + + /** + * Find all mimetypes by file extension + * + * @param string $lookuoFileExtension + * @return array|null + */ + public function findTypeAll(string $lookuoFileExtension): ?array { $this->initializeDatabase(); @@ -92,7 +130,7 @@ function ($mimetypeDefinition) use ($lookuoFileExtension) { return null; } - return array_keys($foundDbEntries)[0]; + return array_keys($foundDbEntries); } /** @@ -108,12 +146,29 @@ public function findByExtension(string $lookuoFileExtension): ?string } /** - * Find by mime type + * Gwt first file extension by mimetype * * @param string $lookupMimeType * @return string|null */ public function findMimeType(string $lookupMimeType): ?string + { + $fileExtensions = $this->findMimeTypeAll($lookupMimeType); + + if (is_null($fileExtensions)) { + return null; + } + + return $fileExtensions[0]; + } + + /** + * Gwt all file extensions by mimetype + * + * @param string $lookupMimeType + * @return array|null + */ + public function findMimeTypeAll(string $lookupMimeType): ?array { $this->initializeDatabase(); @@ -129,7 +184,7 @@ function ($mimetypeDefinition, $mimetype) use ($lookupMimeType) { return null; } - return current($foundDbEntries)["extensions"][0]; + return current($foundDbEntries)["extensions"]; } /** diff --git a/tests/testcases/MimeDbTest.php b/tests/testcases/MimeDbTest.php index adb1441..f94b6b6 100644 --- a/tests/testcases/MimeDbTest.php +++ b/tests/testcases/MimeDbTest.php @@ -32,10 +32,34 @@ public function testConstruction(): void $this->assertEmpty($this->getPrivatePropertyFromObject($this->mimeDb, 'mimeDatabase')->getValue($this->mimeDb)); } + /** + * Test "singleton" method + * + * @covers \horstoeko\mimeDb\MimeDb::singleton + * @return void + */ + public function testSingleton(): void + { + $instance = MimeDb::singleton(); + + $this->assertNotNull($instance); + $this->assertInstanceOf(MimeDb::class, $instance); + $this->assertEmpty($this->getPrivatePropertyFromObject($instance, 'mimeDatabase')->getValue($instance)); + + $instance->findType('.mp4'); + + $this->assertNotEmpty($this->getPrivatePropertyFromObject($instance, 'mimeDatabase')->getValue($instance)); + + $instance2 = MimeDb::singleton(); + + $this->assertNotEmpty($this->getPrivatePropertyFromObject($instance2, 'mimeDatabase')->getValue($instance2)); + } + /** * Test "findType" method * * @covers \horstoeko\mimeDb\MimeDb::findType + * @covers \horstoeko\mimeDb\MimeDb::findTypeAll * @covers \horstoeko\mimeDb\MimeDb::initializeDatabase * @covers \horstoeko\mimeDb\MimeDb::loadedDatabase * @covers \horstoeko\mimeDb\MimeDb::loadDatabase @@ -52,9 +76,29 @@ public function testFindType(): void $this->assertNull($this->mimeDb->findType('.unknown')); } + /** + * Test "findTypeAll" method + * + * @covers \horstoeko\mimeDb\MimeDb::findTypeAll + * @covers \horstoeko\mimeDb\MimeDb::initializeDatabase + * @covers \horstoeko\mimeDb\MimeDb::loadedDatabase + * @covers \horstoeko\mimeDb\MimeDb::loadDatabase + * @return void + */ + public function testFindTypeAll(): void + { + $this->assertIsArray($this->mimeDb->findTypeAll('.docx')); + $this->assertArrayHasKey(0, $this->mimeDb->findTypeAll('.docx')); + $this->assertArrayNotHasKey(1, $this->mimeDb->findTypeAll('.docx')); + + $this->assertNull($this->mimeDb->findTypeAll('.unknown')); + } + /** * Test "findByExtension" method * + * @covers \horstoeko\mimeDb\MimeDb::findType + * @covers \horstoeko\mimeDb\MimeDb::findTypeAll * @covers \horstoeko\mimeDb\MimeDb::findByExtension * @covers \horstoeko\mimeDb\MimeDb::initializeDatabase * @covers \horstoeko\mimeDb\MimeDb::loadedDatabase @@ -76,6 +120,7 @@ public function testFindByExtension(): void * Test "findMimeType" method * * @covers \horstoeko\mimeDb\MimeDb::findMimeType + * @covers \horstoeko\mimeDb\MimeDb::findMimeTypeAll * @covers \horstoeko\mimeDb\MimeDb::initializeDatabase * @covers \horstoeko\mimeDb\MimeDb::loadedDatabase * @covers \horstoeko\mimeDb\MimeDb::loadDatabase @@ -88,4 +133,33 @@ public function testFindMimeType(): void $this->assertEquals("mid", $this->mimeDb->findMimeType('audio/midi')); $this->assertNull($this->mimeDb->findMimeType('unknown/unknown')); } + + /** + * Test "findMimeTypeAll" method + * + * @covers \horstoeko\mimeDb\MimeDb::findMimeTypeAll + * @covers \horstoeko\mimeDb\MimeDb::initializeDatabase + * @covers \horstoeko\mimeDb\MimeDb::loadedDatabase + * @covers \horstoeko\mimeDb\MimeDb::loadDatabase + * @return void + */ + public function testFindMimeTypeAll(): void + { + $this->assertIsArray($this->mimeDb->findMimeTypeAll('application/vnd.openxmlformats-officedocument.wordprocessingml.document')); + $this->assertArrayHasKey(0, $this->mimeDb->findMimeTypeAll('application/vnd.openxmlformats-officedocument.wordprocessingml.document')); + $this->assertArrayNotHasKey(1, $this->mimeDb->findMimeTypeAll('application/vnd.openxmlformats-officedocument.wordprocessingml.document')); + $this->assertEquals("docx", $this->mimeDb->findMimeTypeAll('application/vnd.openxmlformats-officedocument.wordprocessingml.document')[0]); + + $this->assertIsArray($this->mimeDb->findMimeTypeAll('video/x-matroska')); + $this->assertArrayHasKey(0, $this->mimeDb->findMimeTypeAll('video/x-matroska')); + $this->assertArrayHasKey(1, $this->mimeDb->findMimeTypeAll('video/x-matroska')); + $this->assertArrayHasKey(2, $this->mimeDb->findMimeTypeAll('video/x-matroska')); + $this->assertArrayNotHasKey(3, $this->mimeDb->findMimeTypeAll('video/x-matroska')); + $this->assertEquals("mkv", $this->mimeDb->findMimeTypeAll('video/x-matroska')[0]); + $this->assertEquals("mk3d", $this->mimeDb->findMimeTypeAll('video/x-matroska')[1]); + $this->assertEquals("mks", $this->mimeDb->findMimeTypeAll('video/x-matroska')[2]); + + $this->assertNull($this->mimeDb->findMimeTypeAll('application/pdx')); + $this->assertNull($this->mimeDb->findMimeTypeAll('unknown/unknown')); + } }