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 0000000..a9cb5ea Binary files /dev/null and b/tests/files/slick.bin differ