-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c624979
Showing
13 changed files
with
521 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
class Attributes | ||
{ | ||
public function __construct( | ||
protected array $attributes = [], | ||
) { | ||
|
||
} | ||
|
||
public function all($namespace = null): array | ||
{ | ||
return $this->attributes[$namespace] ?? []; | ||
} | ||
|
||
public function get($key, $namespace = null): mixed | ||
{ | ||
return $this->attributes[$namespace][$key] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
abstract class BodyItem | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
class Document | ||
{ | ||
public function __construct( | ||
/** @var array<File> $files */ | ||
protected array $files = [], | ||
) { | ||
} | ||
|
||
public function addFile(File $file): void | ||
{ | ||
$this->files[] = $file; | ||
} | ||
|
||
public function getFiles(): array | ||
{ | ||
return $this->files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
class File | ||
{ | ||
public function __construct( | ||
/** @var array<BodyItem> $items */ | ||
protected array $items = [], | ||
protected ?Attributes $attributes = new Attributes(), | ||
) { | ||
|
||
} | ||
|
||
public function addBodyItem(BodyItem $item): void | ||
{ | ||
$this->items[] = $item; | ||
} | ||
|
||
public function getBodyItems(): array | ||
{ | ||
return $this->items; | ||
} | ||
|
||
public function getAttributes(): Attributes | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
class Group | ||
{ | ||
public function __construct( | ||
/** @var array<TransUnit> $transUnits */ | ||
protected array $transUnits = [], | ||
protected ?Attributes $attributes = new Attributes(), | ||
) { | ||
} | ||
|
||
public function addTransUnit(TransUnit $transUnit) | ||
{ | ||
$this->transUnits[] = $transUnit; | ||
} | ||
|
||
public function getTransUnits(): array | ||
{ | ||
return $this->transUnits; | ||
} | ||
|
||
public function getAttributes(): Attributes | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
class Source | ||
{ | ||
public function __construct( | ||
protected string $content, | ||
protected ?Attributes $attributes = new Attributes(), | ||
) { | ||
|
||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function getAttributes(): Attributes | ||
{ | ||
return $this->attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
class Target | ||
{ | ||
public function __construct( | ||
protected string $content, | ||
protected ?Attributes $attributes = new Attributes(), | ||
) { | ||
|
||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function getAttributes(): Attributes | ||
{ | ||
return $this->attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Domain\V12; | ||
|
||
class TransUnit | ||
{ | ||
public function __construct( | ||
protected Source $source, | ||
protected ?Target $target = null, | ||
protected ?Attributes $attributes = new Attributes(), | ||
) { | ||
|
||
} | ||
|
||
public function getSource(): Source | ||
{ | ||
return $this->source; | ||
} | ||
|
||
public function getTarget(): ?Target | ||
{ | ||
return $this->target; | ||
} | ||
|
||
public function getAttributes(): Attributes | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace VIVOMEDIA\XliffParser\Reader\V12; | ||
|
||
use VIVOMEDIA\XliffParser\Domain\V12\Attributes; | ||
use VIVOMEDIA\XliffParser\Domain\V12\Document; | ||
use VIVOMEDIA\XliffParser\Domain\V12\File; | ||
use VIVOMEDIA\XliffParser\Domain\V12\Group; | ||
use VIVOMEDIA\XliffParser\Domain\V12\Source; | ||
use VIVOMEDIA\XliffParser\Domain\V12\Target; | ||
use VIVOMEDIA\XliffParser\Domain\V12\TransUnit; | ||
|
||
class XliffReader | ||
{ | ||
public function read($file): Document | ||
{ | ||
$xml = simplexml_load_file($file); | ||
|
||
if ($xml->getName() === 'xliff' && $xml->version = '1.2') { | ||
$document = $this->getDocumentForNode($xml); | ||
} else { | ||
throw new \RuntimeException("Not a valid Xliff file in Version 1.2"); | ||
} | ||
|
||
return $document; | ||
} | ||
|
||
private function xmlAttributesToAttributes(\SimpleXMLElement $bodyItemNode): Attributes | ||
{ | ||
return new Attributes([ | ||
null => ((array)$bodyItemNode->attributes())['@attributes'] ?? [], | ||
'xml' => ((array)$bodyItemNode->attributes('http://www.w3.org/XML/1998/namespace'))['@attributes'] ?? [], | ||
]); | ||
} | ||
|
||
private function getDocumentForNode(\SimpleXMLElement $node): Document | ||
{ | ||
$files = []; | ||
foreach ($node->children() as $fileNode) { | ||
if ($fileNode->getName() === 'file') { | ||
if ($fileNode->body) { | ||
$files[] = $this->getFileForNode($fileNode); | ||
} else { | ||
throw new \RuntimeException("Not a valid Xliff file in Version 1.2"); | ||
} | ||
} | ||
} | ||
|
||
return new Document($files); | ||
} | ||
|
||
private function getFileForNode(\SimpleXMLElement $node): File | ||
{ | ||
$items = []; | ||
foreach ($node->body->children() as $bodyItemNode) { | ||
if ($bodyItemNode->getName() == 'trans-unit') { | ||
$items[] = $this->getTransUnitForNode($bodyItemNode); | ||
} elseif ($bodyItemNode->getName() == 'group') { | ||
$items[] = $this->getGroupForNode($bodyItemNode); | ||
} | ||
} | ||
return new File($items, $this->xmlAttributesToAttributes($node)); | ||
} | ||
|
||
private function getGroupForNode(\SimpleXMLElement $node): Group | ||
{ | ||
$groupItems = []; | ||
foreach ($node->children() as $groupItemNode) { | ||
$groupItems[] = $this->getTransUnitForNode($groupItemNode); | ||
} | ||
return new Group($groupItems, $this->xmlAttributesToAttributes($node)); | ||
} | ||
|
||
private function getTransUnitForNode(\SimpleXMLElement $node): TransUnit | ||
{ | ||
$source = new Source( | ||
(string)$node->source, | ||
$this->xmlAttributesToAttributes($node->source) | ||
); | ||
|
||
$target = null; | ||
if ($node->target) { | ||
$target = new Target( | ||
(string)$node->target, | ||
$this->xmlAttributesToAttributes($node->target) | ||
); | ||
} | ||
|
||
return new TransUnit( | ||
$source, | ||
$target, | ||
$this->xmlAttributesToAttributes($node) | ||
); | ||
} | ||
} |
Oops, something went wrong.