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
|
|
|
* @var null|\Doctrine\Common\Cache\Cache |
73
|
|
|
*/ |
74
|
|
|
protected $cacheProvider; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var null|string |
78
|
|
|
*/ |
79
|
|
|
protected $purifierCacheLocation = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return HtmlDiffConfig |
83
|
|
|
*/ |
84
|
11 |
|
public static function create() |
85
|
|
|
{ |
86
|
11 |
|
return new self(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* HtmlDiffConfig constructor. |
91
|
|
|
*/ |
92
|
11 |
|
public function __construct() |
93
|
|
|
{ |
94
|
11 |
|
$this->setSpecialCaseTags($this->specialCaseTags); |
95
|
11 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
4 |
|
public function getMatchThreshold() |
101
|
|
|
{ |
102
|
4 |
|
return $this->matchThreshold; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $matchThreshold |
107
|
|
|
* |
108
|
|
|
* @return AbstractDiff |
|
|
|
|
109
|
|
|
*/ |
110
|
|
|
public function setMatchThreshold($matchThreshold) |
111
|
|
|
{ |
112
|
|
|
$this->matchThreshold = $matchThreshold; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $chars |
119
|
|
|
*/ |
120
|
|
|
public function setSpecialCaseChars(array $chars) |
121
|
|
|
{ |
122
|
|
|
$this->specialCaseChars = $chars; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array|null |
127
|
|
|
*/ |
128
|
11 |
|
public function getSpecialCaseChars() |
129
|
|
|
{ |
130
|
11 |
|
return $this->specialCaseChars; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $char |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function addSpecialCaseChar($char) |
139
|
|
|
{ |
140
|
|
|
if (!in_array($char, $this->specialCaseChars)) { |
141
|
|
|
$this->specialCaseChars[] = $char; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $char |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function removeSpecialCaseChar($char) |
153
|
|
|
{ |
154
|
|
|
$key = array_search($char, $this->specialCaseChars); |
155
|
|
|
if ($key !== false) { |
156
|
|
|
unset($this->specialCaseChars[$key]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $tags |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
11 |
|
public function setSpecialCaseTags(array $tags = array()) |
168
|
|
|
{ |
169
|
11 |
|
$this->specialCaseTags = $tags; |
170
|
11 |
|
$this->specialCaseOpeningTags = array(); |
171
|
11 |
|
$this->specialCaseClosingTags = array(); |
172
|
|
|
|
173
|
11 |
|
foreach ($this->specialCaseTags as $tag) { |
174
|
11 |
|
$this->addSpecialCaseTag($tag); |
175
|
11 |
|
} |
176
|
|
|
|
177
|
11 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $tag |
182
|
|
|
* |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
11 |
|
public function addSpecialCaseTag($tag) |
186
|
|
|
{ |
187
|
11 |
|
if (!in_array($tag, $this->specialCaseTags)) { |
188
|
|
|
$this->specialCaseTags[] = $tag; |
189
|
|
|
} |
190
|
|
|
|
191
|
11 |
|
$opening = $this->getOpeningTag($tag); |
192
|
11 |
|
$closing = $this->getClosingTag($tag); |
193
|
|
|
|
194
|
11 |
|
if (!in_array($opening, $this->specialCaseOpeningTags)) { |
195
|
11 |
|
$this->specialCaseOpeningTags[] = $opening; |
196
|
11 |
|
} |
197
|
11 |
|
if (!in_array($closing, $this->specialCaseClosingTags)) { |
198
|
11 |
|
$this->specialCaseClosingTags[] = $closing; |
199
|
11 |
|
} |
200
|
|
|
|
201
|
11 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $tag |
206
|
|
|
* |
207
|
|
|
* @return $this |
208
|
|
|
*/ |
209
|
|
|
public function removeSpecialCaseTag($tag) |
210
|
|
|
{ |
211
|
|
|
if (($key = array_search($tag, $this->specialCaseTags)) !== false) { |
212
|
|
|
unset($this->specialCaseTags[$key]); |
213
|
|
|
|
214
|
|
|
$opening = $this->getOpeningTag($tag); |
215
|
|
|
$closing = $this->getClosingTag($tag); |
216
|
|
|
|
217
|
|
|
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) { |
218
|
|
|
unset($this->specialCaseOpeningTags[$key]); |
219
|
|
|
} |
220
|
|
|
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) { |
221
|
|
|
unset($this->specialCaseClosingTags[$key]); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return array|null |
230
|
|
|
*/ |
231
|
|
|
public function getSpecialCaseTags() |
232
|
|
|
{ |
233
|
|
|
return $this->specialCaseTags; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return boolean |
239
|
|
|
*/ |
240
|
11 |
|
public function isGroupDiffs() |
241
|
|
|
{ |
242
|
11 |
|
return $this->groupDiffs; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param boolean $groupDiffs |
247
|
|
|
* |
248
|
|
|
* @return HtmlDiffConfig |
249
|
|
|
*/ |
250
|
|
|
public function setGroupDiffs($groupDiffs) |
251
|
|
|
{ |
252
|
|
|
$this->groupDiffs = $groupDiffs; |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
public function getEncoding() |
261
|
|
|
{ |
262
|
|
|
return $this->encoding; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param string $encoding |
267
|
|
|
* |
268
|
|
|
* @return HtmlDiffConfig |
269
|
|
|
*/ |
270
|
11 |
|
public function setEncoding($encoding) |
271
|
|
|
{ |
272
|
11 |
|
$this->encoding = $encoding; |
273
|
|
|
|
274
|
11 |
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return boolean |
279
|
|
|
*/ |
280
|
|
|
public function isInsertSpaceInReplace() |
281
|
|
|
{ |
282
|
|
|
return $this->insertSpaceInReplace; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param boolean $insertSpaceInReplace |
287
|
|
|
* |
288
|
|
|
* @return HtmlDiffConfig |
289
|
|
|
*/ |
290
|
|
|
public function setInsertSpaceInReplace($insertSpaceInReplace) |
291
|
|
|
{ |
292
|
|
|
$this->insertSpaceInReplace = $insertSpaceInReplace; |
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
11 |
|
public function getIsolatedDiffTags() |
301
|
|
|
{ |
302
|
11 |
|
return $this->isolatedDiffTags; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param array $isolatedDiffTags |
307
|
|
|
* |
308
|
|
|
* @return HtmlDiffConfig |
309
|
|
|
*/ |
310
|
|
|
public function setIsolatedDiffTags($isolatedDiffTags) |
311
|
|
|
{ |
312
|
|
|
$this->isolatedDiffTags = $isolatedDiffTags; |
313
|
|
|
|
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param string $tag |
319
|
|
|
* @param null|string $placeholder |
320
|
|
|
* |
321
|
|
|
* @return $this |
322
|
|
|
*/ |
323
|
|
|
public function addIsolatedDiffTag($tag, $placeholder = null) |
324
|
|
|
{ |
325
|
|
|
if (null === $placeholder) { |
326
|
|
|
$placeholder = sprintf('[[REPLACE_%s]]', strtoupper($tag)); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if ($this->isIsolatedDiffTag($tag) && $this->isolatedDiffTags[$tag] !== $placeholder) { |
330
|
|
|
throw new \InvalidArgumentException( |
331
|
|
|
sprintf('Isolated diff tag "%s" already exists using a different placeholder', $tag) |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$matchingKey = array_search($placeholder, $this->isolatedDiffTags, true); |
336
|
|
|
if (false !== $matchingKey && $matchingKey !== $tag) { |
337
|
|
|
throw new \InvalidArgumentException( |
338
|
|
|
sprintf('Placeholder already being used for a different tag "%s"', $tag) |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if (!array_key_exists($tag, $this->isolatedDiffTags)) { |
343
|
|
|
$this->isolatedDiffTags[$tag] = $placeholder; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param string $tag |
351
|
|
|
* |
352
|
|
|
* @return $this |
353
|
|
|
*/ |
354
|
|
|
public function removeIsolatedDiffTag($tag) |
355
|
|
|
{ |
356
|
|
|
if ($this->isIsolatedDiffTag($tag)) { |
357
|
|
|
unset($this->isolatedDiffTags[$tag]); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string $tag |
365
|
|
|
* |
366
|
|
|
* @return bool |
367
|
|
|
*/ |
368
|
11 |
|
public function isIsolatedDiffTag($tag) |
369
|
|
|
{ |
370
|
11 |
|
return array_key_exists($tag, $this->isolatedDiffTags); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param string $text |
375
|
|
|
* |
376
|
|
|
* @return bool |
377
|
|
|
*/ |
378
|
11 |
|
public function isIsolatedDiffTagPlaceholder($text) |
379
|
|
|
{ |
380
|
11 |
|
return in_array($text, $this->isolatedDiffTags, true); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @param string $tag |
385
|
|
|
* |
386
|
|
|
* @return null|string |
387
|
|
|
*/ |
388
|
11 |
|
public function getIsolatedDiffTagPlaceholder($tag) |
389
|
|
|
{ |
390
|
11 |
|
return $this->isIsolatedDiffTag($tag) ? $this->isolatedDiffTags[$tag] : null; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @return array |
395
|
|
|
*/ |
396
|
6 |
|
public function getSpecialCaseOpeningTags() |
397
|
|
|
{ |
398
|
6 |
|
return $this->specialCaseOpeningTags; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @return array |
403
|
|
|
*/ |
404
|
6 |
|
public function getSpecialCaseClosingTags() |
405
|
|
|
{ |
406
|
6 |
|
return $this->specialCaseClosingTags; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @return boolean |
411
|
|
|
*/ |
412
|
5 |
|
public function isUseTableDiffing() |
413
|
|
|
{ |
414
|
5 |
|
return $this->useTableDiffing; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @param boolean $useTableDiffing |
419
|
|
|
* |
420
|
|
|
* @return HtmlDiffConfig |
421
|
|
|
*/ |
422
|
|
|
public function setUseTableDiffing($useTableDiffing) |
423
|
|
|
{ |
424
|
|
|
$this->useTableDiffing = $useTableDiffing; |
425
|
|
|
|
426
|
|
|
return $this; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @param null|\Doctrine\Common\Cache\Cache $cacheProvider |
431
|
|
|
* |
432
|
|
|
* @return $this |
433
|
|
|
*/ |
434
|
|
|
public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null) |
435
|
|
|
{ |
436
|
|
|
$this->cacheProvider = $cacheProvider; |
437
|
|
|
|
438
|
|
|
return $this; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @return null|\Doctrine\Common\Cache\Cache |
443
|
|
|
*/ |
444
|
11 |
|
public function getCacheProvider() |
445
|
|
|
{ |
446
|
11 |
|
return $this->cacheProvider; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* @param null|string |
451
|
|
|
* |
452
|
|
|
* @return $this |
453
|
|
|
*/ |
454
|
|
|
public function setPurifierCacheLocation($purifierCacheLocation = null) |
455
|
|
|
{ |
456
|
|
|
$this->purifierCacheLocation = $purifierCacheLocation; |
457
|
|
|
|
458
|
|
|
return $this; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* @return null|string |
463
|
|
|
*/ |
464
|
|
|
public function getPurifierCacheLocation() |
465
|
|
|
{ |
466
|
|
|
return $this->purifierCacheLocation; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* @param string $tag |
471
|
|
|
* |
472
|
|
|
* @return string |
473
|
|
|
*/ |
474
|
11 |
|
protected function getOpeningTag($tag) |
475
|
|
|
{ |
476
|
11 |
|
return "/<".$tag."[^>]*/i"; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* @param string $tag |
481
|
|
|
* |
482
|
|
|
* @return string |
483
|
|
|
*/ |
484
|
11 |
|
protected function getClosingTag($tag) |
485
|
|
|
{ |
486
|
11 |
|
return "</".$tag.">"; |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
|
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.