| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jfcherng\Diff\Renderer\Html\LineRenderer; |
| 6 |
|
| 7 |
use Jfcherng\Diff\SequenceMatcher; |
| 8 |
|
| 9 |
/** |
| 10 |
* Base renderer for rendering HTML-based line diffs. |
| 11 |
* |
| 12 |
* @todo use typed properties (BC breaking for public interface) in v7 |
| 13 |
*/ |
| 14 |
abstract class AbstractLineRenderer implements LineRendererInterface |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var SequenceMatcher the sequence matcher |
| 18 |
*/ |
| 19 |
protected $sequenceMatcher; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var array the differ options |
| 23 |
*/ |
| 24 |
protected $differOptions = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array the renderer options |
| 28 |
*/ |
| 29 |
protected $rendererOptions = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* The constructor. |
| 33 |
* |
| 34 |
* @param array $differOptions the differ options |
| 35 |
* @param array $rendererOptions the renderer options |
| 36 |
*/ |
| 37 |
public function __construct(array $differOptions, array $rendererOptions) |
| 38 |
{ |
| 39 |
$this->sequenceMatcher = new SequenceMatcher([], []); |
| 40 |
|
| 41 |
$this |
| 42 |
->setDifferOptions($differOptions) |
| 43 |
->setRendererOptions($rendererOptions) |
| 44 |
; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Set the differ options. |
| 49 |
* |
| 50 |
* @param array $differOptions the differ options |
| 51 |
* |
| 52 |
* @return static |
| 53 |
*/ |
| 54 |
public function setDifferOptions(array $differOptions): self |
| 55 |
{ |
| 56 |
$this->differOptions = $differOptions; |
| 57 |
$this->sequenceMatcher->setOptions($differOptions); |
| 58 |
|
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Set the renderer options. |
| 64 |
* |
| 65 |
* @param array $rendererOptions the renderer options |
| 66 |
* |
| 67 |
* @return static |
| 68 |
*/ |
| 69 |
public function setRendererOptions(array $rendererOptions): self |
| 70 |
{ |
| 71 |
$this->rendererOptions = $rendererOptions; |
| 72 |
|
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets the differ options. |
| 78 |
* |
| 79 |
* @return array the differ options |
| 80 |
*/ |
| 81 |
public function getDifferOptions(): array |
| 82 |
{ |
| 83 |
return $this->differOptions; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Gets the renderer options. |
| 88 |
* |
| 89 |
* @return array the renderer options |
| 90 |
*/ |
| 91 |
public function getRendererOptions(): array |
| 92 |
{ |
| 93 |
return $this->rendererOptions; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get the changed extent segments. |
| 98 |
* |
| 99 |
* @param string[] $old the old array |
| 100 |
* @param string[] $new the new array |
| 101 |
* |
| 102 |
* @return int[][] the changed extent segments |
| 103 |
*/ |
| 104 |
protected function getChangedExtentSegments(array $old, array $new): array |
| 105 |
{ |
| 106 |
return $this->sequenceMatcher->setSequences($old, $new)->getOpcodes(); |
| 107 |
} |
| 108 |
} |