Skip to content

Commit

Permalink
feature: add EXTTITLE tag #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Jan 4, 2024
1 parent a7b7ac6 commit e168cee
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ foreach ($data as $entry) {
case $extTag instanceof \M3uParser\Tag\Playlist: // If PLAYLIST tag
echo $extTag->getValue() . "\n";
break;

case $extTag instanceof \M3uParser\Tag\ExtTitle: // If EXTTITLE tag
echo $extTag->getValue() . "\n";
break;
}
}
}
Expand Down Expand Up @@ -161,9 +165,13 @@ $entry->addExtTag(
->setValue('Rock')
);
$entry->addExtTag(
(new ExtPlaylist())
(new Playlist())
->setValue('My favorite playlist')
);
$entry->addExtTag(
(new ExtTitle())
->setValue('title')
);

$data = new M3uData();
$data->setAttribute('test-name', 'test-value');
Expand All @@ -178,6 +186,7 @@ echo $data;
#EXTVLCOPT:http-user-agent=M2uParser
#EXTGRP:Rock
#PLAYLIST:My favorite playlist
#EXTTITLE:title
test-path
*/
```
Expand Down
57 changes: 57 additions & 0 deletions src/Tag/ExtTitle.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/33
*/
class ExtTitle implements ExtTagInterface
{
private string $value;

/**
* #EXTTITLE:super-song.
*/
public function __construct(string $lineStr = null)
{
if (null !== $lineStr) {
$this->make($lineStr);
}
}

public function __toString(): string
{
return '#EXTTITLE:'.$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, '#EXTTITLE:');
}

protected function make(string $lineStr): void
{
/*
EXTTITLE format:
#EXTTITLE:<value>
example:
#EXTTITLE:name
*/
$dataLineStr = \substr($lineStr, \strlen('#EXTTITLE:'));
$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 @@ -6,6 +6,7 @@
use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtLogo;
use M3uParser\Tag\ExtTagInterface;
use M3uParser\Tag\ExtTitle;
use M3uParser\Tag\ExtTv;
use M3uParser\Tag\ExtVlcOpt;
use M3uParser\Tag\Playlist;
Expand Down Expand Up @@ -38,7 +39,7 @@ public function addTag(string $tag): self
}

/**
* Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST).
* Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST, EXTTITLE).
*/
public function addDefaultTags(): self
{
Expand All @@ -48,6 +49,7 @@ public function addDefaultTags(): self
$this->addTag(ExtVlcOpt::class);
$this->addTag(ExtGrp::class);
$this->addTag(Playlist::class);
$this->addTag(ExtTitle::class);

return $this;
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Tag/ExtTitleTest.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\ExtTitle;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @coversNothing
*/
class ExtTitleTest extends TestCase
{
public function testParseExtTitle(): void
{
$m3uParser = new M3uParser();
$m3uParser->addDefaultTags();
$data = $m3uParser->parseFile(__DIR__.'/../fixtures/exttitle.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 ExtTitle $extTitle */
$extTitle = $extTags[0];
self::assertInstanceOf(ExtTitle::class, $extTitle);

self::assertEquals('Rock music', $extTitle->getValue());
}

public function testGenerateExtTitle(): void
{
$expectedString = '#EXTM3U'."\n";
$expectedString .= '#EXTTITLE:Rock music'."\n";
$expectedString .= 'test-path';

$entry = new M3uEntry();
$entry->setPath('test-path');
$entry->addExtTag(
(new ExtTitle())
->setValue('Rock music')
);

$data = new M3uData();
$data->append($entry);

self::assertEquals($expectedString, (string) $data);
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/exttitle.m3u
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#EXTM3U

#EXTTITLE:Rock music
rtp://@127.0.0.1:5003

0 comments on commit e168cee

Please sign in to comment.