Skip to content

Commit

Permalink
Update AudiobookTest and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Sep 30, 2024
1 parent b0455e8 commit 548c82b
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 47 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ You want to add a format? [See FAQ](#faq)

`Audio::class` convert some properties to be more readable.

- `ape` format: [`Id3TagApe`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagApe.php)
- `asf` format: [`Id3TagAsf`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagAsf.php)
- `id3v1` format: [`Id3TagAudioV1`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagAudioV1.php)
- `id3v2` format: [`Id3TagAudioV2`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagAudioV2.php)
- `matroska` format: [`Id3TagMatroska`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagMatroska.php)
- `quicktime` format: [`Id3TagQuicktime`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagQuicktime.php)
- `vorbiscomment` format: [`Id3TagVorbisComment`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagVorbisComment.php)
- `riff` format: [`Id3TagRiff`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagRiff.php)
- `unknown` format: [`Id3TagVorbisComment`](https://github.com/kiwilan/php-audio/blob/main/src/Id3/Tag/Id3TagVorbisComment.php)

| ID3 type | Original | New property |
| :-------------: | :---------------------: | :--------------: |
| `id3v2` | `band` | `album_artist` |
Expand Down
267 changes: 244 additions & 23 deletions src/Id3/Id3Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Id3Writer
/**
* @param array<string, string> $options
* @param array<string, array> $new_tags
* @param string[] $custom_tags
* @param string[] $warnings
* @param string[] $errors
* @param string[] $tag_formats
Expand All @@ -21,15 +22,14 @@ protected function __construct(
protected Audio $audio,
protected getid3_writetags $writer,
protected AudioCore $core,
// protected array $options = ['encoding' => 'UTF-8'],
protected bool $is_manual = false,
protected array $new_tags = [],
// protected array $warnings = [],
// protected array $errors = [],
// protected bool $override_tags = true,
protected array $custom_tags = [],
protected array $warnings = [],
protected array $errors = [],
protected bool $remove_old_tags = false,
// protected bool $fail_on_error = true,
protected bool $fail_on_error = false,
protected array $tag_formats = [],
// protected ?string $path = null,
protected bool $success = false,
) {}

Expand Down Expand Up @@ -63,31 +63,264 @@ public function title(string $title): self
return $this;
}

public function artist(string $artist): self
{
$this->core->artist = $artist;

return $this;
}

public function album(string $album): self
{
$this->core->album = $album;

return $this;
}

public function save(): void
public function albumArtist(string $album_artist): self
{
$this->core->album_artist = $album_artist;

return $this;
}

public function year(string $year): self
{
$this->core->year = $year;

return $this;
}

public function genre(string $genre): self
{
$this->core->genre = $genre;

return $this;
}

public function trackNumber(string|int $track_number): self
{
if (is_int($track_number)) {
$track_number = (string) $track_number;
}

$this->core->track_number = $track_number;

return $this;
}

public function discNumber(string|int $disc_number): self
{
if (is_int($disc_number)) {
$disc_number = (string) $disc_number;
}

$this->core->disc_number = $disc_number;

return $this;
}

public function composer(string $composer): self
{
$this->core->composer = $composer;

return $this;
}

public function comment(string $comment): self
{
$this->core->comment = $comment;

return $this;
}

public function lyrics(string $lyrics): self
{
$this->core->lyrics = $lyrics;

return $this;
}

public function isCompilation(): self
{
$this->core->is_compilation = true;

return $this;
}

public function isNotCompilation(): self
{
$this->core->is_compilation = false;

return $this;
}

public function creationDate(string $creation_date): self
{
$this->core->creation_date = $creation_date;

return $this;
}

public function copyright(string $copyright): self
{
$this->core->copyright = $copyright;

return $this;
}

public function encodingBy(string $encoding_by): self
{
$this->core->encoding_by = $encoding_by;

return $this;
}

public function encoding(string $encoding): self
{
$this->core->encoding = $encoding;

return $this;
}

public function description(string $description): self
{
$this->core->description = $description;

return $this;
}

public function synopsis(string $synopsis): self
{
$this->core->synopsis = $synopsis;

return $this;
}

public function language(string $language): self
{
$this->core->language = $language;

return $this;
}

/**
* Add custom tags without dedicated method.
*
* Example:
*
* ```php
* $writer->tag('TXXX:CustomTag', 'CustomValue');
* ```
*/
public function tag(string $key, string $value): self
{
$this->assignTags();
$this->custom_tags[$key] = $value;

return $this;
}

/**
* Set manually tags, to know which key used for which tag, you have to refer to documentation.
*
* @docs https://github.com/kiwilan/php-audio#convert-properties
*
* For example, album artist for `id3` encoded files, is `band` key.
*
* @param array<string, string> $tags
*/
public function tags(array $tags): self
{
$this->new_tags = $this->convertTags($tags);
$this->is_manual = true;

return $this;
}

/**
* Fail on errors, by default it's `false`.
*/
public function failOnErrors(): self
{
$this->fail_on_error = true;

return $this;
}

public function save(): bool
{
$this->parseTagFormats();
if (! $this->is_manual) {
$this->assignTags();
}

$this->writer->tagformats = $this->tag_formats;
$this->writer->tag_data = $this->new_tags;

$this->success = $this->writer->WriteTags();

$this->errors = $this->writer->errors;
$this->warnings = $this->writer->warnings;

$this->handleErrors();
ray($this);

return $this->success;
}

private function handleErrors(): void
{
$this->errors = $this->writer->errors;
$this->warnings = $this->writer->warnings;

$errors = implode(', ', $this->errors);
$warnings = implode(', ', $this->warnings);
$supported = match ($this->audio->getFormat()) {
AudioFormatEnum::flac => true,
AudioFormatEnum::mp3 => true,
AudioFormatEnum::ogg => true,
default => false
};

if (! empty($this->errors)) {
$msg = 'Save tags failed.';

$errors = strip_tags($errors);
$errors = "Errors: {$errors}.";
if (! empty($this->errors)) {
$msg .= " {$errors}";
}

$warnings = "Warnings: {$warnings}.";
if (! empty($this->warnings)) {
$msg .= " {$warnings}";
}

$isSuccess = $this->success ? 'true' : 'false';
$success = "Success: {$isSuccess}";
$msg .= " {$success}";

error_log($msg);

if ($this->fail_on_error) {
throw new \Exception($msg);
}
}

if (! $supported && $this->fail_on_error) {
throw new \Exception("Format {$this->audio->getFormat()->value} is not supported.");
}

if (! empty($this->warnings)) {
error_log($warnings);
}
}

/**
* Assign tags from core to tag formats.
*/
private function assignTags(): self
{
$this->parseTagFormats();

$convert = match ($this->audio->getType()) {
AudioTypeEnum::id3 => AudioCore::toId3v2($this->core),
AudioTypeEnum::vorbiscomment => AudioCore::toVorbisComment($this->core),
Expand All @@ -106,21 +339,9 @@ private function assignTags(): self
...$this->audio->getRawTags(), // old tags
...$convert->toArray(), // new tags
];
ray($this->new_tags);
$this->new_tags = $this->convertTags($this->new_tags);

// $tags = [];
// if ($convert) {
// $tags = $convert->toArray();
// }

// $tags = $this->convertTags($tags);
// $this->attachCover($tags);

// $this->tags = [
// ...$this->tags,
// ...$tags,
// ];

return $this;
}

Expand Down
1 change: 1 addition & 0 deletions tests/AudiobookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
expect($raw['comment'])->toBe('English');
expect($raw['asin'])->toBe('ASIN');
expect($raw['album_artist'])->toBe('Robin Hobb');
ray($raw);

expect($audiobook->isWritable())->toBeTrue();
expect($audiobook->isValid())->toBeTrue();
Expand Down
31 changes: 31 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Kiwilan\Audio\Audio;

define('MP3_NO_META', __DIR__.'/media/test-no-meta.mp3');
define('AUDIOBOOK', __DIR__.'/media/audiobook.m4b');
define('AUDIOBOOK_MP3', __DIR__.'/media/audiobook.mp3');
Expand Down Expand Up @@ -147,3 +149,32 @@ function clearOutput()
}
}
}

