1<?php
2
3declare(strict_types=1);
4
5/*
6 * FINE granularity DIFF
7 *
8 * (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
9 * (c) 2013 Robert Crowe (http://cogpowered.com)
10 * (c) 2021 Christian Kuhn
11 *
12 * For the full copyright and license information, please view
13 * the LICENSE file that was distributed with this source code.
14 */
15
16namespace cogpowered\FineDiff;
17
18use cogpowered\FineDiff\Granularity\Character;
19use cogpowered\FineDiff\Granularity\GranularityInterface;
20use cogpowered\FineDiff\Parser\OpcodesInterface;
21use cogpowered\FineDiff\Parser\Parser;
22use cogpowered\FineDiff\Parser\ParserInterface;
23use cogpowered\FineDiff\Render\Html;
24use cogpowered\FineDiff\Render\RendererInterface;
25
26/**
27 * Diff class.
28 */
29class Diff
30{
31    /**
32     * @var GranularityInterface
33     */
34    protected $granularity;
35
36    /**
37     * @var RendererInterface
38     */
39    protected $renderer;
40
41    /**
42     * @var ParserInterface
43     */
44    protected $parser;
45
46    /**
47     * Instantiate a new instance of Diff.
48     *
49     * @param GranularityInterface|null $granularity Level of diff.
50     * @param RendererInterface|null $renderer Diff renderer.
51     * @param ParserInterface|null $parser Parser used to generate opcodes.
52     */
53    public function __construct(GranularityInterface $granularity = null, RendererInterface $renderer = null, ParserInterface $parser = null)
54    {
55        $this->granularity = $granularity ?? new Character();
56        $this->renderer = $renderer ?? new Html();
57        $this->parser = $parser ?? new Parser($this->granularity);
58    }
59
60    /**
61     * Returns the granularity object used by the parser.
62     */
63    public function getGranularity(): GranularityInterface
64    {
65        return $this->parser->getGranularity();
66    }
67
68    /**
69     * Set the granularity level of the parser.
70     */
71    public function setGranularity(GranularityInterface $granularity): void
72    {
73        $this->parser->setGranularity($granularity);
74    }
75
76    /**
77     * Get the render.
78     */
79    public function getRenderer(): RendererInterface
80    {
81        return $this->renderer;
82    }
83
84    /**
85     * Set the renderer.
86     */
87    public function setRenderer(RendererInterface $renderer): void
88    {
89        $this->renderer = $renderer;
90    }
91
92    /**
93     * Get the parser responsible for generating the diff/opcodes.
94     */
95    public function getParser(): ParserInterface
96    {
97        return $this->parser;
98    }
99
100    /**
101     * Set the parser.
102     */
103    public function setParser(ParserInterface $parser): void
104    {
105        $this->parser = $parser;
106    }
107
108    /**
109     * Gets the diff / opcodes between two strings.
110     *
111     * Returns the opcode diff which can be used for example
112     * to generate HTML report of the differences.
113     */
114    public function getOpcodes(string $from_text, string $to_text): OpcodesInterface
115    {
116        return $this->parser->parse($from_text, $to_text);
117    }
118
119    /**
120     * Render the difference between two strings.
121     * By default, will return the difference as HTML.
122     */
123    public function render(string $from_text, string $to_text): string
124    {
125        // First we need the opcodes
126        $opcodes = $this->getOpcodes($from_text, $to_text);
127        return $this->renderer->process($from_text, $opcodes);
128    }
129}
130