Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
JsonDumpFormatter | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
format | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
close | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFooter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatEntry | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
indent | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace ContentTranslation; |
4 | |
5 | class JsonDumpFormatter { |
6 | private $isStarted = false; |
7 | |
8 | public function format( array $entry ) { |
9 | $output = ''; |
10 | |
11 | if ( !$this->isStarted ) { |
12 | $this->isStarted = true; |
13 | $output .= $this->getHeader(); |
14 | $output .= $this->formatEntry( $entry ); |
15 | } else { |
16 | $output .= ",\n"; |
17 | $output .= $this->formatEntry( $entry ); |
18 | } |
19 | |
20 | return $output; |
21 | } |
22 | |
23 | public function close() { |
24 | if ( !$this->isStarted ) { |
25 | return '[]'; |
26 | } |
27 | |
28 | $output = "\n"; |
29 | $output .= $this->getFooter(); |
30 | |
31 | return $output; |
32 | } |
33 | |
34 | private function getHeader() { |
35 | return "[\n"; |
36 | } |
37 | |
38 | private function getFooter() { |
39 | return "]\n"; |
40 | } |
41 | |
42 | /** |
43 | * @param array $entry |
44 | * @return string |
45 | */ |
46 | private function formatEntry( array $entry ) { |
47 | $output = ''; |
48 | $indent = ' '; |
49 | |
50 | foreach ( $entry['corpora'] as $id => $unit ) { |
51 | unset( $unit['source']['timestamp'], $unit['user']['timestamp'], $unit['mt']['timestamp'] ); |
52 | |
53 | $globalId = "{$entry['id']}/$id"; |
54 | $section = [ |
55 | 'id' => $globalId, |
56 | 'sourceLanguage' => $entry['sourceLanguage'], |
57 | 'targetLanguage' => $entry['targetLanguage'], |
58 | 'source' => $unit['source'], |
59 | 'mt' => $unit['mt'], |
60 | 'target' => $unit['user'], |
61 | ]; |
62 | |
63 | $json = \FormatJson::encode( $section, $indent, \FormatJson::ALL_OK ); |
64 | $output .= self::indent( $indent, $json ) . ",\n"; |
65 | } |
66 | |
67 | $output = rtrim( $output, "\n," ); |
68 | return $output; |
69 | } |
70 | |
71 | private static function indent( $indent, $text ) { |
72 | // Assuming literal newlines do not occur within strings |
73 | $text = $indent . str_replace( "\n", "\n$indent", $text ); |
74 | return $text; |
75 | } |
76 | } |