From a7b7ac6c9d9bafcb909752b730f563524e3b2a83 Mon Sep 17 00:00:00 2001 From: Gemorroj Date: Thu, 4 Jan 2024 15:08:30 +0300 Subject: [PATCH] feature: add PLAYLIST tag #29 --- README.md | 12 +++++++- src/Tag/ExtGrp.php | 2 +- src/Tag/Playlist.php | 57 +++++++++++++++++++++++++++++++++++ src/TagsManagerTrait.php | 4 ++- tests/Tag/PlaylistTest.php | 59 +++++++++++++++++++++++++++++++++++++ tests/fixtures/playlist.m3u | 4 +++ 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 src/Tag/Playlist.php create mode 100644 tests/Tag/PlaylistTest.php create mode 100644 tests/fixtures/playlist.m3u diff --git a/README.md b/README.md index 9d6816f..221c4f2 100644 --- a/README.md +++ b/README.md @@ -106,10 +106,14 @@ foreach ($data as $entry) { case $extTag instanceof \M3uParser\Tag\ExtVlcOpt: // If EXTVLCOPT tag echo $extTag->getKey() . ':' . $extTag->getValue() . "\n"; break; - + case $extTag instanceof \M3uParser\Tag\ExtGrp: // If EXTGRP tag echo $extTag->getValue() . "\n"; break; + + case $extTag instanceof \M3uParser\Tag\Playlist: // If PLAYLIST tag + echo $extTag->getValue() . "\n"; + break; } } } @@ -126,6 +130,7 @@ use M3uParser\Tag\ExtTv; use M3uParser\Tag\ExtLogo; use M3uParser\Tag\ExtVlcOpt; use M3uParser\Tag\ExtGrp; +use M3uParser\Tag\Playlist; $entry = new M3uEntry(); $entry->setPath('test-path'); @@ -155,6 +160,10 @@ $entry->addExtTag( (new ExtGrp()) ->setValue('Rock') ); +$entry->addExtTag( + (new ExtPlaylist()) + ->setValue('My favorite playlist') +); $data = new M3uData(); $data->setAttribute('test-name', 'test-value'); @@ -168,6 +177,7 @@ echo $data; #EXTLOGO:https://example.org/logo.png #EXTVLCOPT:http-user-agent=M2uParser #EXTGRP:Rock +#PLAYLIST:My favorite playlist test-path */ ``` diff --git a/src/Tag/ExtGrp.php b/src/Tag/ExtGrp.php index 0bffaa2..ab209f1 100644 --- a/src/Tag/ExtGrp.php +++ b/src/Tag/ExtGrp.php @@ -45,7 +45,7 @@ protected function make(string $lineStr): void { /* EXTGRP format: -#EXTGRP: +#EXTGRP: example: #EXTGRP:Rock */ diff --git a/src/Tag/Playlist.php b/src/Tag/Playlist.php new file mode 100644 index 0000000..34577c7 --- /dev/null +++ b/src/Tag/Playlist.php @@ -0,0 +1,57 @@ +make($lineStr); + } + } + + public function __toString(): string + { + return '#PLAYLIST:'.$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, '#PLAYLIST:'); + } + + protected function make(string $lineStr): void + { + /* +PLAYLIST format: +#PLAYLIST: +example: +#PLAYLIST:My favorite music + */ + $dataLineStr = \substr($lineStr, \strlen('#PLAYLIST:')); + $dataLineStr = \trim($dataLineStr); + + $this->setValue($dataLineStr); + } +} diff --git a/src/TagsManagerTrait.php b/src/TagsManagerTrait.php index f6a6a15..c8c722b 100644 --- a/src/TagsManagerTrait.php +++ b/src/TagsManagerTrait.php @@ -8,6 +8,7 @@ use M3uParser\Tag\ExtTagInterface; use M3uParser\Tag\ExtTv; use M3uParser\Tag\ExtVlcOpt; +use M3uParser\Tag\Playlist; trait TagsManagerTrait { @@ -37,7 +38,7 @@ public function addTag(string $tag): self } /** - * Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP). + * Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST). */ public function addDefaultTags(): self { @@ -46,6 +47,7 @@ public function addDefaultTags(): self $this->addTag(ExtLogo::class); $this->addTag(ExtVlcOpt::class); $this->addTag(ExtGrp::class); + $this->addTag(Playlist::class); return $this; } diff --git a/tests/Tag/PlaylistTest.php b/tests/Tag/PlaylistTest.php new file mode 100644 index 0000000..ff493e3 --- /dev/null +++ b/tests/Tag/PlaylistTest.php @@ -0,0 +1,59 @@ +addDefaultTags(); + $data = $m3uParser->parseFile(__DIR__.'/../fixtures/playlist.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 Playlist $playlist */ + $playlist = $extTags[0]; + self::assertInstanceOf(Playlist::class, $playlist); + + self::assertEquals('My favorite playlist', $playlist->getValue()); + } + + public function testGeneratePlaylist(): void + { + $expectedString = '#EXTM3U'."\n"; + $expectedString .= '#PLAYLIST:My favorite playlist'."\n"; + $expectedString .= 'test-path'; + + $entry = new M3uEntry(); + $entry->setPath('test-path'); + $entry->addExtTag( + (new Playlist()) + ->setValue('My favorite playlist') + ); + + $data = new M3uData(); + $data->append($entry); + + self::assertEquals($expectedString, (string) $data); + } +} diff --git a/tests/fixtures/playlist.m3u b/tests/fixtures/playlist.m3u new file mode 100644 index 0000000..778e6d6 --- /dev/null +++ b/tests/fixtures/playlist.m3u @@ -0,0 +1,4 @@ +#EXTM3U + +#PLAYLIST:My favorite playlist +rtp://@127.0.0.1:5003