Skip to content

Commit

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

case $extTag instanceof \M3uParser\Tag\ExtAlbumArtUrl: // If EXTALBUMARTURL tag
echo $extTag->getValue() . "\n";
break;
}
}
}
Expand All @@ -135,6 +139,8 @@ use M3uParser\Tag\ExtLogo;
use M3uParser\Tag\ExtVlcOpt;
use M3uParser\Tag\ExtGrp;
use M3uParser\Tag\Playlist;
use M3uParser\Tag\ExtTitle;
use M3uParser\Tag\ExtAlbumArtUrl;

$entry = new M3uEntry();
$entry->setPath('test-path');
Expand Down Expand Up @@ -172,6 +178,10 @@ $entry->addExtTag(
(new ExtTitle())
->setValue('title')
);
$entry->addExtTag(
(new ExtAlbumArtUrl())
->setValue('https://store.example.com/download/A32X5yz-1.jpg')
);

$data = new M3uData();
$data->setAttribute('test-name', 'test-value');
Expand All @@ -187,6 +197,7 @@ echo $data;
#EXTGRP:Rock
#PLAYLIST:My favorite playlist
#EXTTITLE:title
#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg
test-path
*/
```
Expand Down
57 changes: 57 additions & 0 deletions src/Tag/ExtAlbumArtUrl.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/34
*/
class ExtAlbumArtUrl implements ExtTagInterface
{
private string $value;

/**
* #EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg.
*/
public function __construct(string $lineStr = null)
{
if (null !== $lineStr) {
$this->make($lineStr);
}
}

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

protected function make(string $lineStr): void
{
/*
EXTALBUMARTURL format:
#EXTALBUMARTURL:<url>
example:
#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg
*/
$dataLineStr = \substr($lineStr, \strlen('#EXTALBUMARTURL:'));
$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\ExtAlbumArtUrl;
use M3uParser\Tag\ExtGrp;
use M3uParser\Tag\ExtInf;
use M3uParser\Tag\ExtLogo;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function addTag(string $tag): self
}

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

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

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

self::assertEquals('https://store.example.com/download/A32X5yz-1.jpg', $extAlbumArtUrl->getValue());
}

public function testGenerateExtAlbumArtUrl(): void
{
$expectedString = '#EXTM3U'."\n";
$expectedString .= '#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg'."\n";
$expectedString .= 'test-path';

$entry = new M3uEntry();
$entry->setPath('test-path');
$entry->addExtTag(
(new ExtAlbumArtUrl())
->setValue('https://store.example.com/download/A32X5yz-1.jpg')
);

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

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

#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg
rtp://@127.0.0.1:5003

0 comments on commit a846c4a

Please sign in to comment.