|
4 | 4 |
|
5 | 5 | class ListDiff extends HtmlDiff
|
6 | 6 | {
|
| 7 | + /** |
| 8 | + * This is the minimum percentage a list item can match its counterpart in order to be considered a match. |
| 9 | + * @var integer |
| 10 | + */ |
| 11 | + protected static $listMatchThreshold = 50; |
| 12 | + |
7 | 13 | /** @var array */
|
8 | 14 | protected $listWords = array();
|
9 | 15 |
|
@@ -106,22 +112,42 @@ protected function diffListContent()
|
106 | 112 | */
|
107 | 113 | protected function formatThisListContent()
|
108 | 114 | {
|
109 |
| - foreach ($this->oldIsolatedDiffTags as $key => $diffTagArray) { |
110 |
| - $openingTag = $this->getAndStripTag($diffTagArray[0]); |
111 |
| - $closingTag = $this->getAndStripTag($diffTagArray[count($diffTagArray) - 1]); |
112 |
| - |
113 |
| - if (array_key_exists($openingTag, $this->isolatedDiffTags) && |
114 |
| - array_key_exists($closingTag, $this->isolatedDiffTags) |
115 |
| - ) { |
116 |
| - $this->listType = $openingTag; |
117 |
| - array_shift($this->oldIsolatedDiffTags[$key]); |
118 |
| - array_pop($this->oldIsolatedDiffTags[$key]); |
119 |
| - array_shift($this->newIsolatedDiffTags[$key]); |
120 |
| - array_pop($this->newIsolatedDiffTags[$key]); |
121 |
| - $this->list['old'] = $this->oldIsolatedDiffTags[$key]; |
122 |
| - $this->list['new'] = $this->newIsolatedDiffTags[$key]; |
| 115 | + $formatArray = array( |
| 116 | + array('type' => 'old', 'array' => $this->oldIsolatedDiffTags), |
| 117 | + array('type' => 'new', 'array' => $this->newIsolatedDiffTags) |
| 118 | + ); |
| 119 | + |
| 120 | + foreach ($formatArray as $item) { |
| 121 | + $values = array_values($item['array']); |
| 122 | + $this->list[$item['type']] = count($values) |
| 123 | + ? $this->formatList($values[0], $item['type']) |
| 124 | + : array(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * |
| 130 | + * @param array $arrayData |
| 131 | + * @param string $index |
| 132 | + * @return array |
| 133 | + */ |
| 134 | + protected function formatList(array $arrayData, $index = 'old') |
| 135 | + { |
| 136 | + $openingTag = $this->getAndStripTag($arrayData[0]); |
| 137 | + $closingTag = $this->getAndStripTag($arrayData[count($arrayData) - 1]); |
| 138 | + |
| 139 | + if (array_key_exists($openingTag, $this->isolatedDiffTags) && |
| 140 | + array_key_exists($closingTag, $this->isolatedDiffTags) |
| 141 | + ) { |
| 142 | + if ($index == 'old') { |
| 143 | + $this->listType = $this->getAndStripTag($arrayData[0]); |
123 | 144 | }
|
| 145 | + |
| 146 | + array_shift($arrayData); |
| 147 | + array_pop($arrayData); |
124 | 148 | }
|
| 149 | + |
| 150 | + return $arrayData; |
125 | 151 | }
|
126 | 152 |
|
127 | 153 | /**
|
@@ -206,22 +232,27 @@ function ($v) use ($percent) {
|
206 | 232 | );
|
207 | 233 |
|
208 | 234 | arsort($thisBestMatches);
|
209 |
| - |
210 |
| - // If no greater amounts, use this one. |
211 |
| - if (!count($thisBestMatches)) { |
212 |
| - $highestMatch = $percent; |
213 |
| - $highestMatchKey = $key; |
214 |
| - $takenItemKey = $item; |
215 |
| - break; |
216 |
| - } |
217 |
| - |
218 |
| - // Loop through, comparing only the items that have not already been added. |
219 |
| - foreach ($thisBestMatches as $k => $v) { |
220 |
| - if (in_array($k, $takenItems)) { |
| 235 | + |
| 236 | + /** |
| 237 | + * If the list item does not meet the threshold, it will not be considered a match. |
| 238 | + */ |
| 239 | + if ($percent >= self::$listMatchThreshold) { |
| 240 | + // If no greater amounts, use this one. |
| 241 | + if (!count($thisBestMatches)) { |
221 | 242 | $highestMatch = $percent;
|
222 | 243 | $highestMatchKey = $key;
|
223 | 244 | $takenItemKey = $item;
|
224 |
| - break(2); |
| 245 | + break; |
| 246 | + } |
| 247 | + |
| 248 | + // Loop through, comparing only the items that have not already been added. |
| 249 | + foreach ($thisBestMatches as $k => $v) { |
| 250 | + if (in_array($k, $takenItems)) { |
| 251 | + $highestMatch = $percent; |
| 252 | + $highestMatchKey = $key; |
| 253 | + $takenItemKey = $item; |
| 254 | + break(2); |
| 255 | + } |
225 | 256 | }
|
226 | 257 | }
|
227 | 258 | }
|
@@ -350,27 +381,27 @@ protected function convertListContentArrayToString($listContentArray)
|
350 | 381 | * @return string
|
351 | 382 | */
|
352 | 383 | protected function processPlaceholders($text, array $matches)
|
353 |
| - { |
| 384 | + { |
354 | 385 | // Prepare return
|
355 | 386 | $returnText = array();
|
356 | 387 | // Save the contents of all list nodes, new and old.
|
357 | 388 | $contentVault = array(
|
358 | 389 | 'old' => $this->getListContent('old', $matches),
|
359 | 390 | 'new' => $this->getListContent('new', $matches)
|
360 | 391 | );
|
361 |
| - |
| 392 | + |
362 | 393 | $count = 0;
|
363 | 394 | // Loop through the text checking for placeholders. If a nested list is found, create a new ListDiff object for it.
|
364 | 395 | foreach (explode(' ', $text) as $word) {
|
365 | 396 | $preContent = $this->checkWordForDiffTag($this->stripNewLine($word));
|
366 |
| - |
| 397 | + |
367 | 398 | if (in_array(
|
368 | 399 | is_array($preContent) ? $preContent[1] : $preContent,
|
369 | 400 | $this->isolatedDiffTags
|
370 | 401 | )
|
371 | 402 | ) {
|
372 |
| - $oldText = implode('', $contentVault['old'][$count]); |
373 |
| - $newText = implode('', $contentVault['new'][$count]); |
| 403 | + $oldText = array_key_exists($count, $contentVault['old']) ? implode('', $contentVault['old'][$count]) : ''; |
| 404 | + $newText = array_key_exists($count, $contentVault['new']) ? implode('', $contentVault['new'][$count]) : ''; |
374 | 405 | $content = $this->diffList($oldText, $newText);
|
375 | 406 | $count++;
|
376 | 407 | } else {
|
@@ -427,22 +458,21 @@ protected function stripNewLine($text)
|
427 | 458 | * @return array
|
428 | 459 | */
|
429 | 460 | protected function getListContent($indexKey = 'new', array $matches)
|
430 |
| - { |
| 461 | + { |
431 | 462 | $bucket = array();
|
432 | 463 |
|
433 | 464 | if (isset($matches[$indexKey]) && $matches[$indexKey] !== null) {
|
434 | 465 | $start = $this->listsIndex[$indexKey][$matches[$indexKey]];
|
435 |
| - $stop = array_key_exists(($matches[$indexKey] + 1), $this->listsIndex[$indexKey]) |
436 |
| - ? $this->listsIndex[$indexKey][$matches[$indexKey] + 1] |
437 |
| - : $this->findEndForIndex($this->list[$indexKey], $start); |
438 |
| - |
439 |
| - for ($x = $start; $x < $stop; $x++) { |
| 466 | + $stop = $this->findEndForIndex($this->list[$indexKey], $start); |
| 467 | + |
| 468 | + for ($x = $start; $x <= $stop; $x++) { |
| 469 | + |
440 | 470 | if (in_array($this->list[$indexKey][$x], $this->isolatedDiffTags)) {
|
441 | 471 | $bucket[] = $this->listIsolatedDiffTags[$indexKey][$x];
|
442 | 472 | }
|
443 | 473 | }
|
444 | 474 | }
|
445 |
| - |
| 475 | + |
446 | 476 | return $bucket;
|
447 | 477 | }
|
448 | 478 |
|
|
0 commit comments