Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
12.77% |
6 / 47 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
SrtWriter | |
12.77% |
6 / 47 |
|
16.67% |
1 / 6 |
125.19 | |
0.00% |
0 / 1 |
write | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
formatCue | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
formatTimestamp | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
formatText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatNodes | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
formatNode | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace MediaWiki\TimedMediaHandler\TimedText; |
4 | |
5 | class SrtWriter extends Writer { |
6 | /** |
7 | * @inheritDoc |
8 | */ |
9 | public function write( $cues ) { |
10 | return implode( "\n\n", |
11 | array_map( |
12 | [ $this, 'formatCue' ], |
13 | $cues |
14 | ) |
15 | ); |
16 | } |
17 | |
18 | /** |
19 | * @param DOM\Cue $cue cue to output |
20 | * @return string |
21 | */ |
22 | public function formatCue( DOM\Cue $cue ) { |
23 | return (int)$cue->id . |
24 | "\n" . |
25 | $this->formatTimestamp( $cue->start ) . |
26 | " --> " . |
27 | $this->formatTimestamp( $cue->end ) . |
28 | "\n" . |
29 | $this->formatText( $this->formatNodes( $cue->nodes ) ); |
30 | } |
31 | |
32 | /** |
33 | * @param float $time |
34 | * |
35 | * @return string |
36 | */ |
37 | public function formatTimestamp( $time ) { |
38 | $s = floor( $time ); |
39 | $frac = $time - $s; |
40 | $millis = round( $frac * 1000.0 ); |
41 | |
42 | $seconds = $s % 60; |
43 | $s = ( $s - $seconds ) / 60; |
44 | |
45 | $minutes = $s % 60; |
46 | $s = ( $s - $minutes ) / 60; |
47 | |
48 | $hours = $s; |
49 | |
50 | return sprintf( "%02d:%02d:%02d,%03d", |
51 | $hours, |
52 | $minutes, |
53 | $seconds, |
54 | $millis |
55 | ); |
56 | } |
57 | |
58 | /** |
59 | * @param string $text |
60 | * |
61 | * @return string |
62 | */ |
63 | public function formatText( $text ) { |
64 | // Ensure there can be no blank lines in the cue. |
65 | // But cues may span multiple lines. |
66 | return str_replace( "\n\n", "\n \n", $text ); |
67 | } |
68 | |
69 | /** |
70 | * @param array $nodes |
71 | * |
72 | * @return string |
73 | */ |
74 | public function formatNodes( $nodes ) { |
75 | $s = ''; |
76 | foreach ( $nodes as $node ) { |
77 | $s .= $this->formatNode( $node ); |
78 | } |
79 | return $s; |
80 | } |
81 | |
82 | /** |
83 | * @param DOM\Node $node |
84 | * |
85 | * @return string |
86 | */ |
87 | public function formatNode( DOM\Node $node ) { |
88 | if ( $node instanceof DOM\InternalNode ) { |
89 | if ( $node instanceof DOM\BoldNode ) { |
90 | $tag = 'b'; |
91 | } elseif ( $node instanceof DOM\ItalicNode ) { |
92 | $tag = 'i'; |
93 | } elseif ( $node instanceof DOM\UnderlineNode ) { |
94 | $tag = 'u'; |
95 | } else { |
96 | $tag = ''; |
97 | } |
98 | |
99 | $content = $this->formatNodes( $node->nodes ); |
100 | |
101 | if ( $tag ) { |
102 | return "<$tag>$content</$tag>"; |
103 | } |
104 | return $content; |
105 | } |
106 | |
107 | if ( $node instanceof DOM\TextNode ) { |
108 | return htmlspecialchars( $node->text, ENT_NOQUOTES | ENT_HTML5, 'utf-8' ); |
109 | } |
110 | |
111 | return ''; |
112 | } |
113 | } |