app\models\File::getInfo PHP Method

getInfo() public method

Get all applicable ID3 info from the file.
public getInfo ( ) : array | void
return array | void
    public function getInfo()
    {
        $info = $this->getID3->analyze($this->path);
        if (isset($info['error']) || !isset($info['playtime_seconds'])) {
            $this->syncError = isset($info['error']) ? $info['error'][0] : 'No playtime found';
            return;
        }
        // Copy the available tags over to comment.
        // This is a helper from getID3, though it doesn't really work well.
        // We'll still prefer getting ID3v2 tags directly later.
        // Read on.
        getid3_lib::CopyTagsToComments($info);
        $track = 0;
        // Apparently track number can be stored with different indices as the following.
        $trackIndices = ['comments.track', 'comments.tracknumber', 'comments.track_number'];
        for ($i = 0; $i < count($trackIndices) && $track === 0; ++$i) {
            $track = array_get($info, $trackIndices[$i], [0])[0];
        }
        $props = ['artist' => '', 'album' => '', 'compilation' => false, 'title' => '', 'length' => $info['playtime_seconds'], 'track' => (int) $track, 'lyrics' => '', 'cover' => array_get($info, 'comments.picture', [null])[0], 'path' => $this->path, 'mtime' => $this->mtime];
        if (!($comments = array_get($info, 'comments_html'))) {
            return $props;
        }
        // We prefer id3v2 tags over others.
        if (!($artist = array_get($info, 'tags.id3v2.artist', [null])[0])) {
            $artist = array_get($comments, 'artist', [''])[0];
        }
        if (!($albumArtist = array_get($info, 'tags.id3v2.band', [null])[0])) {
            $albumArtist = array_get($comments, 'band', [''])[0];
        }
        if (!($album = array_get($info, 'tags.id3v2.album', [null])[0])) {
            $album = array_get($comments, 'album', [''])[0];
        }
        if (!($title = array_get($info, 'tags.id3v2.title', [null])[0])) {
            $title = array_get($comments, 'title', [''])[0];
        }
        if (!($lyrics = array_get($info, 'tags.id3v2.unsynchronised_lyric', [null])[0])) {
            $lyrics = array_get($comments, 'unsynchronised_lyric', [''])[0];
        }
        // Fixes #323, where tag names can be htmlentities()'ed
        $props['title'] = html_entity_decode(trim($title));
        $props['album'] = html_entity_decode(trim($album));
        $props['artist'] = html_entity_decode(trim($artist));
        $props['albumartist'] = html_entity_decode(trim($albumArtist));
        $props['lyrics'] = html_entity_decode(trim($lyrics));
        // A "compilation" property can is determined by:
        // - "part_of_a_compilation" tag (used by iTunes), or
        // - "albumartist" (used by non-retarded applications).
        $props['compilation'] = (bool) (array_get($comments, 'part_of_a_compilation', [false])[0] || $props['albumartist']);
        return $this->info = $props;
    }

Usage Example

Exemplo n.º 1
0
 public function testHtmlEntitiesInTags()
 {
     $getID3 = m::mock(getID3::class, ['analyze' => ['tags' => ['id3v2' => ['title' => ['&#27700;&#35895;&#24195;&#23455;'], 'album' => ['&#23567;&#23721;&#20117;&#12371; Random'], 'artist' => ['&#20304;&#20489;&#32190;&#38899; Unknown']]], 'encoding' => 'UTF-8', 'playtime_seconds' => 100]]);
     $file = new File(dirname(__FILE__) . '/songs/blank.mp3', $getID3);
     $info = $file->getInfo();
     $this->assertEquals('佐倉綾音 Unknown', $info['artist']);
     $this->assertEquals('小岩井こ Random', $info['album']);
     $this->assertEquals('水谷広実', $info['title']);
 }