Skip to content

Commit 208cad2

Browse files
committed
Merge pull request #3 from caxy/feature-add_new_configuration
Added ability to configure specialCaseChars and groupDiffs, and cleaned ...
2 parents 0afe389 + 12dd11b commit 208cad2

3 files changed

Lines changed: 77 additions & 13 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function getConfigTreeBuilder()
2626
->prototype('scalar')->end()
2727
->end()
2828
->scalarNode('encoding')->end()
29+
->arrayNode('special_case_chars')
30+
->prototype('scalar')->end()
31+
->end()
32+
->booleanNode('group_diffs')->end()
2933
->end();
3034

3135
return $treeBuilder;

Service/HtmlDiffService.php

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,69 @@
77

88
class 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
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"require": {
1616
"php": ">=5.3.3",
1717
"symfony/framework-bundle": "~2.1",
18-
"caxy/php-htmldiff": "*"
18+
"caxy/php-htmldiff": ">=0.0.1"
1919
},
2020
"target-dir": "Caxy/HtmlDiffBundle",
2121
"autoload": {

0 commit comments

Comments
 (0)