From 2a3f690006040212b794dfc2fa7e264d9d5d470e Mon Sep 17 00:00:00 2001 From: Mantas Date: Mon, 22 Apr 2024 14:09:37 +0300 Subject: [PATCH] Add getFormat() method --- src/Subtitles.php | 15 +++++++++++++++ tests/PublicInterfaceTest.php | 16 ++++++++++++++++ tests/files/slick.bin | Bin 0 -> 8576 bytes 3 files changed, 31 insertions(+) create mode 100644 tests/files/slick.bin diff --git a/src/Subtitles.php b/src/Subtitles.php index 07676b8..ccf8aaf 100644 --- a/src/Subtitles.php +++ b/src/Subtitles.php @@ -166,6 +166,21 @@ public function content($format, $options = []) return $content; } + public static function getFormat($string) + { + $modified_string = Helpers::convertToUtf8($string); + $modified_string = Helpers::removeUtf8Bom($modified_string); + $modified_string = Helpers::normalizeNewLines($modified_string); + + $input_converter = Helpers::getConverterByFileContent($modified_string, $string); + + foreach (self::$formats as $format) { + if ($format['class'] === get_class($input_converter)) { + return $format; + } + } + } + // for testing only public function getInternalFormat() { diff --git a/tests/PublicInterfaceTest.php b/tests/PublicInterfaceTest.php index eb89faa..fa6c288 100644 --- a/tests/PublicInterfaceTest.php +++ b/tests/PublicInterfaceTest.php @@ -4,6 +4,7 @@ use Done\Subtitles\Code\Converters\VttConverter; use Done\Subtitles\Code\Helpers; +use Done\Subtitles\Code\UserException; use PHPUnit\Framework\TestCase; use Done\Subtitles\Subtitles; use Helpers\AdditionalAssertionsTrait; @@ -344,4 +345,19 @@ public function testTrim() $this->assertInternalFormatsEqual($expected_internal_format, $actual_internal_format); } + + public function testGetsFormat() + { + $srt_path = './tests/files/srt.srt'; + $format = Subtitles::getFormat(file_get_contents($srt_path)); + $this->assertEquals('srt', $format['extension']); + } + + public function testNotASubtitleFormatThrowsException() + { + $this->expectException(UserException::class); + + $srt_path = './tests/files/slick.bin'; + Subtitles::getFormat(file_get_contents($srt_path)); + } } diff --git a/tests/files/slick.bin b/tests/files/slick.bin new file mode 100644 index 0000000000000000000000000000000000000000..a9cb5eadf7b7d98323c3744c13c3c276984faa34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