From 9e80de6d140f2a8643564ec22ca2684e62d2e59c Mon Sep 17 00:00:00 2001 From: Gemorroj Date: Thu, 4 Jan 2024 14:51:42 +0300 Subject: [PATCH] feature: add EXTGRP tag #28 --- README.md | 10 +++++++ src/Tag/ExtGrp.php | 57 +++++++++++++++++++++++++++++++++++++ src/TagsManagerTrait.php | 4 ++- tests/Tag/ExtGrpTest.php | 59 +++++++++++++++++++++++++++++++++++++++ tests/fixtures/extgrp.m3u | 4 +++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/Tag/ExtGrp.php create mode 100644 tests/Tag/ExtGrpTest.php create mode 100644 tests/fixtures/extgrp.m3u diff --git a/README.md b/README.md index 9069a7e..9d6816f 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,10 @@ 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; } } } @@ -121,6 +125,7 @@ use M3uParser\Tag\ExtInf; use M3uParser\Tag\ExtTv; use M3uParser\Tag\ExtLogo; use M3uParser\Tag\ExtVlcOpt; +use M3uParser\Tag\ExtGrp; $entry = new M3uEntry(); $entry->setPath('test-path'); @@ -146,6 +151,10 @@ $entry->addExtTag( ->setKey('http-user-agent') ->setValue('M2uParser') ); +$entry->addExtTag( + (new ExtGrp()) + ->setValue('Rock') +); $data = new M3uData(); $data->setAttribute('test-name', 'test-value'); @@ -158,6 +167,7 @@ echo $data; #EXTTV:hd,sd;ru;xml-tv-id;https://example.org/icon.png #EXTLOGO:https://example.org/logo.png #EXTVLCOPT:http-user-agent=M2uParser +#EXTGRP:Rock test-path */ ``` diff --git a/src/Tag/ExtGrp.php b/src/Tag/ExtGrp.php new file mode 100644 index 0000000..0bffaa2 --- /dev/null +++ b/src/Tag/ExtGrp.php @@ -0,0 +1,57 @@ +make($lineStr); + } + } + + public function __toString(): string + { + return '#EXTGRP:'.$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, '#EXTGRP:'); + } + + protected function make(string $lineStr): void + { + /* +EXTGRP format: +#EXTGRP: +example: +#EXTGRP:Rock + */ + $dataLineStr = \substr($lineStr, \strlen('#EXTGRP:')); + $dataLineStr = \trim($dataLineStr); + + $this->setValue($dataLineStr); + } +} diff --git a/src/TagsManagerTrait.php b/src/TagsManagerTrait.php index fe005a0..f6a6a15 100644 --- a/src/TagsManagerTrait.php +++ b/src/TagsManagerTrait.php @@ -2,6 +2,7 @@ namespace M3uParser; +use M3uParser\Tag\ExtGrp; use M3uParser\Tag\ExtInf; use M3uParser\Tag\ExtLogo; use M3uParser\Tag\ExtTagInterface; @@ -36,7 +37,7 @@ public function addTag(string $tag): self } /** - * Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT). + * Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP). */ public function addDefaultTags(): self { @@ -44,6 +45,7 @@ public function addDefaultTags(): self $this->addTag(ExtTv::class); $this->addTag(ExtLogo::class); $this->addTag(ExtVlcOpt::class); + $this->addTag(ExtGrp::class); return $this; } diff --git a/tests/Tag/ExtGrpTest.php b/tests/Tag/ExtGrpTest.php new file mode 100644 index 0000000..d58c19b --- /dev/null +++ b/tests/Tag/ExtGrpTest.php @@ -0,0 +1,59 @@ +addDefaultTags(); + $data = $m3uParser->parseFile(__DIR__.'/../fixtures/extgrp.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 ExtGrp $extGrp */ + $extGrp = $extTags[0]; + self::assertInstanceOf(ExtGrp::class, $extGrp); + + self::assertEquals('Rock', $extGrp->getValue()); + } + + public function testGenerateExtGrp(): void + { + $expectedString = '#EXTM3U'."\n"; + $expectedString .= '#EXTGRP:Rock'."\n"; + $expectedString .= 'test-path'; + + $entry = new M3uEntry(); + $entry->setPath('test-path'); + $entry->addExtTag( + (new ExtGrp()) + ->setValue('Rock') + ); + + $data = new M3uData(); + $data->append($entry); + + self::assertEquals($expectedString, (string) $data); + } +} diff --git a/tests/fixtures/extgrp.m3u b/tests/fixtures/extgrp.m3u new file mode 100644 index 0000000..0868fd4 --- /dev/null +++ b/tests/fixtures/extgrp.m3u @@ -0,0 +1,4 @@ +#EXTM3U + +#EXTGRP:Rock +rtp://@127.0.0.1:5003