Skip to content

Commit

Permalink
Added FindAll-Methods, Added Singleton-Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ruff committed Sep 22, 2023
1 parent 99a664f commit 65dc9da
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
63 changes: 59 additions & 4 deletions src/MimeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,34 @@
*/
class MimeDb
{
/**
* Instance
*
* @var horstoeko\mimedb\MimeDb
*/
protected static $singleton = null;

/**
* The internal mime database
*
* @var array
*/
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
*
Expand Down Expand Up @@ -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();

Expand All @@ -92,7 +130,7 @@ function ($mimetypeDefinition) use ($lookuoFileExtension) {
return null;
}

return array_keys($foundDbEntries)[0];
return array_keys($foundDbEntries);
}

/**
Expand All @@ -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();

Expand All @@ -129,7 +184,7 @@ function ($mimetypeDefinition, $mimetype) use ($lookupMimeType) {
return null;
}

return current($foundDbEntries)["extensions"][0];
return current($foundDbEntries)["extensions"];
}

/**
Expand Down
74 changes: 74 additions & 0 deletions tests/testcases/MimeDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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'));
}
}

0 comments on commit 65dc9da

Please sign in to comment.