Skip to content

Commit

Permalink
Add registerConverter()
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Nov 15, 2024
1 parent 80de151 commit f8ffe8f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/Subtitles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions tests/PublicInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use Done\Subtitles\Subtitles;
use Helpers\AdditionalAssertionsTrait;
use Tests\Stubs\FakeDocxConverter;

class PublicInterfaceTest extends TestCase
{
Expand Down Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions tests/Stubs/FakeDocxConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Stubs;

use Done\Subtitles\Code\Converters\ConverterContract;
use Done\Subtitles\Code\Helpers;

class FakeDocxConverter implements ConverterContract
{
public function canParseFileContent($file_content, $original_file_content)
{
return Helpers::strContains($file_content, 'fake_docx');
}

public function fileContentToInternalFormat($file_content, $original_file_content)
{
return [[
'start' => 22,
'end' => 33,
'lines' => ['fake'],
]];
}

public function internalFormatToFileContent(array $internal_format, array $options)
{
return 'fake docx text';
}
}

0 comments on commit f8ffe8f

Please sign in to comment.