@@ -18,7 +18,8 @@ class ListDiff extends HtmlDiff
1818 protected $ list ; // hold the old/new content of the content of the list
1919 protected $ childLists ; // contains the old/new child lists content within this list
2020 protected $ textMatches ; // contains the old/new text strings that match
21- protected $ childListObjects ;
21+ //protected $childListObjects;
22+ protected $ listsIndex ;
2223
2324 public function build ()
2425 {
@@ -30,7 +31,8 @@ public function build()
3031 $ this ->replaceIsolatedDiffTags ();
3132 $ this ->indexNewWords ();
3233 $ this ->diffListContent ();
33- die;
34+
35+ return $ this ->content ;
3436 }
3537
3638 protected function diffListContent ()
@@ -76,6 +78,8 @@ protected function matchAndCompareLists()
7678 {
7779 // Build childLists array of old/new content of lists.
7880 $ this ->buildChildLists ();
81+
82+ $ this ->indexLists ();
7983 // Compare the lists, saving total matches to textMatches array.
8084 $ this ->compareChildLists ();
8185 // Create the child list objects from textMatches array
@@ -86,68 +90,70 @@ protected function compareChildLists()
8690 {
8791 // Always compare the new against the old.
8892 // Compare each new string against each old string.
89- $ matchPercentages = array ();
93+ $ bestMatchPercentages = array ();
9094 foreach ($ this ->childLists ['new ' ] as $ thisKey => $ thisList ) {
91- $ matchPercentages [$ thisKey ] = array ();
95+ $ bestMatchPercentages [$ thisKey ] = array ();
9296 foreach ($ this ->childLists ['old ' ] as $ thatKey => $ thatList ) {
9397 similar_text ($ thisList , $ thatList , $ percentage );
94- $ matchPercentages [$ thisKey ][] = $ percentage ;
98+ $ bestMatchPercentages [$ thisKey ][] = $ percentage ;
9599 }
96100 }
97- //var_dump($matchPercentages);
98101
99- $ bestMatchPercentages = $ matchPercentages ;
100102 foreach ($ bestMatchPercentages as &$ thisMatch ) {
101103 arsort ($ thisMatch );
102104 }
103- var_dump ($ bestMatchPercentages );
105+ // var_dump($bestMatchPercentages);
104106
105107 // Build matches.
106108 $ matches = array ();
107109 $ taken = array ();
108- $ takenItems = array (2 ,3 );
109- $ absolute = 100 ;
110+ $ absoluteMatch = 100 ;
110111 foreach ($ bestMatchPercentages as $ item => $ percentages ) {
111112 $ highestMatch = -1 ;
112113 $ highestMatchKey = -1 ;
113114
114115 foreach ($ percentages as $ key => $ percent ) {
115- $ str = "key: " .$ key ." / percent: " .$ percent ;
116- //var_dump($str);
116+ // Check that the key for the percentage is not already taken and the new percentage is higher.
117117 if (!in_array ($ key , $ taken ) && $ percent > $ highestMatch ) {
118- // If matches 100%, set and move on.
119- /*
120- * if ($percent == $absolute) {
118+ // If an absolute match, choose this one.
119+ if ($ percent == $ absoluteMatch ) {
121120 $ highestMatch = $ percent ;
122121 $ highestMatchKey = $ key ;
123122 break ;
124123 } else {
125- // If not an absolute match, loop through the other high results, checking if any are higher
126- foreach ($bestMatchPercentages as $otherItem => $otherPercentages) {
127- if ($otherPercentages[$key] > $percent) {
128- array_column($taken, $otherPercentages)
124+ // Get all the other matces for the same $key
125+ $ columns = array_column ($ bestMatchPercentages , $ key );
126+ //$str = "All the other matches for this key:".$key; var_dump($str);
127+ //var_dump($columns);
128+ $ thisBestMatches = array_filter (
129+ $ columns ,
130+ function ($ v ) use ($ percent ) {
131+ return $ v > $ percent ;
129132 }
133+ );
134+
135+ //$str = "Best Matches Sorted, with lower matches filtered out: ".$percent; var_dump($str);
136+ arsort ($ thisBestMatches );
137+ //var_dump($thisBestMatches);
138+
139+ // If no greater amounts, use this one.
140+ if (!count ($ thisBestMatches )) {
141+ $ highestMatch = $ percent ;
142+ $ highestMatchKey = $ key ;
143+ break ;
130144 }
131- }
132- */
133- $ str = "Key: " .$ key ." / percent: " .$ percent ; var_dump ($ str );
134- $ columns = array_column ($ bestMatchPercentages , $ key );
135- var_dump (
136- // Start to filter: GOAL is to get values higher than $percent and keys not included in $takenItems
137- array_filter (
138- // Build array we want the filter to use
139- $ columns ,
140- // return if value is higher than percent
141- function ($ v ) use ($ percent , $ columns ) {
142- return $ v > $ percent && ();
145+
146+ // Loop through, comparing only the items that have not already been added.
147+ /*foreach ($thisBestMatches as $k => $v) {
148+ if (!in_array($k, $takenItems)) {
149+ $highestMatch = $percent;
150+ $highestMatchKey = $key;
151+ $takenItemKey = $item;
152+ break(2);
143153 }
144- )
145- );
146- /*var_dump(
147- (array_column($bestMatchPercentages, $key))
148- );*/
154+ }*/
155+ }
149156 }
150- die;
151157 }
152158
153159 $ matches [] = array ('new ' => $ item , 'old ' => $ highestMatchKey > -1 ? $ highestMatchKey : null );
@@ -158,7 +164,7 @@ function ($v) use ($percent, $columns) {
158164
159165 // Save the matches.
160166 $ this ->textMatches = $ matches ;
161- //var_dump ($matches);
167+ $ this -> dump ($ matches );
162168 }
163169
164170 protected function buildChildLists ()
@@ -178,7 +184,89 @@ protected function createChildListObjects()
178184
179185 protected function diff ()
180186 {
187+ $ this ->content = $ this ->addListTypeWrapper ();
188+
189+ foreach ($ this ->textMatches as $ key => $ matches ) {
190+ $ oldText = $ matches ['old ' ] !== null ? $ this ->childLists ['old ' ][$ matches ['old ' ]] : '' ;
191+ $ newText = $ matches ['new ' ] !== null ? $ this ->childLists ['new ' ][$ matches ['new ' ]] : '' ;
192+ $ this ->dump ("OLD TEXT: " . $ oldText );
193+ $ this ->dump ("NEW TEXT: " .$ newText );
194+
195+ $ this ->content .= "<li> " ;
196+ if ($ newText && !$ oldText ) {
197+ $ this ->content .= $ newText ;
198+ } elseif ($ oldText && !$ newText ) {
199+ $ this ->content .= "THIS RIGHT HERE " ;
200+ } else {
201+ $ thisDiff = $ this ->processPlaceholders ($ this ->diffElements ($ oldText , $ newText ), $ matches );
202+ $ this ->content .= $ thisDiff ;
203+ }
204+ $ this ->content .= "</li> " ;
205+ }
206+
207+ $ this ->content .= $ this ->addListTypeWrapper (false );
208+ }
209+
210+ protected function processPlaceholders ($ text , array $ matches )
211+ {
212+ $ returnText = array ();
213+ $ contentVault = array (
214+ 'old ' => $ this ->getListContent ('old ' , $ matches ),
215+ 'new ' => $ this ->getListContent ('new ' , $ matches )
216+ );
217+
218+ $ count = 0 ;
219+ foreach (explode (' ' , $ text ) as $ word ) {
220+ $ content = $ word ;
221+ if (in_array ($ word , $ this ->isolatedDiffTags )) {
222+ $ oldText = implode ('' , $ contentVault ['old ' ][$ count ]);
223+ $ newText = implode ('' , $ contentVault ['new ' ][$ count ]);
224+ $ content = $ this ->diffList ($ oldText , $ newText );
225+ $ count ++;
226+ }
227+
228+ $ returnText [] = $ content ;
229+ }
230+ return implode (' ' , $ returnText );
231+ }
232+
233+ protected function getListContent ($ indexKey = 'new ' , array $ matches )
234+ {
235+ $ bucket = array ();
236+ $ start = $ this ->listsIndex [$ indexKey ][$ matches [$ indexKey ]];
237+ $ stop = $ this ->listsIndex [$ indexKey ][$ matches [$ indexKey ] + 1 ];
238+ for ($ x = $ start ; $ x < $ stop ; $ x ++) {
239+ if (in_array ($ this ->list [$ indexKey ][$ x ], $ this ->isolatedDiffTags )) {
240+ $ bucket [] = $ this ->listIsolatedDiffTags [$ indexKey ][$ x ];
241+ }
242+ }
243+ return $ bucket ;
244+ }
245+
246+ protected function indexLists ()
247+ {
248+ $ this ->listsIndex = array ();
249+ $ lookingFor = "<li> " ;
181250
251+ foreach ($ this ->list as $ type => $ list ) {
252+ $ this ->listsIndex [$ type ] = array ();
253+
254+ foreach ($ list as $ key => $ listItem ) {
255+ if ($ listItem == $ lookingFor ) {
256+ $ this ->listsIndex [$ type ][] = $ key ;
257+ }
258+ }
259+ }
260+ }
261+
262+ protected function addListTypeWrapper ($ opening = true )
263+ {
264+ return "< " . (!$ opening ? "/ " : '' ) . $ this ->listType . "> " ;
265+ }
266+
267+ protected function dump ($ content )
268+ {
269+ var_dump ($ content );
182270 }
183271
184272 public function replaceListIsolatedDiffTags ()
0 commit comments