function testMp3Writer(Audio $audio)
{
expect($audio->getTitle())->toBe('Introduction');
expect($audio->getArtist())->toBe('Mr Piouf');
expect($audio->getAlbum())->toBe('P1PDD Le conclave de Troie');
expect($audio->getGenre())->toBe('Roleplaying game');
expect($audio->getYear())->toBe(2016);
expect($audio->getTrackNumber())->toBe('1');
expect($audio->getComment())->toBe('http://www.p1pdd.com');
expect($audio->getAlbumArtist())->toBe('P1PDD & Mr Piouf');
expect($audio->getComposer())->toBe('P1PDD & Piouf');
expect($audio->getDiscNumber())->toBe('1');
expect($audio->isCompilation())->toBeTrue();
}

function testMp3Writed(Audio $audio)
{
expect($audio->getTitle())->toBe('New Title');
expect($audio->getArtist())->toBe('New Artist');
expect($audio->getAlbum())->toBe('New Album');
expect($audio->getGenre())->toBe('New Genre');
expect($audio->getYear())->toBe(2022);
expect($audio->getAlbumArtist())->toBe('New Album Artist');
expect($audio->getComment())->toBe('New Comment');
expect($audio->getComposer())->toBe('New Composer');
expect($audio->getDiscNumber())->toBe('2/2');
expect($audio->isCompilation())->toBeFalse();
}
Loading

0 comments on commit 548c82b

Please sign in to comment.