| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jfcherng\Diff\Renderer\Html; |
| 6 |
|
| 7 |
use Jfcherng\Diff\SequenceMatcher; |
| 8 |
|
| 9 |
/** |
| 10 |
* HTML Json diff generator. |
| 11 |
*/ |
| 12 |
class JsonHtml extends AbstractHtml |
| 13 |
{ |
| 14 |
/** |
| 15 |
* {@inheritdoc} |
| 16 |
*/ |
| 17 |
public const INFO = [ |
| 18 |
'desc' => 'HTML Json', |
| 19 |
'type' => 'Html', |
| 20 |
]; |
| 21 |
|
| 22 |
/** |
| 23 |
* {@inheritdoc} |
| 24 |
*/ |
| 25 |
public const IS_TEXT_RENDERER = true; |
| 26 |
|
| 27 |
public function getResultForIdenticalsDefault(): string |
| 28 |
{ |
| 29 |
return '[]'; |
| 30 |
} |
| 31 |
|
| 32 |
protected function redererChanges(array $changes): string |
| 33 |
{ |
| 34 |
if ($this->options['outputTagAsString']) { |
| 35 |
$this->convertTagToString($changes); |
| 36 |
} |
| 37 |
|
| 38 |
return json_encode($changes, $this->options['jsonEncodeFlags']); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Convert tags of changes to their string form for better readability. |
| 43 |
* |
| 44 |
* @param array[][] $changes the changes |
| 45 |
*/ |
| 46 |
protected function convertTagToString(array &$changes): void |
| 47 |
{ |
| 48 |
foreach ($changes as &$hunks) { |
| 49 |
foreach ($hunks as &$block) { |
| 50 |
$block['tag'] = SequenceMatcher::opIntToStr($block['tag']); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
protected function formatStringFromLines(string $string): string |
| 56 |
{ |
| 57 |
return $this->htmlSafe($string); |
| 58 |
} |
| 59 |
} |