Skip to content

Commit 94fc428

Browse files
committedFeb 15, 2025·
feature: add EXTALB tag #30
1 parent a67426a commit 94fc428

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-1
lines changed
 

‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ foreach ($data as $entry) {
132132
case $extTag instanceof \M3uParser\Tag\ExtArt: // If EXTART tag
133133
echo $extTag->getValue() . "\n";
134134
break;
135+
136+
case $extTag instanceof \M3uParser\Tag\ExtAlb: // If EXTALB tag
137+
echo $extTag->getValue() . "\n";
138+
break;
135139
}
136140
}
137141
}
@@ -155,6 +159,7 @@ use M3uParser\Tag\ExtTitle;
155159
use M3uParser\Tag\ExtAlbumArtUrl;
156160
use M3uParser\Tag\ExtGenre;
157161
use M3uParser\Tag\ExtArt;
162+
use M3uParser\Tag\ExtAlb;
158163

159164
$entry = new M3uEntry();
160165
$entry->setPath('test-path');
@@ -204,6 +209,10 @@ $entry->addExtTag(
204209
(new ExtArt())
205210
->setValue('some artist')
206211
);
212+
$entry->addExtTag(
213+
(new ExtAlb())
214+
->setValue('some album')
215+
);
207216

208217
$data = new M3uData();
209218
$data->setAttribute('test-name', 'test-value');
@@ -222,6 +231,7 @@ echo $data;
222231
#EXTALBUMARTURL:https://store.example.com/download/A32X5yz-1.jpg
223232
#EXTGENRE:Rock
224233
#EXTART:some artist
234+
#EXTALB:some album
225235
test-path
226236
*/
227237
```

‎src/Tag/ExtAlb.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace M3uParser\Tag;
6+
7+
/**
8+
* @see https://github.com/Gemorroj/M3uParser/issues/30
9+
*/
10+
class ExtAlb implements ExtTagInterface
11+
{
12+
private string $value;
13+
14+
/**
15+
* #ExtAlb:some album.
16+
*/
17+
public function __construct(?string $lineStr = null)
18+
{
19+
if (null !== $lineStr) {
20+
$this->make($lineStr);
21+
}
22+
}
23+
24+
public function __toString(): string
25+
{
26+
return '#EXTALB:'.$this->getValue();
27+
}
28+
29+
public function setValue(string $value): self
30+
{
31+
$this->value = $value;
32+
33+
return $this;
34+
}
35+
36+
public function getValue(): string
37+
{
38+
return $this->value;
39+
}
40+
41+
public static function isMatch(string $lineStr): bool
42+
{
43+
return 0 === \stripos($lineStr, '#EXTALB:');
44+
}
45+
46+
protected function make(string $lineStr): void
47+
{
48+
/*
49+
EXTALB format:
50+
#EXTALB:<value>
51+
example:
52+
#EXTALB:soma album
53+
*/
54+
$dataLineStr = \substr($lineStr, \strlen('#EXTALB:'));
55+
$dataLineStr = \trim($dataLineStr);
56+
57+
$this->setValue($dataLineStr);
58+
}
59+
}

‎src/TagsManagerTrait.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace M3uParser;
66

7+
use M3uParser\Tag\ExtAlb;
78
use M3uParser\Tag\ExtAlbumArtUrl;
89
use M3uParser\Tag\ExtArt;
910
use M3uParser\Tag\ExtGenre;
@@ -44,7 +45,7 @@ public function addTag(string $tag): self
4445
}
4546

4647
/**
47-
* Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST, EXTTITLE, EXTALBUMARTURL, EXTGENRE, EXTART).
48+
* Add default tags (EXTINF, EXTTV, EXTLOGO, EXTVLCOPT, EXTGRP, PLAYLIST, EXTTITLE, EXTALBUMARTURL, EXTGENRE, EXTART, EXTALB).
4849
*/
4950
public function addDefaultTags(): self
5051
{
@@ -58,6 +59,7 @@ public function addDefaultTags(): self
5859
$this->addTag(ExtAlbumArtUrl::class);
5960
$this->addTag(ExtGenre::class);
6061
$this->addTag(ExtArt::class);
62+
$this->addTag(ExtAlb::class);
6163

6264
return $this;
6365
}

‎tests/Tag/ExtAlbTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace M3uParser\Tests\Tag;
6+
7+
use M3uParser\M3uData;
8+
use M3uParser\M3uEntry;
9+
use M3uParser\M3uParser;
10+
use M3uParser\Tag\ExtAlb;
11+
use M3uParser\Tag\ExtTagInterface;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* @internal
16+
*
17+
* @coversNothing
18+
*/
19+
class ExtAlbTest extends TestCase
20+
{
21+
public function testParseExtAlb(): void
22+
{
23+
$m3uParser = new M3uParser();
24+
$m3uParser->addDefaultTags();
25+
$data = $m3uParser->parseFile(__DIR__.'/../fixtures/extalb.m3u');
26+
27+
/** @var M3uEntry $entry */
28+
$entry = $data[0];
29+
30+
self::assertEquals('rtp://@127.0.0.1:5003', $entry->getPath());
31+
32+
/** @var ExtTagInterface[] $extTags */
33+
$extTags = $entry->getExtTags();
34+
self::assertCount(1, $extTags);
35+
36+
/** @var ExtAlb $extAlb */
37+
$extAlb = $extTags[0];
38+
self::assertInstanceOf(ExtAlb::class, $extAlb);
39+
40+
self::assertEquals('some album', $extAlb->getValue());
41+
}
42+
43+
public function testGenerateExtAlb(): void
44+
{
45+
$expectedString = '#EXTM3U'."\n";
46+
$expectedString .= '#EXTALB:some album'."\n";
47+
$expectedString .= 'test-path';
48+
49+
$entry = new M3uEntry();
50+
$entry->setPath('test-path');
51+
$entry->addExtTag(
52+
(new ExtAlb())
53+
->setValue('some album')
54+
);
55+
56+
$data = new M3uData();
57+
$data->append($entry);
58+
59+
self::assertEquals($expectedString, (string) $data);
60+
}
61+
}

‎tests/fixtures/extalb.m3u

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#EXTM3U
2+
3+
#EXTALB:some album
4+
rtp://@127.0.0.1:5003

0 commit comments

Comments
 (0)
Please sign in to comment.