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;
33 protected $encoding = 'UTF-8';
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]]',
56 protected $matchThreshold = 80;
60 protected $specialCaseOpeningTags = array();
64 protected $specialCaseClosingTags = array();
69 protected $useTableDiffing = true;
72 * @var null|\Doctrine\Common\Cache\Cache
74 protected $cacheProvider;
79 protected $purifierCacheLocation = null;
82 * @return HtmlDiffConfig
84 public static function create()
90 * HtmlDiffConfig constructor.
92 public function __construct()
94 $this->setSpecialCaseTags($this->specialCaseTags);
100 public function getMatchThreshold()
102 return $this->matchThreshold;
106 * @param int $matchThreshold
108 * @return AbstractDiff
110 public function setMatchThreshold($matchThreshold)
112 $this->matchThreshold = $matchThreshold;
118 * @param array $chars
120 public function setSpecialCaseChars(array $chars)
122 $this->specialCaseChars = $chars;
128 public function getSpecialCaseChars()
130 return $this->specialCaseChars;
134 * @param string $char
138 public function addSpecialCaseChar($char)
140 if (!in_array($char, $this->specialCaseChars)) {
141 $this->specialCaseChars[] = $char;
148 * @param string $char
152 public function removeSpecialCaseChar($char)
154 $key = array_search($char, $this->specialCaseChars);
155 if ($key !== false) {
156 unset($this->specialCaseChars[$key]);
167 public function setSpecialCaseTags(array $tags = array())
169 $this->specialCaseTags = $tags;
170 $this->specialCaseOpeningTags = array();
171 $this->specialCaseClosingTags = array();
173 foreach ($this->specialCaseTags as $tag) {
174 $this->addSpecialCaseTag($tag);
185 public function addSpecialCaseTag($tag)
187 if (!in_array($tag, $this->specialCaseTags)) {
188 $this->specialCaseTags[] = $tag;
191 $opening = $this->getOpeningTag($tag);
192 $closing = $this->getClosingTag($tag);
194 if (!in_array($opening, $this->specialCaseOpeningTags)) {
195 $this->specialCaseOpeningTags[] = $opening;
197 if (!in_array($closing, $this->specialCaseClosingTags)) {
198 $this->specialCaseClosingTags[] = $closing;
209 public function removeSpecialCaseTag($tag)
211 if (($key = array_search($tag, $this->specialCaseTags)) !== false) {
212 unset($this->specialCaseTags[$key]);
214 $opening = $this->getOpeningTag($tag);
215 $closing = $this->getClosingTag($tag);
217 if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) {
218 unset($this->specialCaseOpeningTags[$key]);
220 if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) {
221 unset($this->specialCaseClosingTags[$key]);
231 public function getSpecialCaseTags()
233 return $this->specialCaseTags;
239 public function isGroupDiffs()
241 return $this->groupDiffs;
245 * @param bool $groupDiffs
247 * @return HtmlDiffConfig
249 public function setGroupDiffs($groupDiffs)
251 $this->groupDiffs = $groupDiffs;
259 public function getEncoding()
261 return $this->encoding;
265 * @param string $encoding
267 * @return HtmlDiffConfig
269 public function setEncoding($encoding)
271 $this->encoding = $encoding;
279 public function isInsertSpaceInReplace()
281 return $this->insertSpaceInReplace;
285 * @param bool $insertSpaceInReplace
287 * @return HtmlDiffConfig
289 public function setInsertSpaceInReplace($insertSpaceInReplace)
291 $this->insertSpaceInReplace = $insertSpaceInReplace;
299 public function getIsolatedDiffTags()
301 return $this->isolatedDiffTags;
305 * @param array $isolatedDiffTags
307 * @return HtmlDiffConfig
309 public function setIsolatedDiffTags($isolatedDiffTags)
311 $this->isolatedDiffTags = $isolatedDiffTags;
318 * @param null|string $placeholder
322 public function addIsolatedDiffTag($tag, $placeholder = null)
324 if (null === $placeholder) {
325 $placeholder = sprintf('[[REPLACE_%s]]', strtoupper($tag));
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)
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)
341 if (!array_key_exists($tag, $this->isolatedDiffTags)) {
342 $this->isolatedDiffTags[$tag] = $placeholder;
353 public function removeIsolatedDiffTag($tag)
355 if ($this->isIsolatedDiffTag($tag)) {
356 unset($this->isolatedDiffTags[$tag]);
367 public function isIsolatedDiffTag($tag)
369 return array_key_exists($tag, $this->isolatedDiffTags);
373 * @param string $text
377 public function isIsolatedDiffTagPlaceholder($text)
379 return in_array($text, $this->isolatedDiffTags, true);
385 * @return null|string
387 public function getIsolatedDiffTagPlaceholder($tag)
389 return $this->isIsolatedDiffTag($tag) ? $this->isolatedDiffTags[$tag] : null;
395 public function getSpecialCaseOpeningTags()
397 return $this->specialCaseOpeningTags;
403 public function getSpecialCaseClosingTags()
405 return $this->specialCaseClosingTags;
411 public function isUseTableDiffing()
413 return $this->useTableDiffing;
417 * @param bool $useTableDiffing
419 * @return HtmlDiffConfig
421 public function setUseTableDiffing($useTableDiffing)
423 $this->useTableDiffing = $useTableDiffing;
429 * @param null|\Doctrine\Common\Cache\Cache $cacheProvider
433 public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null)
435 $this->cacheProvider = $cacheProvider;
441 * @return null|\Doctrine\Common\Cache\Cache
443 public function getCacheProvider()
445 return $this->cacheProvider;
453 public function setPurifierCacheLocation($purifierCacheLocation = null)
455 $this->purifierCacheLocation = $purifierCacheLocation;
461 * @return null|string
463 public function getPurifierCacheLocation()
465 return $this->purifierCacheLocation;
473 protected function getOpeningTag($tag)
475 return '/<'.$tag.'[^>]*/i';
483 protected function getClosingTag($tag)
485 return '</'.$tag.'>';