Skip to content

Commit 2fa5c42

Browse files
authored
Merge pull request #14 from jfcherng/add-renderer-option-resultForIdenticals
Add renderer option: resultForIdenticals
2 parents 53ac441 + 9a71712 commit 2fa5c42

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ $rendererOptions = [
9292
// internally, ops (tags) are all int type but this is not good for human reading.
9393
// set this to "true" to convert them into string form before outputting.
9494
'outputTagAsString' => true,
95+
// change this value to a non-null one as the the returned diff
96+
// if the two input strings are identical
97+
'resultForIdenticals' => null,
9598
// extra HTML classes added to the DOM of the diff container
9699
'wrapperClasses' => ['diff-wrapper'],
97100
];

example/demo.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
// internally, ops (tags) are all int type but this is not good for human reading.
4949
// set this to "true" to convert them into string form before outputting.
5050
'outputTagAsString' => false,
51+
// change this value to a non-null one as the the returned diff
52+
// if the two input strings are identical
53+
'resultForIdenticals' => null,
5154
// extra HTML classes added to the DOM of the diff container
5255
'wrapperClasses' => ['diff-wrapper'],
5356
];

src/Renderer/AbstractRenderer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ abstract class AbstractRenderer implements RendererInterface
4949
// internally, ops (tags) are all int type but this is not good for human reading.
5050
// set this to "true" to convert them into string form before outputting.
5151
'outputTagAsString' => false,
52+
// change this value to a non-null one as the the returned diff
53+
// if the two input strings are identical
54+
'resultForIdenticals' => null,
5255
// extra HTML classes added to the DOM of the diff container
5356
'wrapperClasses' => ['diff-wrapper'],
5457
];
@@ -104,12 +107,23 @@ public function getOptions(): array
104107

105108
/**
106109
* {@inheritdoc}
110+
*
111+
* @final
112+
*
113+
* @todo mark this method with "final" in the next major release
107114
*/
108115
public function getResultForIdenticals(): string
109116
{
110-
return '';
117+
return $this->options['resultForIdenticals'] ?? $this->getResultForIdenticalsDefault();
111118
}
112119

120+
/**
121+
* Get the renderer default result when the old and the new are the same.
122+
*
123+
* @return string
124+
*/
125+
abstract public function getResultForIdenticalsDefault(): string;
126+
113127
/**
114128
* {@inheritdoc}
115129
*/

src/Renderer/Html/AbstractHtml.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ abstract class AbstractHtml extends AbstractRenderer
3232
SequenceMatcher::OP_REP => 'rep',
3333
];
3434

35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function getResultForIdenticalsDefault(): string
39+
{
40+
return '';
41+
}
42+
3543
/**
3644
* Render and return an array structure suitable for generating HTML
3745
* based differences. Generally called by subclasses that generate a

src/Renderer/Html/Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class Json extends AbstractHtml
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function getResultForIdenticals(): string
31+
public function getResultForIdenticalsDefault(): string
3232
{
3333
return '[]';
3434
}

src/Renderer/Text/AbstractText.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ abstract class AbstractText extends AbstractRenderer
1515
* @var bool is this renderer pure text?
1616
*/
1717
const IS_TEXT_RENDERER = true;
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function getResultForIdenticalsDefault(): string
23+
{
24+
return '';
25+
}
1826
}

tests/Renderer/RendererTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,28 @@ public function testSetOptionsWithLanguageArray(): void
4444
'Rederer options: "language" array should work.'
4545
);
4646
}
47+
48+
/**
49+
* Test the AbstractRenderer::setOptions with result for identicals.
50+
*
51+
* @covers \Jfcherng\Diff\Renderer\AbstractRenderer::setOptions
52+
*/
53+
public function testSetOptionsWithResultForIdenticals(): void
54+
{
55+
$testMarker = '_TEST_MARKER_';
56+
57+
$diffResult = DiffHelper::calculate(
58+
'we are the same',
59+
'we are the same',
60+
'Inline',
61+
[],
62+
['resultForIdenticals' => $testMarker]
63+
);
64+
65+
static::assertSame(
66+
$testMarker,
67+
$diffResult,
68+
'Rederer options: result for identicals should work.'
69+
);
70+
}
4771
}

0 commit comments

Comments
 (0)