77
88class HtmlDiffService
99{
10+ const PARAMETER_SPECIAL_CASE_TAGS = 'special_case_tags ' ;
11+ const PARAMETER_SPECIAL_CASE_CHARS = 'special_case_chars ' ;
12+ const PARAMETER_ENCODING = 'encoding ' ;
13+ const PARAMETER_GROUP_DIFFS = 'group_diffs ' ;
14+
1015 protected $ container ;
1116
12- protected $ specialCaseTags = array ('strong ' , 'b ' , 'i ' , 'big ' , 'small ' , 'u ' , 'sub ' , 'sup ' , 'strike ' , 's ' , 'p ' );
17+ protected $ specialCaseTags = HtmlDiff::defaultSpecialCaseTags;
18+
19+ protected $ specialCaseChars = HtmlDiff::defaultSpecialCaseChars;
20+
21+ protected $ groupDiffs = HtmlDiff::defaultGroupDiffs;
1322
1423 protected $ encoding = 'UTF-8 ' ;
1524
1625 public function __construct (ContainerInterface $ container )
1726 {
1827 $ this ->container = $ container ;
19-
20- $ encoding = $ this ->container ->getParameter ('caxy_html_diff.encoding ' );
21- if (!empty ($ encoding )) {
22- $ this ->encoding = $ encoding ;
23- }
24-
25- $ specialCaseTags = $ this ->container ->getParameter ('caxy_html_diff.special_case_tags ' );
26- if (!empty ($ specialCaseTags )) {
27- $ this ->specialCaseTags = $ specialCaseTags ;
28+
29+ $ this ->loadParameter (self ::PARAMETER_ENCODING , $ this ->encoding );
30+ $ this ->loadParameter (self ::PARAMETER_SPECIAL_CASE_TAGS , $ this ->specialCaseTags );
31+ $ this ->loadParameter (self ::PARAMETER_SPECIAL_CASE_CHARS , $ this ->specialCaseChars );
32+ $ this ->loadParameter (self ::PARAMETER_GROUP_DIFFS , $ this ->groupDiffs );
33+ }
34+
35+ public function loadParameter ($ param , &$ property )
36+ {
37+ $ param = 'caxy_html_diff. ' . $ param ;
38+ if ($ this ->container ->hasParameter ($ param )) {
39+ $ property = $ this ->container ->getParameter ($ param );
2840 }
2941 }
3042
31- public function diff ($ oldText , $ newText )
43+ public function diff ($ oldText , $ newText, $ groupDiffs = null , $ specialCaseChars = null )
3244 {
33- $ diff = new HtmlDiff ($ oldText , $ newText , $ this ->encoding , $ this ->specialCaseTags );
45+ if ($ groupDiffs === null ) {
46+ $ groupDiffs = $ this ->groupDiffs ;
47+ }
48+
49+ if ($ specialCaseChars === null ) {
50+ $ specialCaseChars = $ this ->specialCaseChars ;
51+ }
52+
53+ $ diff = new HtmlDiff ($ oldText , $ newText , $ this ->encoding , $ this ->specialCaseTags , $ groupDiffs );
54+
55+ if ($ specialCaseChars !== null ) {
56+ $ diff ->setSpecialCaseChars ($ specialCaseChars );
57+ }
3458
3559 return $ diff ->build ();
3660 }
61+
62+ public function getGroupDiffs ()
63+ {
64+ return $ this ->groupDiffs ;
65+ }
66+
67+ public function setGroupDiffs ($ boolean )
68+ {
69+ $ this ->groupDiffs = $ boolean ;
70+
71+ return $ this ;
72+ }
3773
3874 public function getEncoding ()
3975 {
@@ -70,4 +106,28 @@ public function removeSpecialCaseTag($tag)
70106 return $ this ;
71107 }
72108
109+ public function setSpecialCaseChars (array $ chars )
110+ {
111+ $ this ->specialCaseChars = $ chars ;
112+ }
113+
114+ public function getSpecialCaseChars ()
115+ {
116+ return $ this ->specialCaseChars ;
117+ }
118+
119+ public function addSpecialCaseChar ($ char )
120+ {
121+ if (!in_array ($ char , $ this ->specialCaseChars )) {
122+ $ this ->specialCaseChars [] = $ char ;
123+ }
124+ }
125+
126+ public function removeSpecialCaseChar ($ char )
127+ {
128+ $ key = array_search ($ char , $ this ->specialCaseChars );
129+ if ($ key !== false ) {
130+ unset($ this ->specialCaseChars [$ key ]);
131+ }
132+ }
73133}
0 commit comments