diff --git a/README.md b/README.md index 124f44d..ffb61ef 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,10 @@ foreach ($data as $entry) { case $extTag instanceof \M3uParser\Tag\ExtTitle: // If EXTTITLE tag echo $extTag->getValue() . "\n"; break; + + case $extTag instanceof \M3uParser\Tag\ExtAlbumArtUrl: // If EXTALBUMARTURL tag + echo $extTag->getValue() . "\n"; + break; } } } @@ -135,6 +139,8 @@ use M3uParser\Tag\ExtLogo; use M3uParser\Tag\ExtVlcOpt; use M3uParser\Tag\ExtGrp; use M3uParser\Tag\Playlist; +use M3uParser\Tag\ExtTitle; +use M3uParser\Tag\ExtAlbumArtUrl; $entry = new M3uEntry(); $entry->setPath('test-path'); @@ -172,6 +178,10 @@ $entry->addExtTag( (new ExtTitle()) ->setValue('title') ); +$entry->addExtTag( + (new ExtAlbumArtUrl()) + ->setValue('https://store.example.com/download/A32X5yz-1.jpg') +); $data = new M3uData(); $data->setAttribute('test-name', 'test-value'); @@ -187,6 +197,7 @@ echo $data; #EXTGRP:Rock #PLAYLIST:My favorite playlist #EXTTITLE:title +#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg test-path */ ``` diff --git a/src/Tag/ExtAlbumArtUrl.php b/src/Tag/ExtAlbumArtUrl.php new file mode 100644 index 0000000..b910e83 --- /dev/null +++ b/src/Tag/ExtAlbumArtUrl.php @@ -0,0 +1,57 @@ +make($lineStr); + } + } + + public function __toString(): string + { + return '#EXTALBUMARTURL:'.$this->getValue(); + } + + public function setValue(string $value): self + { + $this->value = $value; + + return $this; + } + + public function getValue(): string + { + return $this->value; + } + + public static function isMatch(string $lineStr): bool + { + return 0 === \stripos($lineStr, '#EXTALBUMARTURL:'); + } + + protected function make(string $lineStr): void + { + /* +EXTALBUMARTURL format: +#EXTALBUMARTURL: +example: +#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg + */ + $dataLineStr = \substr($lineStr, \strlen('#EXTALBUMARTURL:')); + $dataLineStr = \trim($dataLineStr); + + $this->setValue($dataLineStr); + } +} diff --git a/src/TagsManagerTrait.php b/src/TagsManagerTrait.php index c385d6e..8a89811 100644 --- a/src/TagsManagerTrait.php +++ b/src/TagsManagerTrait.php @@ -2,6 +2,7 @@ namespace M3uParser; +use M3uParser\Tag\ExtAlbumArtUrl; use M3uParser\Tag\ExtGrp; use M3uParser\Tag\ExtInf; use M3uParser\Tag\ExtLogo; @@ -39,7 +40,7 @@ public function addTag(string $tag): self } /** - * Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST, EXTTITLE). + * Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST, EXTTITLE, EXTALBUMARTURL). */ public function addDefaultTags(): self { @@ -50,6 +51,7 @@ public function addDefaultTags(): self $this->addTag(ExtGrp::class); $this->addTag(Playlist::class); $this->addTag(ExtTitle::class); + $this->addTag(ExtAlbumArtUrl::class); return $this; } diff --git a/tests/Tag/ExtAlbumArtUrlTest.php b/tests/Tag/ExtAlbumArtUrlTest.php new file mode 100644 index 0000000..27ad745 --- /dev/null +++ b/tests/Tag/ExtAlbumArtUrlTest.php @@ -0,0 +1,59 @@ +addDefaultTags(); + $data = $m3uParser->parseFile(__DIR__.'/../fixtures/extalbumarturl.m3u'); + + /** @var M3uEntry $entry */ + $entry = $data[0]; + + self::assertEquals('rtp://@127.0.0.1:5003', $entry->getPath()); + + /** @var ExtTagInterface[] $extTags */ + $extTags = $entry->getExtTags(); + self::assertCount(1, $extTags); + + /** @var ExtAlbumArtUrl $extAlbumArtUrl */ + $extAlbumArtUrl = $extTags[0]; + self::assertInstanceOf(ExtAlbumArtUrl::class, $extAlbumArtUrl); + + self::assertEquals('https://store.example.com/download/A32X5yz-1.jpg', $extAlbumArtUrl->getValue()); + } + + public function testGenerateExtAlbumArtUrl(): void + { + $expectedString = '#EXTM3U'."\n"; + $expectedString .= '#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg'."\n"; + $expectedString .= 'test-path'; + + $entry = new M3uEntry(); + $entry->setPath('test-path'); + $entry->addExtTag( + (new ExtAlbumArtUrl()) + ->setValue('https://store.example.com/download/A32X5yz-1.jpg') + ); + + $data = new M3uData(); + $data->append($entry); + + self::assertEquals($expectedString, (string) $data); + } +} diff --git a/tests/fixtures/extalbumarturl.m3u b/tests/fixtures/extalbumarturl.m3u new file mode 100644 index 0000000..abe62ad --- /dev/null +++ b/tests/fixtures/extalbumarturl.m3u @@ -0,0 +1,4 @@ +#EXTM3U + +#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg +rtp://@127.0.0.1:5003