diff --git a/README.md b/README.md index ba9975d..7dc669e 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,14 @@ Basically what your implementation should be able to do, is convert subtitle fil Best example is to look how [SrtConverter.php](https://github.com/mantas783/subtitle-converter/blob/master/src/code/Converters/SrtConverter.php) is implemented. And this is example of [.srt file](https://github.com/mantas783/subtitle-converter/blob/master/tests/files/srt.srt). +## Registering your converter + +```php +Subtitles::registerConverter(FakeDocxConverter::class, 'docx_fake', 'docx', 'Fake docx converter'); +``` + +You can add a new converter or replace the existing one if the format name is the same (the second parameter). + ### "Internal Format" "Internal Format" is just a PHP array. It is used internally in library to be able to convert between different formats. diff --git a/src/Subtitles.php b/src/Subtitles.php index 7cb66a0..9578b87 100644 --- a/src/Subtitles.php +++ b/src/Subtitles.php @@ -181,6 +181,20 @@ public static function getFormat($string) } } + public static function registerConverter($class, $string_format, $extension, $name) + { + // unset class if the name of format is the same + foreach (self::$formats as $k => $format) { + if ($format['format'] === $string_format) { + unset(self::$formats[$k]); + } + } + unset($format); + + // add at the beginning + array_unshift(self::$formats, ['extension' => $extension, 'format' => $string_format, 'name' => $name, 'class' => $class]); + } + public function getInternalFormat() { return $this->internal_format; diff --git a/tests/PublicInterfaceTest.php b/tests/PublicInterfaceTest.php index fa6c288..637c7b1 100644 --- a/tests/PublicInterfaceTest.php +++ b/tests/PublicInterfaceTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use Done\Subtitles\Subtitles; use Helpers\AdditionalAssertionsTrait; +use Tests\Stubs\FakeDocxConverter; class PublicInterfaceTest extends TestCase { @@ -360,4 +361,25 @@ public function testNotASubtitleFormatThrowsException() $srt_path = './tests/files/slick.bin'; Subtitles::getFormat(file_get_contents($srt_path)); } + + public function testRegisterConverter() + { + // replacing the class + $initial_format_count = count(Subtitles::$formats); + Subtitles::registerConverter(FakeDocxConverter::class, 'docx', 'docx', 'Fake docx converter'); + $after_format_count = count(Subtitles::$formats); + Subtitles::registerConverter(FakeDocxConverter::class, 'docx_fake', 'docx', 'Fake docx converter'); + $final_format_count = count(Subtitles::$formats); + $this->assertEquals($initial_format_count, $after_format_count); // replaces with the same name (count the same) + $this->assertEquals($initial_format_count + 1, $final_format_count); // adds new + + // from format + $actual_internal_format = Subtitles::loadFromString('fake_docx')->getInternalFormat(); + $expected_internal_format = [['start' => 22, 'end' => 33, 'lines' => ['fake']]]; + $this->assertInternalFormatsEqual($expected_internal_format, $actual_internal_format); + + // to format + $actual = (new Subtitles)->add(1, 2, 'a')->content('docx'); + $this->assertEquals('fake docx text', $actual); + } } diff --git a/tests/Stubs/FakeDocxConverter.php b/tests/Stubs/FakeDocxConverter.php new file mode 100644 index 0000000..35e278d --- /dev/null +++ b/tests/Stubs/FakeDocxConverter.php @@ -0,0 +1,28 @@ + 22, + 'end' => 33, + 'lines' => ['fake'], + ]]; + } + + public function internalFormatToFileContent(array $internal_format, array $options) + { + return 'fake docx text'; + } +} \ No newline at end of file