Skip to content

Commit

Permalink
feature: add EXTVLCOPT tag #27
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Jan 4, 2024
1 parent a46fef9 commit 7ef3b0b
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 6 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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
*/
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
73 changes: 73 additions & 0 deletions src/Tag/ExtVlcOpt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace M3uParser\Tag;

/**
* @see https://github.com/Gemorroj/M3uParser/issues/27
*/
class ExtVlcOpt implements ExtTagInterface
{
private string $key;
private string $value;

/**
* #EXTVLCOPT:http-user-agent=Mozilla/5.0 (Windows NT 6.1; rv:61.0) Gecko/20100101 Firefox/61.0.
*/
public function __construct(string $lineStr = null)
{
if (null !== $lineStr) {
$this->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:<key> = <value>
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);
}
}
4 changes: 3 additions & 1 deletion src/TagsManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use M3uParser\Tag\ExtLogo;
use M3uParser\Tag\ExtTagInterface;
use M3uParser\Tag\ExtTv;
use M3uParser\Tag\ExtVlcOpt;

trait TagsManagerTrait
{
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion tests/Tag/ExtLogoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
61 changes: 61 additions & 0 deletions tests/Tag/ExtVlcOptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace M3uParser\Tests\Tag;

use M3uParser\M3uData;
use M3uParser\M3uEntry;
use M3uParser\M3uParser;
use M3uParser\Tag\ExtTagInterface;
use M3uParser\Tag\ExtVlcOpt;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @coversNothing
*/
class ExtVlcOptTest extends TestCase
{
public function testParseExtVlcOpt(): void
{
$m3uParser = new M3uParser();
$m3uParser->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);
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/extvlcopt.m3u
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#EXTM3U

#EXTVLCOPT:http-user-agent=Lavf53.32.100
rtp://@127.0.0.1:5003

0 comments on commit 7ef3b0b

Please # to comment.