Skip to content

Commit db9f825

Browse files
Merge pull request #23 from kiwilan/develop
v3.0.05
2 parents 8de55cb + eeffe50 commit db9f825

File tree

8 files changed

+93
-7
lines changed

8 files changed

+93
-7
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ $audio->getStik(); // `?string` (audiobook)
6767
$audio->getDuration(); // `?float` to get duration in seconds
6868
```
6969

70+
Raw tags:
71+
72+
```php
73+
use Kiwilan\Audio\Audio;
74+
75+
$audio = Audio::get('path/to/audio.mp3');
76+
77+
$audio->getTags(); // `array` with all tags
78+
$title = $audio->getTag('title'); // `?string` to get title same as `$audio->getTitle()`
79+
```
80+
7081
Additional metadata:
7182

7283
```php
@@ -82,6 +93,20 @@ $audio->getType(); // `?AudioTypeEnum` ID3 type (id3, riff, asf, quicktime, matr
8293
$audio->getExtras(); // `array` with raw metadata (could contains some metadata not parsed)
8394
```
8495

96+
Raw audio:
97+
98+
> [!NOTE]
99+
>
100+
> Cover is removed from `toArray()` method, you can use `getCover()` method to get cover metadata.
101+
102+
```php
103+
use Kiwilan\Audio\Audio;
104+
105+
$audio = Audio::get('path/to/audio.mp3');
106+
107+
$audio->toArray(); // `array` with all metadata
108+
```
109+
85110
Advanced properties:
86111

87112
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "kiwilan/php-audio",
33
"description": "PHP package to parse and update audio files metadata, with `JamesHeinrich/getID3`.",
4-
"version": "3.0.04",
4+
"version": "3.0.05",
55
"keywords": [
66
"audio",
77
"php",

src/Audio.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,31 @@ private function coreToProperties(?AudioCore $core): self
463463

464464
return $this;
465465
}
466+
467+
public function getTag(string $tag): ?string
468+
{
469+
$tags = $this->reader->toTagsArray();
470+
471+
return $tags[$tag] ?? null;
472+
}
473+
474+
/**
475+
* Get all tags as array.
476+
*
477+
* @return array<string, string>
478+
*/
479+
public function getTags(): array
480+
{
481+
return $this->reader->toTagsArray();
482+
}
483+
484+
/**
485+
* Get all raw metadata as array.
486+
*
487+
* @return array<string, mixed>
488+
*/
489+
public function toArray(): array
490+
{
491+
return $this->reader->toArray();
492+
}
466493
}

src/Models/Id3Reader.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ public function getRaw(): array
178178
{
179179
return $this->raw;
180180
}
181+
182+
public function toTagsArray(): array
183+
{
184+
$tags = $this->raw['tags'] ?? [];
185+
$first = reset($tags);
186+
187+
$items = [];
188+
foreach ($first as $key => $value) {
189+
$items[$key] = $value[0] ?? null;
190+
}
191+
192+
return $items;
193+
}
194+
195+
public function toArray(): array
196+
{
197+
$raw = $this->raw;
198+
if (array_key_exists('comments', $raw) && array_key_exists('picture', $raw['comments'])) {
199+
$raw['comments']['picture'] = 'cover string (removed for array)';
200+
}
201+
202+
return $raw;
203+
}
181204
}
182205

183206
class Id3Audio

src/Models/Id3Writer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public function stik(?string $stik): self
216216
}
217217

218218
/**
219-
* @param string $pathOrData Path to cover image or binary data
219+
* @param string $pathOrData Path to cover image or binary data
220220
*/
221221
public function cover(string $pathOrData): self
222222
{
@@ -288,7 +288,7 @@ public function tags(array $tags): self
288288
/**
289289
* Set tag format.
290290
*
291-
* @param string[] $tags Options are `id3v1`, `id3v2.2`, `id2v2.3`, `id3v2.4`, `ape`, `vorbiscomment`, `metaflac`, `real`
291+
* @param string[] $tags Options are `id3v1`, `id3v2.2`, `id2v2.3`, `id3v2.4`, `ape`, `vorbiscomment`, `metaflac`, `real`
292292
*/
293293
public function tagFormats(array $tags): self
294294
{

tests/AudioTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
expect($audio->getFormat())->toBe($format);
2929
expect($audio->getDuration())->toBeFloat();
3030
expect($audio->getExtras())->toBeArray();
31+
expect($audio->getTags())->toBeArray();
32+
expect($audio->toArray())->toBeArray();
3133

3234
$metadata = $audio->getAudio();
3335
expect($metadata->getPath())->toBeString();

tests/AudiobookTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,27 @@
1414
expect($audio->getTrackNumber())->toBe('1/1');
1515
expect($audio->getComment())->toBe('P1PDD team');
1616
expect($audio->getAlbumArtist())->toBe('Mr Piouf and P1PDD');
17-
expect($audio->getComposer())->toBeNull();
18-
expect($audio->getDiscNumber())->toBeNull();
17+
expect($audio->getComposer())->toBe('Composer');
18+
expect($audio->getDiscNumber())->toBe('1');
1919
expect($audio->isCompilation())->toBe(false);
2020
expect($audio->getPath())->toBe(AUDIOBOOK);
2121
expect($audio->getFormat())->toBe(AudioFormatEnum::m4b);
2222
expect($audio->getCreationDate())->toBe('2023-06-04T12:00:00Z');
2323
expect($audio->getEncodingBy())->toBe('Mr Piouf');
2424
expect($audio->getEncoding())->toBe('Audiobook Builder 2.2.6 (www.splasm.com), macOS 13.4');
2525
expect($audio->getCopyright())->toBeString();
26-
expect($audio->getDescription())->toBe('Première campagne de P1PDD');
27-
expect($audio->getLyrics())->toBe('P1PDD');
26+
expect($audio->getDescription())->toBe('Description');
27+
expect($audio->getLyrics())->toBe('Lyrics');
2828
expect($audio->getStik())->toBe('Audiobook');
2929
expect($audio->getDuration())->toBe(11.00);
3030
expect($audio->getExtras())->toBeArray();
31+
expect($audio->toArray())->toBeArray();
32+
33+
expect($audio->getTags())->toBeArray();
34+
expect($audio->getTag('title'))->toBe('P1PDD Saison 1');
35+
expect($audio->getTag('artist'))->toBe('Mr Piouf');
36+
expect($audio->getTag('album'))->toBe('P1PDD Saison 1');
37+
expect($audio->getTag('genre'))->toBe('Audiobooks');
38+
expect($audio->getTag('track_number'))->toBe('1/1');
39+
expect($audio->getTag('comment'))->toBe('P1PDD team');
3140
});

tests/media/audiobook.m4b

4.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)