From 7fac608fb9219cf0f9c72aeb6f63fda6e0b933e1 Mon Sep 17 00:00:00 2001 From: Mantas Date: Sun, 27 Aug 2023 13:32:04 +0300 Subject: [PATCH] Fix vtt exception --- src/Code/Converters/VttConverter.php | 3 +++ tests/formats/VttTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Code/Converters/VttConverter.php b/src/Code/Converters/VttConverter.php index 7356b5b..22d0016 100644 --- a/src/Code/Converters/VttConverter.php +++ b/src/Code/Converters/VttConverter.php @@ -96,6 +96,9 @@ protected static function vttTimeToInternal($vtt_time) // parts[0] could be mm:ss or hh:mm:ss format -> always use hh:mm:ss $parts[0] = substr_count($parts[0], ':') == 2 ? $parts[0] : '00:'.$parts[0]; + if (!isset($parts[1])) { + throw new UserException("Time doesn't have milliseconds: " . $vtt_time); + } $only_seconds = strtotime("1970-01-01 {$parts[0]} UTC"); $milliseconds = (float)('0.' . $parts[1]); diff --git a/tests/formats/VttTest.php b/tests/formats/VttTest.php index b917b0a..2fd3d34 100644 --- a/tests/formats/VttTest.php +++ b/tests/formats/VttTest.php @@ -90,6 +90,21 @@ public function testParsesFileWithCue() $this->assertInternalFormatsEqual($expected, $actual); } + public function testExceptionIfWrongTimestamp() + { + $this->expectException(UserException::class); + + $input_vtt_file_content = <<< TEXT +WEBVTT + +1 +00:00:00.00 --> 00:00:01.o0 +a +TEXT; + + Subtitles::loadFromString($input_vtt_file_content)->getInternalFormat(); + } + public function testNoExceptionWhenEmptyFile() { $this->expectException(UserException::class);