41
41
* @author Chris Boulton <[email protected] >
42
42
* @copyright (c) 2009 Chris Boulton
43
43
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
44
- * @version 1.8
44
+ * @version 1.9
45
45
* @link https://github.com/JBlond/php-diff
46
46
*/
47
47
@@ -61,18 +61,18 @@ class HtmlArray extends RendererAbstract
61
61
62
62
/**
63
63
* From https://gist.github.com/stemar/8287074
64
- * @param mixed $string The input string.
65
- * @param mixed $replacement The replacement string.
66
- * @param mixed $start If start is positive, the replacing will begin at the start'th offset into string.
64
+ * @param string $string The input string.
65
+ * @param string $replacement The replacement string.
66
+ * @param int $start If start is positive, the replacing will begin at the start'th offset into string.
67
67
* If start is negative, the replacing will begin at the start'th character from the end of string.
68
- * @param mixed $length If given and is positive, it represents the length of the portion of string which is to
68
+ * @param int|null $length If given and is positive, it represents the length of the portion of string which is to
69
69
* be replaced. If it is negative, it represents the number of characters from the end of string at which to
70
70
* stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the
71
71
* end of string. Of course, if length is zero then this function will have the effect of inserting replacement
72
72
* into string at the given start offset.
73
73
* @return string|array The result string is returned. If string is an array then array is returned.
74
74
*/
75
- public function mbSubstrReplace ($ string , $ replacement , $ start , $ length = null )
75
+ public function mbSubstrReplace (string $ string , string $ replacement , int $ start , $ length = null )
76
76
{
77
77
if (is_array ($ string )) {
78
78
$ num = count ($ string );
@@ -115,6 +115,54 @@ public function mbSubstrReplace($string, $replacement, $start, $length = null)
115
115
return join ($ smatches ['0 ' ]);
116
116
}
117
117
118
+ /**
119
+ * @param string|array $changes
120
+ * @param SideBySide|Inline $object
121
+ * @return string
122
+ */
123
+ public function renderHtml ($ changes , $ object )
124
+ {
125
+ $ html = '' ;
126
+ if (empty ($ changes )) {
127
+ return $ html ;
128
+ }
129
+
130
+ $ html .= $ object ->generateTableHeader ();
131
+
132
+ foreach ($ changes as $ i => $ blocks ) {
133
+ // If this is a separate block, we're condensing code so output ...,
134
+ // indicating a significant portion of the code has been collapsed as
135
+ // it is the same
136
+ if ($ i > 0 ) {
137
+ $ html .= $ object ->generateSkippedTable ();
138
+ }
139
+
140
+ foreach ($ blocks as $ change ) {
141
+ $ html .= '<tbody class="Change ' .ucfirst ($ change ['tag ' ]).'"> ' ;
142
+ switch ($ change ['tag ' ]) {
143
+ // Equal changes should be shown on both sides of the diff
144
+ case 'equal ' :
145
+ $ html .= $ object ->generateTableRowsEqual ($ change );
146
+ break ;
147
+ // Added lines only on the right side
148
+ case 'insert ' :
149
+ $ html .= $ object ->generateTableRowsInsert ($ change );
150
+ break ;
151
+ // Show deleted lines only on the left side
152
+ case 'delete ' :
153
+ $ html .= $ object ->generateTableRowsDelete ($ change );
154
+ break ;
155
+ // Show modified lines on both sides
156
+ case 'replace ' :
157
+ $ html .= $ object ->generateTableRowsReplace ($ change );
158
+ break ;
159
+ }
160
+ $ html .= '</tbody> ' ;
161
+ }
162
+ }
163
+ $ html .= '</table> ' ;
164
+ return $ html ;
165
+ }
118
166
/**
119
167
* Render and return an array structure suitable for generating HTML
120
168
* based differences. Generally called by subclasses that generate a
@@ -204,7 +252,7 @@ public function render()
204
252
* @param string $toLine The second string.
205
253
* @return array Array containing the starting position (0 by default) and the ending position (-1 by default)
206
254
*/
207
- private function getChangeExtent ($ fromLine , $ toLine )
255
+ private function getChangeExtent (string $ fromLine , string $ toLine )
208
256
{
209
257
$ start = 0 ;
210
258
$ limit = min (mb_strlen ($ fromLine ), mb_strlen ($ toLine ));
@@ -230,7 +278,7 @@ private function getChangeExtent($fromLine, $toLine)
230
278
* @param array $lines Array of lines to format.
231
279
* @return array Array of the formatted lines.
232
280
*/
233
- protected function formatLines ($ lines )
281
+ protected function formatLines (array $ lines ) : array
234
282
{
235
283
if ($ this ->options ['tabSize ' ] !== false ) {
236
284
$ lines = array_map (array ($ this , 'ExpandTabs ' ), $ lines );
@@ -248,7 +296,7 @@ protected function formatLines($lines)
248
296
* @param array $matches The string of spaces.
249
297
* @return string The HTML representation of the string.
250
298
*/
251
- protected function fixSpaces ($ matches )
299
+ protected function fixSpaces (array $ matches ) : string
252
300
{
253
301
$ buffer = '' ;
254
302
$ count = 0 ;
@@ -273,7 +321,7 @@ protected function fixSpaces($matches)
273
321
* @param string $line The containing tabs to convert.
274
322
* @return string The line with the tabs converted to spaces.
275
323
*/
276
- private function expandTabs ($ line )
324
+ private function expandTabs (string $ line ) : string
277
325
{
278
326
$ tabSize = $ this ->options ['tabSize ' ];
279
327
while (($ pos = strpos ($ line , "\t" )) !== false ) {
@@ -292,7 +340,7 @@ private function expandTabs($line)
292
340
* @param string $string The string.
293
341
* @return string The string with the HTML characters replaced by entities.
294
342
*/
295
- private function htmlSafe ($ string )
343
+ private function htmlSafe (string $ string ) : string
296
344
{
297
345
return htmlspecialchars ($ string , ENT_NOQUOTES , 'UTF-8 ' );
298
346
}
@@ -303,7 +351,7 @@ private function htmlSafe($string)
303
351
* @param integer $j1
304
352
* @return array
305
353
*/
306
- private function getDefaultArray ($ tag , $ i1 , $ j1 )
354
+ private function getDefaultArray (string $ tag , int $ i1 , int $ j1 ) : array
307
355
{
308
356
return array
309
357
(
0 commit comments