Skip to content

Commit

Permalink
Show error to user when XML is invalid and the reason why it is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Aug 4, 2023
1 parent d05a340 commit ddb1028
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Done\Subtitles\Code\Converters;

use Done\Subtitles\Code\UserException;

class TtmlConverter implements ConverterContract
{
public function canParseFileContent($file_content)
Expand All @@ -11,8 +13,14 @@ public function canParseFileContent($file_content)

public function fileContentToInternalFormat($file_content)
{
libxml_use_internal_errors(true);
$dom = new \DOMDocument();
@$dom->loadXML($file_content);
$dom->loadXML($file_content);

$errors = libxml_get_errors();
if (!empty($errors)) {
throw new UserException('Invalid XML: ' . trim($errors[0]->message));
}

$fps = self::framesPerSecond($dom);

Expand Down
5 changes: 5 additions & 0 deletions tests/formats/TtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Done\Subtitles\Code\Converters\TtmlConverter;
use Done\Subtitles\Code\Helpers;
use Done\Subtitles\Code\UserException;
use Done\Subtitles\Subtitles;
use PHPUnit\Framework\TestCase;
use Helpers\AdditionalAssertionsTrait;
Expand Down Expand Up @@ -65,6 +66,8 @@ public function testParses2()

public function testDuplicatedElementIdsParse()
{
$this->expectException(UserException::class);

$ttml_path = './tests/files/ttml_with_duplicated_element_ids.ttml';
$actual = Subtitles::loadFromFile($ttml_path)->getInternalFormat();
$expected = (new Subtitles())
Expand All @@ -77,6 +80,8 @@ public function testDuplicatedElementIdsParse()

public function testTimeParseWithFpsAndMultiplierGiven()
{
$this->expectException(UserException::class);

$ttml_path = './tests/files/ttml_with_fps_and_multiplier_given.ttml';
$actual = Subtitles::loadFromFile($ttml_path)->getInternalFormat();
$expected = (new Subtitles())
Expand Down

0 comments on commit ddb1028

Please sign in to comment.