Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
TmxDumpFormatter | |
0.00% |
0 / 43 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
format | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
begin | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
close | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
formatEntry | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | namespace ContentTranslation; |
4 | |
5 | use XMLWriter; |
6 | |
7 | class TmxDumpFormatter { |
8 | /** @var bool */ |
9 | private $isStarted = false; |
10 | |
11 | /** @var XMLWriter */ |
12 | private $xml; |
13 | |
14 | /** @var string */ |
15 | private $sourceLanguage; |
16 | |
17 | /** |
18 | * @param string $sourceLanguage |
19 | */ |
20 | public function __construct( $sourceLanguage ) { |
21 | $this->sourceLanguage = $sourceLanguage; |
22 | |
23 | $this->xml = new XMLWriter(); |
24 | $this->xml->openMemory(); |
25 | $this->xml->setIndent( true ); |
26 | $this->xml->setIndentString( ' ' ); |
27 | $this->xml->startDocument( '1.0', 'UTF-8' ); |
28 | } |
29 | |
30 | /** |
31 | * @param array $entry |
32 | * @return string|int |
33 | */ |
34 | public function format( array $entry ) { |
35 | if ( !$this->isStarted ) { |
36 | $this->isStarted = true; |
37 | $this->begin(); |
38 | } |
39 | |
40 | $this->formatEntry( $entry ); |
41 | |
42 | return $this->xml->flush(); |
43 | } |
44 | |
45 | private function begin() { |
46 | $this->xml->startElement( 'tmx' ); |
47 | $this->xml->writeAttribute( 'version', '1.4' ); |
48 | $this->xml->startElement( 'header' ); |
49 | $this->xml->writeAttribute( 'creationtool', 'dump-corpora.php / XMLWriter' ); |
50 | $this->xml->writeAttribute( 'creationtoolversion', '2.0.0' ); |
51 | $this->xml->writeAttribute( 'segtype', 'block' ); |
52 | $this->xml->writeAttribute( 'o-tmf', 'sql' ); |
53 | $this->xml->writeAttribute( 'adminlang', 'en' ); |
54 | $this->xml->writeAttribute( 'sourcelang', $this->sourceLanguage ); |
55 | $this->xml->writeAttribute( 'datatype', 'plaintext' ); |
56 | $this->xml->endElement(); // header |
57 | $this->xml->startElement( 'body' ); |
58 | } |
59 | |
60 | public function close() { |
61 | $this->xml->endElement(); // body |
62 | // @phan-suppress-next-line PhanPluginDuplicateAdjacentStatement |
63 | $this->xml->endElement(); // tmx |
64 | |
65 | return $this->xml->flush(); |
66 | } |
67 | |
68 | private function formatEntry( array $entry ) { |
69 | foreach ( $entry['corpora'] as $id => $units ) { |
70 | $this->xml->startElement( 'tu' ); |
71 | $this->xml->writeAttribute( 'srclang', $entry['sourceLanguage'] ); |
72 | foreach ( $units as $origin => $unit ) { |
73 | if ( !is_array( $unit ) || $unit['content'] === null ) { |
74 | continue; |
75 | } |
76 | |
77 | $this->xml->startElement( 'tuv' ); |
78 | if ( $origin === 'source' ) { |
79 | $this->xml->writeAttribute( 'xml:lang', $entry['sourceLanguage'] ); |
80 | } else { |
81 | $this->xml->writeAttribute( 'xml:lang', $entry['targetLanguage'] ); |
82 | } |
83 | |
84 | $this->xml->startElement( 'prop' ); |
85 | $this->xml->writeAttribute( 'type', 'origin' ); |
86 | $this->xml->text( $origin ); |
87 | $this->xml->endElement(); // prop |
88 | |
89 | $this->xml->writeElement( 'seg', $unit['content'] ); |
90 | |
91 | $this->xml->endElement(); // tuv |
92 | } |
93 | |
94 | $this->xml->endElement(); // tu |
95 | } |
96 | } |
97 | } |