22
33namespace Caxy \HtmlDiff ;
44
5+ /**
6+ * Class AbstractDiff
7+ * @package Caxy\HtmlDiff
8+ */
59abstract class AbstractDiff
610{
11+ /**
12+ * @var array
13+ */
714 public static $ defaultSpecialCaseTags = array ('strong ' , 'b ' , 'i ' , 'big ' , 'small ' , 'u ' , 'sub ' , 'sup ' , 'strike ' , 's ' , 'p ' );
15+ /**
16+ * @var array
17+ */
818 public static $ defaultSpecialCaseChars = array ('. ' , ', ' , '( ' , ') ' , '\'' );
19+ /**
20+ * @var bool
21+ */
922 public static $ defaultGroupDiffs = true ;
1023
24+ /**
25+ * @var string
26+ */
1127 protected $ content ;
28+ /**
29+ * @var string
30+ */
1231 protected $ oldText ;
32+ /**
33+ * @var string
34+ */
1335 protected $ newText ;
36+ /**
37+ * @var array
38+ */
1439 protected $ oldWords = array ();
40+ /**
41+ * @var array
42+ */
1543 protected $ newWords = array ();
44+ /**
45+ * @var string
46+ */
1647 protected $ encoding ;
48+ /**
49+ * @var array
50+ */
1751 protected $ specialCaseOpeningTags = array ();
52+ /**
53+ * @var array
54+ */
1855 protected $ specialCaseClosingTags = array ();
56+ /**
57+ * @var array|null
58+ */
1959 protected $ specialCaseTags ;
60+ /**
61+ * @var array|null
62+ */
2063 protected $ specialCaseChars ;
64+ /**
65+ * @var bool|null
66+ */
2167 protected $ groupDiffs ;
68+ /**
69+ * @var int
70+ */
2271 protected $ matchThreshold = 80 ;
2372
73+ /**
74+ * AbstractDiff constructor.
75+ *
76+ * @param string $oldText
77+ * @param string $newText
78+ * @param string $encoding
79+ * @param null|array $specialCaseTags
80+ * @param null|bool $groupDiffs
81+ */
2482 public function __construct ($ oldText , $ newText , $ encoding = 'UTF-8 ' , $ specialCaseTags = null , $ groupDiffs = null )
2583 {
2684 if ($ specialCaseTags === null ) {
@@ -60,25 +118,35 @@ public function setMatchThreshold($matchThreshold)
60118 return $ this ;
61119 }
62120
63-
64-
121+ /**
122+ * @param array $chars
123+ */
65124 public function setSpecialCaseChars (array $ chars )
66125 {
67126 $ this ->specialCaseChars = $ chars ;
68127 }
69128
129+ /**
130+ * @return array|null
131+ */
70132 public function getSpecialCaseChars ()
71133 {
72134 return $ this ->specialCaseChars ;
73135 }
74136
137+ /**
138+ * @param string $char
139+ */
75140 public function addSpecialCaseChar ($ char )
76141 {
77142 if (!in_array ($ char , $ this ->specialCaseChars )) {
78143 $ this ->specialCaseChars [] = $ char ;
79144 }
80145 }
81146
147+ /**
148+ * @param string $char
149+ */
82150 public function removeSpecialCaseChar ($ char )
83151 {
84152 $ key = array_search ($ char , $ this ->specialCaseChars );
@@ -87,6 +155,9 @@ public function removeSpecialCaseChar($char)
87155 }
88156 }
89157
158+ /**
159+ * @param array $tags
160+ */
90161 public function setSpecialCaseTags (array $ tags = array ())
91162 {
92163 $ this ->specialCaseTags = $ tags ;
@@ -96,6 +167,9 @@ public function setSpecialCaseTags(array $tags = array())
96167 }
97168 }
98169
170+ /**
171+ * @param string $tag
172+ */
99173 public function addSpecialCaseTag ($ tag )
100174 {
101175 if (!in_array ($ tag , $ this ->specialCaseTags )) {
@@ -113,6 +187,9 @@ public function addSpecialCaseTag($tag)
113187 }
114188 }
115189
190+ /**
191+ * @param string $tag
192+ */
116193 public function removeSpecialCaseTag ($ tag )
117194 {
118195 if (($ key = array_search ($ tag , $ this ->specialCaseTags )) !== false ) {
@@ -130,46 +207,85 @@ public function removeSpecialCaseTag($tag)
130207 }
131208 }
132209
210+ /**
211+ * @return array|null
212+ */
133213 public function getSpecialCaseTags ()
134214 {
135215 return $ this ->specialCaseTags ;
136216 }
137217
218+ /**
219+ * @return string
220+ */
138221 public function getOldHtml ()
139222 {
140223 return $ this ->oldText ;
141224 }
142225
226+ /**
227+ * @return string
228+ */
143229 public function getNewHtml ()
144230 {
145231 return $ this ->newText ;
146232 }
147233
234+ /**
235+ * @return string
236+ */
148237 public function getDifference ()
149238 {
150239 return $ this ->content ;
151240 }
152241
242+ /**
243+ * @param bool $boolean
244+ *
245+ * @return $this
246+ */
153247 public function setGroupDiffs ($ boolean )
154248 {
155249 $ this ->groupDiffs = $ boolean ;
250+
251+ return $ this ;
156252 }
157253
254+ /**
255+ * @return bool
256+ */
158257 public function isGroupDiffs ()
159258 {
160259 return $ this ->groupDiffs ;
161260 }
162261
262+ /**
263+ * @param string $tag
264+ *
265+ * @return string
266+ */
163267 protected function getOpeningTag ($ tag )
164268 {
165269 return "/< " .$ tag ."[^>]*/i " ;
166270 }
167271
272+ /**
273+ * @param string $tag
274+ *
275+ * @return string
276+ */
168277 protected function getClosingTag ($ tag )
169278 {
170279 return "</ " .$ tag ."> " ;
171280 }
172281
282+ /**
283+ * @param string $str
284+ * @param string $start
285+ * @param string $end
286+ *
287+ * @return string
288+ */
173289 protected function getStringBetween ($ str , $ start , $ end )
174290 {
175291 $ expStr = explode ( $ start , $ str , 2 );
@@ -185,7 +301,12 @@ protected function getStringBetween($str, $start, $end)
185301 return '' ;
186302 }
187303
188- protected function purifyHtml ($ html , $ tags = null )
304+ /**
305+ * @param string $html
306+ *
307+ * @return string
308+ */
309+ protected function purifyHtml ($ html )
189310 {
190311 if ( class_exists ( 'Tidy ' ) && false ) {
191312 $ config = array ( 'output-xhtml ' => true , 'indent ' => false );
@@ -205,11 +326,21 @@ protected function splitInputsToWords()
205326 $ this ->newWords = $ this ->convertHtmlToListOfWords ( $ this ->explode ( $ this ->newText ) );
206327 }
207328
329+ /**
330+ * @param string $text
331+ *
332+ * @return bool
333+ */
208334 protected function isPartOfWord ($ text )
209335 {
210336 return ctype_alnum (str_replace ($ this ->specialCaseChars , '' , $ text ));
211337 }
212338
339+ /**
340+ * @param array $characterString
341+ *
342+ * @return array
343+ */
213344 protected function convertHtmlToListOfWords ($ characterString )
214345 {
215346 $ mode = 'character ' ;
@@ -286,21 +417,41 @@ protected function convertHtmlToListOfWords($characterString)
286417 return $ words ;
287418 }
288419
420+ /**
421+ * @param string $val
422+ *
423+ * @return bool
424+ */
289425 protected function isStartOfTag ($ val )
290426 {
291427 return $ val == "< " ;
292428 }
293429
430+ /**
431+ * @param string $val
432+ *
433+ * @return bool
434+ */
294435 protected function isEndOfTag ($ val )
295436 {
296437 return $ val == "> " ;
297438 }
298439
440+ /**
441+ * @param string $value
442+ *
443+ * @return bool
444+ */
299445 protected function isWhiteSpace ($ value )
300446 {
301447 return !preg_match ( '[^\s] ' , $ value );
302448 }
303449
450+ /**
451+ * @param string $value
452+ *
453+ * @return array
454+ */
304455 protected function explode ($ value )
305456 {
306457 // as suggested by @onassar
0 commit comments