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