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

Allow partial tag list to be retrieved #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 28 additions & 11 deletions PhpId3/Id3TagsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,37 @@ public function __construct($fileHandle)

}

public function readAllTags()
/**
* Read only list of tags
*
* @param array $tagsToRetrieve (as ['ID3tagNameAsKey' => 'string describing ID3 tag'])
* @return $this
*/
public function readTags($tagsToRetrieve = [])
{
assert( $this->validMp3 === TRUE);

$bytesPos = 10; //From headers

$this->fileReader->setMap(array(
"frameId" => array(BinaryFileReader::FIXED, 4),
"size" => array(BinaryFileReader::FIXED, 4, BinaryFileReader::INT),
"flag" => array(BinaryFileReader::FIXED, 2),
"body" => array(BinaryFileReader::SIZE_OF, "size"),
));

$id3Tags = Id3Tags::getId3Tags();
"frameId" => array(BinaryFileReader::FIXED, 4),
"size" => array(BinaryFileReader::FIXED, 4, BinaryFileReader::INT),
"flag" => array(BinaryFileReader::FIXED, 2),
"body" => array(BinaryFileReader::SIZE_OF, "size"),
));

$allTags = Id3Tags::getId3Tags();
while (($file_data = $this->fileReader->read())) {

if (!in_array($file_data->frameId, array_keys($id3Tags))) {
if (!in_array($file_data->frameId, array_keys($allTags))) {
break;
} else if (!in_array($file_data->frameId, array_keys($tagsToRetrieve))) {
continue;
}

$body = $file_data->body;

// If frame is a text frame then we have to consider
// If frame is a text frame then we have to consider
// encoding as shown in spec section 4.2
if( $file_data->frameId[0] === "T" )
{
Expand All @@ -70,7 +77,7 @@ public function readAllTags()
}

$this->id3Array[$file_data->frameId] = array(
"fullTagName" => $id3Tags[$file_data->frameId],
"fullTagName" => $allTags[$file_data->frameId],
"position" => $bytesPos,
"size" => $file_data->size,
"body" => $body,
Expand All @@ -81,6 +88,16 @@ public function readAllTags()
return $this;
}

/**
* ReadAllTags is an indirection to readTags using full tags list
*
* @return Id3TagsReader
*/
public function readAllTags()
{
return $this->readTags(Id3Tags::getId3Tags());
}

public function getId3Array()
{
return $this->id3Array;
Expand Down
15 changes: 14 additions & 1 deletion Tests/Id3TagsReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ public function testGetImage()
$this->assertEquals(file_get_contents(__DIR__ . $this->albumCover), $image[1]);
}

public function testGetId3TagsArray()
/**
* Test readTags with partial tag array
*/
public function testGetId3TagsArrayWithPartialArray()
{
$id3 = new Id3TagsReader(fopen(__DIR__ . $this->mp3File, "rb"));
$id3->readTags(array_flip(["TIT2"]));
$id3Tags = $id3->getId3Array();

$this->assertEquals($id3Tags["TIT2"]["body"], 'Piranha');
$this->assertCount(1, $id3Tags);
}

public function testGetId3TagsArrayWithCompleteArray()
{
$id3Tags = $this->id3->getId3Array();

Expand Down