Skip to content

Commit

Permalink
feature: add PLAYLIST tag #29
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Jan 4, 2024
1 parent 9e80de6 commit a7b7ac6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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
*/
```
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/ExtGrp.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function make(string $lineStr): void
{
/*
EXTGRP format:
#EXTGRP:<key>
#EXTGRP:<value>
example:
#EXTGRP:Rock
*/
Expand Down
57 changes: 57 additions & 0 deletions src/Tag/Playlist.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/29
*/
class Playlist implements ExtTagInterface
{
private string $value;

/**
* #PLAYLIST:My favorite music.
*/
public function __construct(string $lineStr = null)
{
if (null !== $lineStr) {
$this->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:<value>
example:
#PLAYLIST:My favorite music
*/
$dataLineStr = \substr($lineStr, \strlen('#PLAYLIST:'));
$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 @@ -8,6 +8,7 @@
use M3uParser\Tag\ExtTagInterface;
use M3uParser\Tag\ExtTv;
use M3uParser\Tag\ExtVlcOpt;
use M3uParser\Tag\Playlist;

trait TagsManagerTrait
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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;
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Tag/PlaylistTest.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\ExtTagInterface;
use M3uParser\Tag\Playlist;
use PHPUnit\Framework\TestCase;

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

#PLAYLIST:My favorite playlist
rtp://@127.0.0.1:5003

0 comments on commit a7b7ac6

Please sign in to comment.