Skip to content

Commit

Permalink
feature: add EXTART tag #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Feb 15, 2025
1 parent 4f6738d commit a67426a
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ foreach ($data as $entry) {
case $extTag instanceof \M3uParser\Tag\ExtGenre: // If EXTGENRE tag
echo $extTag->getValue() . "\n";
break;

case $extTag instanceof \M3uParser\Tag\ExtArt: // If EXTART tag
echo $extTag->getValue() . "\n";
break;
}
}
}
Expand All @@ -150,6 +154,7 @@ use M3uParser\Tag\Playlist;
use M3uParser\Tag\ExtTitle;
use M3uParser\Tag\ExtAlbumArtUrl;
use M3uParser\Tag\ExtGenre;
use M3uParser\Tag\ExtArt;

$entry = new M3uEntry();
$entry->setPath('test-path');
Expand Down Expand Up @@ -195,6 +200,10 @@ $entry->addExtTag(
(new ExtGenre())
->setValue('Rock')
);
$entry->addExtTag(
(new ExtArt())
->setValue('some artist')
);

$data = new M3uData();
$data->setAttribute('test-name', 'test-value');
Expand All @@ -212,6 +221,7 @@ echo $data;
#EXTTITLE:title
#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg
#EXTGENRE:Rock
#EXTART:some artist
test-path
*/
```
Expand Down
59 changes: 59 additions & 0 deletions src/Tag/ExtArt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace M3uParser\Tag;

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

/**
* #EXTART:Гражданская оборона.
*/
public function __construct(?string $lineStr = null)
{
if (null !== $lineStr) {
$this->make($lineStr);
}
}

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

protected function make(string $lineStr): void
{
/*
EXTART format:
#EXTART:<value>
example:
#EXTART:Гражданская оборона
*/
$dataLineStr = \substr($lineStr, \strlen('#EXTART:'));
$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 @@ -5,6 +5,7 @@
namespace M3uParser;

use M3uParser\Tag\ExtAlbumArtUrl;
use M3uParser\Tag\ExtArt;
use M3uParser\Tag\ExtGenre;
use M3uParser\Tag\ExtGrp;
use M3uParser\Tag\ExtInf;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function addTag(string $tag): self
}

/**
* Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST, EXTTITLE, EXTALBUMARTURL, EXTGENRE).
* Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST, EXTTITLE, EXTALBUMARTURL, EXTGENRE, EXTART).
*/
public function addDefaultTags(): self
{
Expand All @@ -56,6 +57,7 @@ public function addDefaultTags(): self
$this->addTag(ExtTitle::class);
$this->addTag(ExtAlbumArtUrl::class);
$this->addTag(ExtGenre::class);
$this->addTag(ExtArt::class);

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

declare(strict_types=1);

namespace M3uParser\Tests\Tag;

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

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

self::assertEquals('some artist', $extArt->getValue());
}

public function testGenerateExtArt(): void
{
$expectedString = '#EXTM3U'."\n";
$expectedString .= '#EXTART:some artist'."\n";
$expectedString .= 'test-path';

$entry = new M3uEntry();
$entry->setPath('test-path');
$entry->addExtTag(
(new ExtArt())
->setValue('some artist')
);

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

self::assertEquals($expectedString, (string) $data);
}
}
2 changes: 1 addition & 1 deletion tests/Tag/ExtGenreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testParseExtGenre(): void
self::assertEquals('Rock', $extGenre->getValue());
}

public function testGenerateExtGrp(): void
public function testGenerateExtGenre(): void
{
$expectedString = '#EXTM3U'."\n";
$expectedString .= '#EXTGENRE:Rock'."\n";
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/extart.m3u
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#EXTM3U

#EXTART:some artist
rtp://@127.0.0.1:5003

0 comments on commit a67426a

Please sign in to comment.