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