From 1f313ce80fdf7da175844ac12c15f2a058b21cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Sun, 28 Jul 2024 15:44:55 +0200 Subject: [PATCH] v3.0.08 Add `getDurationHumanReadable()` method to get the duration in human readable format: `HH:MM:SS`. --- composer.json | 2 +- src/Audio.php | 11 ++++++-- src/Models/AudioCore.php | 12 ++++----- src/Models/AudioCover.php | 2 +- src/Models/AudioMetadata.php | 3 +-- src/Models/AudioStat.php | 3 +-- src/Models/Id3Reader.php | 50 ++++++++++++------------------------ src/Models/Id3Writer.php | 7 +++-- tests/AudioTest.php | 1 + tests/AudiobookTest.php | 1 + tests/Mp3Test.php | 1 + 11 files changed, 41 insertions(+), 52 deletions(-) diff --git a/composer.json b/composer.json index 20ace07..5033e57 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "kiwilan/php-audio", "description": "PHP package to parse and update audio files metadata, with `JamesHeinrich/getID3`.", - "version": "3.0.07", + "version": "3.0.08", "keywords": [ "audio", "php", diff --git a/src/Audio.php b/src/Audio.php index 2c85bd2..4bf8e65 100755 --- a/src/Audio.php +++ b/src/Audio.php @@ -74,8 +74,7 @@ protected function __construct( protected AudioStat $stat, protected Id3Reader $reader, protected ?Id3Writer $writer = null, - ) { - } + ) {} public static function get(string $path): self { @@ -304,6 +303,14 @@ public function getDuration(): ?float return $this->duration; } + /** + * Get `duration` in human readable format: `00:00:00`. + */ + public function getDurationHumanReadable(): ?string + { + return gmdate('H:i:s', intval($this->duration)); + } + /** * Know if audio file is valid. */ diff --git a/src/Models/AudioCore.php b/src/Models/AudioCore.php index 3b833a5..720746a 100644 --- a/src/Models/AudioCore.php +++ b/src/Models/AudioCore.php @@ -27,8 +27,7 @@ public function __construct( protected ?string $stik = null, protected bool $hasCover = false, protected ?AudioCoreCover $cover = null, - ) { - } + ) {} public function getTitle(): ?string { @@ -456,11 +455,11 @@ public static function toAsf(AudioCore $core): Id3TagAsf public static function fromId3(?Id3AudioTagV1 $v1, ?Id3AudioTagV2 $v2): AudioCore { if (! $v1) { - $v1 = new Id3AudioTagV1(); + $v1 = new Id3AudioTagV1; } if (! $v2) { - $v2 = new Id3AudioTagV2(); + $v2 = new Id3AudioTagV2; } return new AudioCore( @@ -646,12 +645,11 @@ public function __construct( protected ?string $picturetypeid = null, protected ?string $description = null, protected ?string $mime = null, - ) { - } + ) {} public static function make(string $pathOrData): self { - $self = new self(); + $self = new self; if (file_exists($pathOrData)) { $image = getimagesize($pathOrData); diff --git a/src/Models/AudioCover.php b/src/Models/AudioCover.php index 28f7fd6..201c119 100644 --- a/src/Models/AudioCover.php +++ b/src/Models/AudioCover.php @@ -18,7 +18,7 @@ public static function make(?Id3Comments $comments): ?self return null; } - $self = new self(); + $self = new self; $self->contents = $comments->picture()->data(); $self->mimeType = $comments->picture()->image_mime(); diff --git a/src/Models/AudioMetadata.php b/src/Models/AudioMetadata.php index 22bd79c..0e04738 100644 --- a/src/Models/AudioMetadata.php +++ b/src/Models/AudioMetadata.php @@ -22,8 +22,7 @@ protected function __construct( protected ?string $channelMode = null, protected bool $lossless = false, protected ?float $compressionRatio = null, - ) { - } + ) {} public static function make(Audio $audio): self { diff --git a/src/Models/AudioStat.php b/src/Models/AudioStat.php index 031bb2f..3e70105 100644 --- a/src/Models/AudioStat.php +++ b/src/Models/AudioStat.php @@ -21,8 +21,7 @@ protected function __construct( protected ?DateTime $modifiedAt = null, protected ?int $blockSize = null, protected ?int $numberOfBlocks = null, - ) { - } + ) {} public static function make(string $path): self { diff --git a/src/Models/Id3Reader.php b/src/Models/Id3Reader.php index 26695a6..ed23da6 100644 --- a/src/Models/Id3Reader.php +++ b/src/Models/Id3Reader.php @@ -30,12 +30,11 @@ protected function __construct( protected ?Id3TagsHtml $tags_html = null, protected ?float $bitrate = null, protected ?string $playtime_string = null, - ) { - } + ) {} public static function make(string $path): self { - $self = new self(new getID3()); + $self = new self(new getID3); $self->raw = $self->instance->analyze($path); $self->is_writable = $self->instance->is_writable($path); @@ -260,8 +259,7 @@ protected function __construct( protected bool $lossless = false, protected ?string $encoder_options = null, protected ?float $compression_ratio = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -369,8 +367,7 @@ protected function __construct( protected ?float $resolution_x = null, protected ?float $resolution_y = null, protected ?float $frame_rate = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -429,8 +426,7 @@ protected function __construct( protected bool $lossless = false, protected ?string $encoder_options = null, protected ?float $compression_ratio = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -522,8 +518,7 @@ protected function __construct( protected ?Id3TagRiff $riff = null, protected ?Id3TagMatroska $matroska = null, protected ?Id3TagApe $ape = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -605,8 +600,7 @@ public function __construct( protected ?string $genre = null, protected ?string $comment = null, protected ?string $track_number = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -694,8 +688,7 @@ public function __construct( protected ?string $text = null, protected ?string $unsynchronised_lyric = null, protected ?string $language = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -826,8 +819,7 @@ class Id3Comments protected function __construct( protected ?string $language = null, protected ?Id3CommentsPicture $picture = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -862,8 +854,7 @@ protected function __construct( protected ?string $picturetype = null, protected ?string $description = null, protected ?int $datalength = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -942,8 +933,7 @@ public function __construct( protected ?string $lyrics = null, protected ?string $comment = null, protected ?string $stik = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -1110,8 +1100,7 @@ public function __construct( protected ?string $track_number = null, protected ?string $year = null, protected ?string $encodingsettings = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -1217,8 +1206,7 @@ public function __construct( protected ?string $compilation = null, protected ?string $date = null, protected ?string $tracknumber = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -1339,8 +1327,7 @@ public function __construct( protected ?string $title = null, protected ?string $product = null, protected ?string $software = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -1427,8 +1414,7 @@ public function __construct( protected ?string $date = null, protected ?string $encoder = null, protected ?string $duration = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -1575,8 +1561,7 @@ public function __construct( protected ?string $podcastdesc = null, protected ?string $language = null, protected ?string $year = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { @@ -1734,8 +1719,7 @@ protected function __construct( protected ?Id3TagRiff $riff = null, protected ?Id3TagMatroska $matroska = null, protected ?Id3TagApe $ape = null, - ) { - } + ) {} public static function make(?array $metadata): ?self { diff --git a/src/Models/Id3Writer.php b/src/Models/Id3Writer.php index 5e56a82..d403cdb 100644 --- a/src/Models/Id3Writer.php +++ b/src/Models/Id3Writer.php @@ -51,15 +51,14 @@ protected function __construct( protected Audio $audio, protected getid3_writetags $instance, protected AudioCore $core, - ) { - } + ) {} public static function make(Audio $audio): self { $self = new self( audio: $audio, - instance: new getid3_writetags(), - core: new AudioCore() + instance: new getid3_writetags, + core: new AudioCore ); return $self; diff --git a/tests/AudioTest.php b/tests/AudioTest.php index 02c4fbb..2bd29d2 100644 --- a/tests/AudioTest.php +++ b/tests/AudioTest.php @@ -153,6 +153,7 @@ expect($audio->getgetExtension())->toBe($extension); expect($audio->getFormat())->toBe($format); expect($audio->getDuration())->toBeFloat(); + expect($audio->getDurationHumanReadable())->toBe('00:00:11'); expect($audio->getExtras())->toBeArray(); expect($audio)->toBeInstanceOf(Audio::class); diff --git a/tests/AudiobookTest.php b/tests/AudiobookTest.php index 2c39214..3bcc230 100644 --- a/tests/AudiobookTest.php +++ b/tests/AudiobookTest.php @@ -29,6 +29,7 @@ expect($audio->getLyrics())->toBe('Lyrics'); expect($audio->getStik())->toBe('Audiobook'); expect($audio->getDuration())->toBe(11.00); + expect($audio->getDurationHumanReadable())->toBe('00:00:11'); expect($audio->getExtras())->toBeArray(); expect($audio->toArray())->toBeArray(); diff --git a/tests/Mp3Test.php b/tests/Mp3Test.php index 924bd1b..7598f3c 100644 --- a/tests/Mp3Test.php +++ b/tests/Mp3Test.php @@ -22,6 +22,7 @@ expect($audio->getPath())->toBe(MP3); expect($audio->getFormat())->toBe(AudioFormatEnum::mp3); expect($audio->getDuration())->toBe(11.05); + expect($audio->getDurationHumanReadable())->toBe('00:00:11'); expect($audio->getExtras())->toBeArray(); $audio = $audio->getAudio();