Skip to content

Commit

Permalink
Xliff 1.2 reader and writer
Browse files Browse the repository at this point in the history
  • Loading branch information
dlubitz committed Jan 26, 2024
0 parents commit c624979
Show file tree
Hide file tree
Showing 13 changed files with 521 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Classes/Domain/V12/Attributes.php
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;
}
}
8 changes: 8 additions & 0 deletions Classes/Domain/V12/BodyItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace VIVOMEDIA\XliffParser\Domain\V12;

abstract class BodyItem
{

}
22 changes: 22 additions & 0 deletions Classes/Domain/V12/Document.php
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;
}
}
30 changes: 30 additions & 0 deletions Classes/Domain/V12/File.php
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;
}

}
29 changes: 29 additions & 0 deletions Classes/Domain/V12/Group.php
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;
}

}
23 changes: 23 additions & 0 deletions Classes/Domain/V12/Source.php
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;
}
}
23 changes: 23 additions & 0 deletions Classes/Domain/V12/Target.php
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;
}
}
31 changes: 31 additions & 0 deletions Classes/Domain/V12/TransUnit.php
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;
}


}
95 changes: 95 additions & 0 deletions Classes/Reader/V12/XliffReader.php
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)
);
}
}
Loading

0 comments on commit c624979

Please sign in to comment.