From 7ef3b0ba6b064d6ab5324f4a8196b8b2fcf7e347 Mon Sep 17 00:00:00 2001 From: Gemorroj Date: Thu, 4 Jan 2024 14:39:18 +0300 Subject: [PATCH] feature: add EXTVLCOPT tag #27 --- README.md | 17 +++++++-- composer.json | 2 +- src/Tag/ExtVlcOpt.php | 73 ++++++++++++++++++++++++++++++++++++ src/TagsManagerTrait.php | 4 +- tests/Tag/ExtLogoTest.php | 1 - tests/Tag/ExtVlcOptTest.php | 61 ++++++++++++++++++++++++++++++ tests/fixtures/extvlcopt.m3u | 4 ++ 7 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 src/Tag/ExtVlcOpt.php create mode 100644 tests/Tag/ExtVlcOptTest.php create mode 100644 tests/fixtures/extvlcopt.m3u diff --git a/README.md b/README.md index 5143a5d..9069a7e 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,10 @@ foreach ($data as $entry) { case $extTag instanceof \M3uParser\Tag\ExtLogo: // If EXTLOGO tag echo $extTag->getLogo() . "\n"; break; + + case $extTag instanceof \M3uParser\Tag\ExtVlcOpt: // If EXTVLCOPT tag + echo $extTag->getKey() . ':' . $extTag->getValue() . "\n"; + break; } } } @@ -116,6 +120,7 @@ use M3uParser\M3uEntry; use M3uParser\Tag\ExtInf; use M3uParser\Tag\ExtTv; use M3uParser\Tag\ExtLogo; +use M3uParser\Tag\ExtVlcOpt; $entry = new M3uEntry(); $entry->setPath('test-path'); @@ -136,6 +141,11 @@ $entry->addExtTag( (new ExtLogo()) ->setLogo('https://example.org/logo.png') ); +$entry->addExtTag( + (new ExtVlcOpt()) + ->setKey('http-user-agent') + ->setValue('M2uParser') +); $data = new M3uData(); $data->setAttribute('test-name', 'test-value'); @@ -144,9 +154,10 @@ $data->append($entry); echo $data; /* #EXTM3U test-name="test-value" -#EXTINF: 123 test-attr="test-attrname", extinf-title -#EXTTV: hd,sd;ru;xml-tv-id;https://example.org/icon.png -#EXTLOGO: https://example.org/logo.png +#EXTINF:123 test-attr="test-attrname", extinf-title +#EXTTV:hd,sd;ru;xml-tv-id;https://example.org/icon.png +#EXTLOGO:https://example.org/logo.png +#EXTVLCOPT:http-user-agent=M2uParser test-path */ ``` diff --git a/composer.json b/composer.json index 51b37bf..c1c5803 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.6", - "friendsofphp/php-cs-fixer": "^3.45", + "friendsofphp/php-cs-fixer": "^3.46", "phpstan/phpstan": "^1.10" }, "autoload": { diff --git a/src/Tag/ExtVlcOpt.php b/src/Tag/ExtVlcOpt.php new file mode 100644 index 0000000..46c855b --- /dev/null +++ b/src/Tag/ExtVlcOpt.php @@ -0,0 +1,73 @@ +make($lineStr); + } + } + + public function __toString(): string + { + return '#EXTVLCOPT:'.$this->getKey().'='.$this->getValue(); + } + + public function setKey(string $key): self + { + $this->key = $key; + + return $this; + } + + public function getKey(): string + { + return $this->key; + } + + 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, '#EXTVLCOPT:'); + } + + protected function make(string $lineStr): void + { + /* +EXTVLCOPT format: +#EXTVLCOPT: = +example: +#EXTVLCOPT:http-user-agent=Mozilla/5.0 (Windows NT 6.1; rv:61.0) Gecko/20100101 Firefox/61.0 + */ + $dataLineStr = \substr($lineStr, \strlen('#EXTVLCOPT:')); + $dataLineStr = \trim($dataLineStr); + + [$key, $value] = \explode('=', $dataLineStr, 2); + + $this->setKey($key); + $this->setValue($value); + } +} diff --git a/src/TagsManagerTrait.php b/src/TagsManagerTrait.php index bb72901..fe005a0 100644 --- a/src/TagsManagerTrait.php +++ b/src/TagsManagerTrait.php @@ -6,6 +6,7 @@ use M3uParser\Tag\ExtLogo; use M3uParser\Tag\ExtTagInterface; use M3uParser\Tag\ExtTv; +use M3uParser\Tag\ExtVlcOpt; trait TagsManagerTrait { @@ -35,13 +36,14 @@ public function addTag(string $tag): self } /** - * Add default tags (EXTINF, EXTTV, EXTLOGO). + * Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT). */ public function addDefaultTags(): self { $this->addTag(ExtInf::class); $this->addTag(ExtTv::class); $this->addTag(ExtLogo::class); + $this->addTag(ExtVlcOpt::class); return $this; } diff --git a/tests/Tag/ExtLogoTest.php b/tests/Tag/ExtLogoTest.php index 4dd842b..bf227f7 100644 --- a/tests/Tag/ExtLogoTest.php +++ b/tests/Tag/ExtLogoTest.php @@ -46,7 +46,6 @@ public function testGenerateExtLogo(): void $entry = new M3uEntry(); $entry->setPath('test-path'); - $entry->setPath('test-path'); $entry->addExtTag( (new ExtLogo()) ->setLogo('http://example.org/logo.png') diff --git a/tests/Tag/ExtVlcOptTest.php b/tests/Tag/ExtVlcOptTest.php new file mode 100644 index 0000000..5ff827a --- /dev/null +++ b/tests/Tag/ExtVlcOptTest.php @@ -0,0 +1,61 @@ +addDefaultTags(); + $data = $m3uParser->parseFile(__DIR__.'/../fixtures/extvlcopt.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 ExtVlcOpt $extVlcOpt */ + $extVlcOpt = $extTags[0]; + self::assertInstanceOf(ExtVlcOpt::class, $extVlcOpt); + + self::assertEquals('http-user-agent', $extVlcOpt->getKey()); + self::assertEquals('Lavf53.32.100', $extVlcOpt->getValue()); + } + + public function testGenerateExtVlcOpt(): void + { + $expectedString = '#EXTM3U'."\n"; + $expectedString .= '#EXTVLCOPT:some-key=some-value'."\n"; + $expectedString .= 'test-path'; + + $entry = new M3uEntry(); + $entry->setPath('test-path'); + $entry->addExtTag( + (new ExtVlcOpt()) + ->setKey('some-key') + ->setValue('some-value') + ); + + $data = new M3uData(); + $data->append($entry); + + self::assertEquals($expectedString, (string) $data); + } +} diff --git a/tests/fixtures/extvlcopt.m3u b/tests/fixtures/extvlcopt.m3u new file mode 100644 index 0000000..7e401b9 --- /dev/null +++ b/tests/fixtures/extvlcopt.m3u @@ -0,0 +1,4 @@ +#EXTM3U + +#EXTVLCOPT:http-user-agent=Lavf53.32.100 +rtp://@127.0.0.1:5003