1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caxy\HtmlDiff; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class HtmlDiffConfig |
7
|
|
|
* @package Caxy\HtmlDiff |
8
|
|
|
*/ |
9
|
|
|
class HtmlDiffConfig |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $specialCaseChars = array('.', ',', '(', ')', '\''); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $groupDiffs = true; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $insertSpaceInReplace = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $encoding = 'UTF-8'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $isolatedDiffTags = array( |
40
|
|
|
'ol' => '[[REPLACE_ORDERED_LIST]]', |
41
|
|
|
'ul' => '[[REPLACE_UNORDERED_LIST]]', |
42
|
|
|
'sub' => '[[REPLACE_SUB_SCRIPT]]', |
43
|
|
|
'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
44
|
|
|
'dl' => '[[REPLACE_DEFINITION_LIST]]', |
45
|
|
|
'table' => '[[REPLACE_TABLE]]', |
46
|
|
|
'strong' => '[[REPLACE_STRONG]]', |
47
|
|
|
'b' => '[[REPLACE_B]]', |
48
|
|
|
'em' => '[[REPLACE_EM]]', |
49
|
|
|
'i' => '[[REPLACE_I]]', |
50
|
|
|
'a' => '[[REPLACE_A]]', |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $matchThreshold = 80; |
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $specialCaseOpeningTags = array(); |
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $specialCaseClosingTags = array(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
protected $useTableDiffing = true; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return HtmlDiffConfig |
73
|
|
|
*/ |
74
|
11 |
|
public static function create() |
75
|
|
|
{ |
76
|
11 |
|
return new self(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* HtmlDiffConfig constructor. |
81
|
|
|
*/ |
82
|
11 |
|
public function __construct() |
83
|
|
|
{ |
84
|
11 |
|
$this->setSpecialCaseTags($this->specialCaseTags); |
85
|
11 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
4 |
|
public function getMatchThreshold() |
91
|
|
|
{ |
92
|
4 |
|
return $this->matchThreshold; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param int $matchThreshold |
97
|
|
|
* |
98
|
|
|
* @return AbstractDiff |
|
|
|
|
99
|
|
|
*/ |
100
|
|
|
public function setMatchThreshold($matchThreshold) |
101
|
|
|
{ |
102
|
|
|
$this->matchThreshold = $matchThreshold; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $chars |
109
|
|
|
*/ |
110
|
|
|
public function setSpecialCaseChars(array $chars) |
111
|
|
|
{ |
112
|
|
|
$this->specialCaseChars = $chars; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array|null |
117
|
|
|
*/ |
118
|
11 |
|
public function getSpecialCaseChars() |
119
|
|
|
{ |
120
|
11 |
|
return $this->specialCaseChars; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $char |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function addSpecialCaseChar($char) |
129
|
|
|
{ |
130
|
|
|
if (!in_array($char, $this->specialCaseChars)) { |
131
|
|
|
$this->specialCaseChars[] = $char; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $char |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function removeSpecialCaseChar($char) |
143
|
|
|
{ |
144
|
|
|
$key = array_search($char, $this->specialCaseChars); |
145
|
|
|
if ($key !== false) { |
146
|
|
|
unset($this->specialCaseChars[$key]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $tags |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
11 |
|
public function setSpecialCaseTags(array $tags = array()) |
158
|
|
|
{ |
159
|
11 |
|
$this->specialCaseTags = $tags; |
160
|
11 |
|
$this->specialCaseOpeningTags = array(); |
161
|
11 |
|
$this->specialCaseClosingTags = array(); |
162
|
|
|
|
163
|
11 |
|
foreach ($this->specialCaseTags as $tag) { |
164
|
11 |
|
$this->addSpecialCaseTag($tag); |
165
|
11 |
|
} |
166
|
|
|
|
167
|
11 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $tag |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
11 |
|
public function addSpecialCaseTag($tag) |
176
|
|
|
{ |
177
|
11 |
|
if (!in_array($tag, $this->specialCaseTags)) { |
178
|
|
|
$this->specialCaseTags[] = $tag; |
179
|
|
|
} |
180
|
|
|
|
181
|
11 |
|
$opening = $this->getOpeningTag($tag); |
182
|
11 |
|
$closing = $this->getClosingTag($tag); |
183
|
|
|
|
184
|
11 |
|
if (!in_array($opening, $this->specialCaseOpeningTags)) { |
185
|
11 |
|
$this->specialCaseOpeningTags[] = $opening; |
186
|
11 |
|
} |
187
|
11 |
|
if (!in_array($closing, $this->specialCaseClosingTags)) { |
188
|
11 |
|
$this->specialCaseClosingTags[] = $closing; |
189
|
11 |
|
} |
190
|
|
|
|
191
|
11 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $tag |
196
|
|
|
* |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
public function removeSpecialCaseTag($tag) |
200
|
|
|
{ |
201
|
|
|
if (($key = array_search($tag, $this->specialCaseTags)) !== false) { |
202
|
|
|
unset($this->specialCaseTags[$key]); |
203
|
|
|
|
204
|
|
|
$opening = $this->getOpeningTag($tag); |
205
|
|
|
$closing = $this->getClosingTag($tag); |
206
|
|
|
|
207
|
|
|
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) { |
208
|
|
|
unset($this->specialCaseOpeningTags[$key]); |
209
|
|
|
} |
210
|
|
|
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) { |
211
|
|
|
unset($this->specialCaseClosingTags[$key]); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return array|null |
220
|
|
|
*/ |
221
|
|
|
public function getSpecialCaseTags() |
222
|
|
|
{ |
223
|
|
|
return $this->specialCaseTags; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return boolean |
229
|
|
|
*/ |
230
|
11 |
|
public function isGroupDiffs() |
231
|
|
|
{ |
232
|
11 |
|
return $this->groupDiffs; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param boolean $groupDiffs |
237
|
|
|
* |
238
|
|
|
* @return HtmlDiffConfig |
239
|
|
|
*/ |
240
|
|
|
public function setGroupDiffs($groupDiffs) |
241
|
|
|
{ |
242
|
|
|
$this->groupDiffs = $groupDiffs; |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function getEncoding() |
251
|
|
|
{ |
252
|
|
|
return $this->encoding; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $encoding |
257
|
|
|
* |
258
|
|
|
* @return HtmlDiffConfig |
259
|
|
|
*/ |
260
|
11 |
|
public function setEncoding($encoding) |
261
|
|
|
{ |
262
|
11 |
|
$this->encoding = $encoding; |
263
|
|
|
|
264
|
11 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return boolean |
269
|
|
|
*/ |
270
|
|
|
public function isInsertSpaceInReplace() |
271
|
|
|
{ |
272
|
|
|
return $this->insertSpaceInReplace; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param boolean $insertSpaceInReplace |
277
|
|
|
* |
278
|
|
|
* @return HtmlDiffConfig |
279
|
|
|
*/ |
280
|
|
|
public function setInsertSpaceInReplace($insertSpaceInReplace) |
281
|
|
|
{ |
282
|
|
|
$this->insertSpaceInReplace = $insertSpaceInReplace; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return array |
289
|
|
|
*/ |
290
|
11 |
|
public function getIsolatedDiffTags() |
291
|
|
|
{ |
292
|
11 |
|
return $this->isolatedDiffTags; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param array $isolatedDiffTags |
297
|
|
|
* |
298
|
|
|
* @return HtmlDiffConfig |
299
|
|
|
*/ |
300
|
|
|
public function setIsolatedDiffTags($isolatedDiffTags) |
301
|
|
|
{ |
302
|
|
|
$this->isolatedDiffTags = $isolatedDiffTags; |
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param string $tag |
309
|
|
|
* @param null|string $placeholder |
310
|
|
|
* |
311
|
|
|
* @return $this |
312
|
|
|
*/ |
313
|
|
|
public function addIsolatedDiffTag($tag, $placeholder = null) |
314
|
|
|
{ |
315
|
|
|
if (null === $placeholder) { |
316
|
|
|
$placeholder = sprintf('[[REPLACE_%s]]', strtoupper($tag)); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
if ($this->isIsolatedDiffTag($tag) && $this->isolatedDiffTags[$tag] !== $placeholder) { |
320
|
|
|
throw new \InvalidArgumentException( |
321
|
|
|
sprintf('Isolated diff tag "%s" already exists using a different placeholder', $tag) |
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$matchingKey = array_search($placeholder, $this->isolatedDiffTags, true); |
326
|
|
|
if (false !== $matchingKey && $matchingKey !== $tag) { |
327
|
|
|
throw new \InvalidArgumentException( |
328
|
|
|
sprintf('Placeholder already being used for a different tag "%s"', $tag) |
329
|
|
|
); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
if (!array_key_exists($tag, $this->isolatedDiffTags)) { |
333
|
|
|
$this->isolatedDiffTags[$tag] = $placeholder; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param string $tag |
341
|
|
|
* |
342
|
|
|
* @return $this |
343
|
|
|
*/ |
344
|
|
|
public function removeIsolatedDiffTag($tag) |
345
|
|
|
{ |
346
|
|
|
if ($this->isIsolatedDiffTag($tag)) { |
347
|
|
|
unset($this->isolatedDiffTags[$tag]); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return $this; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param string $tag |
355
|
|
|
* |
356
|
|
|
* @return bool |
357
|
|
|
*/ |
358
|
11 |
|
public function isIsolatedDiffTag($tag) |
359
|
|
|
{ |
360
|
11 |
|
return array_key_exists($tag, $this->isolatedDiffTags); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string $text |
365
|
|
|
* |
366
|
|
|
* @return bool |
367
|
|
|
*/ |
368
|
11 |
|
public function isIsolatedDiffTagPlaceholder($text) |
369
|
|
|
{ |
370
|
11 |
|
return in_array($text, $this->isolatedDiffTags, true); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param string $tag |
375
|
|
|
* |
376
|
|
|
* @return null|string |
377
|
|
|
*/ |
378
|
11 |
|
public function getIsolatedDiffTagPlaceholder($tag) |
379
|
|
|
{ |
380
|
11 |
|
return $this->isIsolatedDiffTag($tag) ? $this->isolatedDiffTags[$tag] : null; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @return array |
385
|
|
|
*/ |
386
|
6 |
|
public function getSpecialCaseOpeningTags() |
387
|
|
|
{ |
388
|
6 |
|
return $this->specialCaseOpeningTags; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @return array |
393
|
|
|
*/ |
394
|
6 |
|
public function getSpecialCaseClosingTags() |
395
|
|
|
{ |
396
|
6 |
|
return $this->specialCaseClosingTags; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @return boolean |
401
|
|
|
*/ |
402
|
5 |
|
public function isUseTableDiffing() |
403
|
|
|
{ |
404
|
5 |
|
return $this->useTableDiffing; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param boolean $useTableDiffing |
409
|
|
|
* |
410
|
|
|
* @return HtmlDiffConfig |
411
|
|
|
*/ |
412
|
|
|
public function setUseTableDiffing($useTableDiffing) |
413
|
|
|
{ |
414
|
|
|
$this->useTableDiffing = $useTableDiffing; |
415
|
|
|
|
416
|
|
|
return $this; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* @param string $tag |
421
|
|
|
* |
422
|
|
|
* @return string |
423
|
|
|
*/ |
424
|
11 |
|
protected function getOpeningTag($tag) |
425
|
|
|
{ |
426
|
11 |
|
return "/<".$tag."[^>]*/i"; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @param string $tag |
431
|
|
|
* |
432
|
|
|
* @return string |
433
|
|
|
*/ |
434
|
11 |
|
protected function getClosingTag($tag) |
435
|
|
|
{ |
436
|
11 |
|
return "</".$tag.">"; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.