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