Skip to content

Commit

Permalink
Add getFormat() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Apr 22, 2024
1 parent fe405f5 commit 2a3f690
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Subtitles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/PublicInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Binary file added tests/files/slick.bin
Binary file not shown.

0 comments on commit 2a3f690

Please sign in to comment.