Skip to content

Commit

Permalink
feature: add EXTGRP tag #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Jan 4, 2024
1 parent 7ef3b0b commit 9e80de6
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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
*/
```
Expand Down
57 changes: 57 additions & 0 deletions src/Tag/ExtGrp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace M3uParser\Tag;

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

/**
* #EXTGRP:music.
*/
public function __construct(string $lineStr = null)
{
if (null !== $lineStr) {
$this->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:<key>
example:
#EXTGRP:Rock
*/
$dataLineStr = \substr($lineStr, \strlen('#EXTGRP:'));
$dataLineStr = \trim($dataLineStr);

$this->setValue($dataLineStr);
}
}
4 changes: 3 additions & 1 deletion src/TagsManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace M3uParser;

use M3uParser\Tag\ExtGrp;
use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtLogo;
use M3uParser\Tag\ExtTagInterface;
Expand Down Expand Up @@ -36,14 +37,15 @@ 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
{
$this->addTag(ExtInf::class);
$this->addTag(ExtTv::class);
$this->addTag(ExtLogo::class);
$this->addTag(ExtVlcOpt::class);
$this->addTag(ExtGrp::class);

return $this;
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Tag/ExtGrpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace M3uParser\Tests\Tag;

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

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

#EXTGRP:Rock
rtp://@127.0.0.1:5003

0 comments on commit 9e80de6

Please sign in to comment.