3 namespace Caxy\HtmlDiff;
6 * Class HtmlDiffConfig.
13 protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
18 protected $specialCaseChars = array('.', ',', '(', ')', '\'');
23 protected $groupDiffs = true;
28 protected $insertSpaceInReplace = false;
31 * Whether to keep newlines in the diff
34 protected $keepNewLines = false;
39 protected $encoding = 'UTF-8';
44 protected $isolatedDiffTags = array(
45 'ol' => '[[REPLACE_ORDERED_LIST]]',
46 'ul' => '[[REPLACE_UNORDERED_LIST]]',
47 'sub' => '[[REPLACE_SUB_SCRIPT]]',
48 'sup' => '[[REPLACE_SUPER_SCRIPT]]',
49 'dl' => '[[REPLACE_DEFINITION_LIST]]',
50 'table' => '[[REPLACE_TABLE]]',
51 'strong' => '[[REPLACE_STRONG]]',
52 'b' => '[[REPLACE_STRONG]]',
53 'em' => '[[REPLACE_EM]]',
54 'i' => '[[REPLACE_EM]]',
55 'a' => '[[REPLACE_A]]',
56 'img' => '[[REPLACE_IMG]]',
57 'pre' => '[[REPLACE_PRE]]',
63 protected $matchThreshold = 80;
67 protected $specialCaseOpeningTags = array();
71 protected $specialCaseClosingTags = array();
76 protected $useTableDiffing = true;
79 * @var null|\Doctrine\Common\Cache\Cache
81 protected $cacheProvider;
86 protected $purifierCacheLocation = null;
89 * @return HtmlDiffConfig
91 public static function create()
97 * HtmlDiffConfig constructor.
99 public function __construct()
101 $this->setSpecialCaseTags($this->specialCaseTags);
107 public function getMatchThreshold()
109 return $this->matchThreshold;
113 * @param int $matchThreshold
115 * @return AbstractDiff
117 public function setMatchThreshold($matchThreshold)
119 $this->matchThreshold = $matchThreshold;
125 * @param array $chars
127 public function setSpecialCaseChars(array $chars)
129 $this->specialCaseChars = $chars;
135 public function getSpecialCaseChars()
137 return $this->specialCaseChars;
141 * @param string $char
145 public function addSpecialCaseChar($char)
147 if (!in_array($char, $this->specialCaseChars)) {
148 $this->specialCaseChars[] = $char;
155 * @param string $char
159 public function removeSpecialCaseChar($char)
161 $key = array_search($char, $this->specialCaseChars);
162 if ($key !== false) {
163 unset($this->specialCaseChars[$key]);
174 public function setSpecialCaseTags(array $tags = array())
176 $this->specialCaseTags = $tags;
177 $this->specialCaseOpeningTags = array();
178 $this->specialCaseClosingTags = array();
180 foreach ($this->specialCaseTags as $tag) {
181 $this->addSpecialCaseTag($tag);
192 public function addSpecialCaseTag($tag)
194 if (!in_array($tag, $this->specialCaseTags)) {
195 $this->specialCaseTags[] = $tag;
198 $opening = $this->getOpeningTag($tag);
199 $closing = $this->getClosingTag($tag);
201 if (!in_array($opening, $this->specialCaseOpeningTags)) {
202 $this->specialCaseOpeningTags[] = $opening;
204 if (!in_array($closing, $this->specialCaseClosingTags)) {
205 $this->specialCaseClosingTags[] = $closing;
216 public function removeSpecialCaseTag($tag)
218 if (($key = array_search($tag, $this->specialCaseTags)) !== false) {
219 unset($this->specialCaseTags[$key]);
221 $opening = $this->getOpeningTag($tag);
222 $closing = $this->getClosingTag($tag);
224 if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) {
225 unset($this->specialCaseOpeningTags[$key]);
227 if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) {
228 unset($this->specialCaseClosingTags[$key]);
238 public function getSpecialCaseTags()
240 return $this->specialCaseTags;
246 public function isGroupDiffs()
248 return $this->groupDiffs;
252 * @param bool $groupDiffs
254 * @return HtmlDiffConfig
256 public function setGroupDiffs($groupDiffs)
258 $this->groupDiffs = $groupDiffs;
266 public function getEncoding()
268 return $this->encoding;
272 * @param string $encoding
274 * @return HtmlDiffConfig
276 public function setEncoding($encoding)
278 $this->encoding = $encoding;
286 public function isInsertSpaceInReplace()
288 return $this->insertSpaceInReplace;
292 * @param bool $insertSpaceInReplace
294 * @return HtmlDiffConfig
296 public function setInsertSpaceInReplace($insertSpaceInReplace)
298 $this->insertSpaceInReplace = $insertSpaceInReplace;
306 public function isKeepNewLines()
308 return $this->keepNewLines;
312 * @param bool $keepNewLines
314 public function setKeepNewLines($keepNewLines)
316 $this->keepNewLines = $keepNewLines;
322 public function getIsolatedDiffTags()
324 return $this->isolatedDiffTags;
328 * @param array $isolatedDiffTags
330 * @return HtmlDiffConfig
332 public function setIsolatedDiffTags($isolatedDiffTags)
334 $this->isolatedDiffTags = $isolatedDiffTags;
341 * @param null|string $placeholder
345 public function addIsolatedDiffTag($tag, $placeholder = null)
347 if (null === $placeholder) {
348 $placeholder = sprintf('[[REPLACE_%s]]', mb_strtoupper($tag));
351 if ($this->isIsolatedDiffTag($tag) && $this->isolatedDiffTags[$tag] !== $placeholder) {
352 throw new \InvalidArgumentException(
353 sprintf('Isolated diff tag "%s" already exists using a different placeholder', $tag)
357 $matchingKey = array_search($placeholder, $this->isolatedDiffTags, true);
358 if (false !== $matchingKey && $matchingKey !== $tag) {
359 throw new \InvalidArgumentException(
360 sprintf('Placeholder already being used for a different tag "%s"', $tag)
364 if (!array_key_exists($tag, $this->isolatedDiffTags)) {
365 $this->isolatedDiffTags[$tag] = $placeholder;
376 public function removeIsolatedDiffTag($tag)
378 if ($this->isIsolatedDiffTag($tag)) {
379 unset($this->isolatedDiffTags[$tag]);
390 public function isIsolatedDiffTag($tag)
392 return array_key_exists($tag, $this->isolatedDiffTags);
396 * @param string $text
400 public function isIsolatedDiffTagPlaceholder($text)
402 return in_array($text, $this->isolatedDiffTags, true);
408 * @return null|string
410 public function getIsolatedDiffTagPlaceholder($tag)
412 return $this->isIsolatedDiffTag($tag) ? $this->isolatedDiffTags[$tag] : null;
418 public function getSpecialCaseOpeningTags()
420 return $this->specialCaseOpeningTags;
426 public function getSpecialCaseClosingTags()
428 return $this->specialCaseClosingTags;
434 public function isUseTableDiffing()
436 return $this->useTableDiffing;
440 * @param bool $useTableDiffing
442 * @return HtmlDiffConfig
444 public function setUseTableDiffing($useTableDiffing)
446 $this->useTableDiffing = $useTableDiffing;
452 * @param null|\Doctrine\Common\Cache\Cache $cacheProvider
456 public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null)
458 $this->cacheProvider = $cacheProvider;
464 * @return null|\Doctrine\Common\Cache\Cache
466 public function getCacheProvider()
468 return $this->cacheProvider;
476 public function setPurifierCacheLocation($purifierCacheLocation = null)
478 $this->purifierCacheLocation = $purifierCacheLocation;
484 * @return null|string
486 public function getPurifierCacheLocation()
488 return $this->purifierCacheLocation;
496 protected function getOpeningTag($tag)
498 return '/<'.$tag.'[^>]*/i';
506 protected function getClosingTag($tag)
508 return '</'.$tag.'>';