@@ -58,6 +58,8 @@ include __DIR__ . '/vendor/autoload.php';
5858use Jfcherng\Diff\Differ;
5959use Jfcherng\Diff\DiffHelper;
6060use Jfcherng\Diff\Factory\RendererFactory;
61+ use Jfcherng\Diff\Options\DifferOptions;
62+ use Jfcherng\Diff\Options\RendererOptions;
6163use Jfcherng\Diff\Renderer\RendererConstant;
6264
6365$oldFile = __DIR__ . '/example/old_file.txt';
@@ -71,69 +73,62 @@ $new = 'And this is the new one.';
7173// HTML renderers: Combined, Inline, JsonHtml, SideBySide
7274$rendererName = 'Unified';
7375
74- // the Diff class options
75- $differOptions = [
76- // show how many neighbor lines
77- // Differ::CONTEXT_ALL can be used to show the whole file
78- 'context' => 3,
76+ // the Differ options
77+ $differOptions = new DifferOptions(
78+ // show how many neighbor lines; Differ::CONTEXT_ALL shows the whole file
79+ context: 3,
7980 // ignore case difference
80- ' ignoreCase' => false,
81+ ignoreCase: false,
8182 // ignore line ending difference
82- ' ignoreLineEnding' => false,
83+ ignoreLineEnding: false,
8384 // ignore whitespace difference
84- ' ignoreWhitespace' => false,
85- // if the input sequence is too long, it will just gives up (especially for char-level diff)
86- ' lengthLimit' => 2000,
87- // if truthy, when inputs are identical, the whole inputs will be rendered in the output
88- ' fullContextIfIdentical' => false,
89- ] ;
90-
91- // the renderer class options
92- $rendererOptions = [
85+ ignoreWhitespace: false,
86+ // if the input sequence is too long, give up (especially for char-level diff)
87+ lengthLimit: 2000,
88+ // when inputs are identical, render the whole content rather than an empty result
89+ fullContextIfIdentical: false,
90+ ) ;
91+
92+ // the renderer options
93+ $rendererOptions = new RendererOptions(
9394 // how detailed the rendered HTML in-line diff is? (none, line, word, char)
94- ' detailLevel' => 'line',
95+ detailLevel: 'line',
9596 // renderer language: eng, cht, chs, jpn, ...
9697 // or an array which has the same keys with a language file
9798 // check the "Custom Language" section in the readme for more advanced usage
98- ' language' => 'eng',
99+ language: 'eng',
99100 // show line numbers in HTML renderers
100- ' lineNumbers' => true,
101+ lineNumbers: true,
101102 // show a separator between different diff hunks in HTML renderers
102- ' separateBlock' => true,
103+ separateBlock: true,
103104 // show the (table) header
104- 'showHeader' => true,
105- // the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
106- // but if you want to visualize them in the backend with "  ; ", you can set this to true
107- 'spacesToNbsp' => false,
105+ showHeader: true,
106+ // render spaces/tabs as <span class =" ch sp" > </span > tags (visualised via CSS)
107+ spaceToHtmlTag: false,
108+ // convert consecutive spaces to   ; in HTML output
109+ spacesToNbsp: false,
108110 // HTML renderer tab width (negative = do not convert into spaces)
109- 'tabSize' => 4,
110- // this option is currently only for the Combined renderer.
111- // it determines whether a replace-type block should be merged or not
112- // depending on the content changed ratio, which values between 0 and 1.
113- 'mergeThreshold' => 0.8,
114- // this option is currently only for the Unified and the Context renderers.
115- // RendererConstant::CLI_COLOR_AUTO = colorize the output if possible (default)
116- // RendererConstant::CLI_COLOR_ENABLE = force to colorize the output
117- // RendererConstant::CLI_COLOR_DISABLE = force not to colorize the output
118- 'cliColorization' => RendererConstant::CLI_COLOR_AUTO,
119- // this option is currently only for the Json renderer.
120- // internally, ops (tags) are all int type but this is not good for human reading.
121- // set this to "true" to convert them into string form before outputting.
122- 'outputTagAsString' => false,
123- // this option is currently only for the Json renderer.
124- // it controls how the output JSON is formatted.
125- // see available options on https://www.php.net/manual/en/function.json-encode.php
126- 'jsonEncodeFlags' => \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,
127- // this option is currently effective when the "detailLevel" is "word"
128- // characters listed in this array can be used to make diff segments into a whole
129- // for example, making "<del >good</del >-<del >looking</del >" into "<del >good-looking</del >"
130- // this should bring better readability but set this to empty array if you do not want it
131- 'wordGlues' => [' ', '-'],
132- // change this value to a string as the returned diff if the two input strings are identical
133- 'resultForIdenticals' => null,
134- // extra HTML classes added to the DOM of the diff container
135- 'wrapperClasses' => ['diff-wrapper'],
136- ];
111+ tabSize: 4,
112+ // Combined renderer: merge replace-blocks whose changed ratio is at or below this threshold (0–1)
113+ mergeThreshold: 0.8,
114+ // Unified/Context renderers CLI colorization:
115+ // RendererConstant::CLI_COLOR_AUTO = colorize if possible (default)
116+ // RendererConstant::CLI_COLOR_ENABLE = force colorize
117+ // RendererConstant::CLI_COLOR_DISABLE = force no color
118+ cliColorization: RendererConstant::CLI_COLOR_AUTO,
119+ // JSON renderer: emit op tags as human-readable strings instead of ints
120+ outputTagAsString: false,
121+ // JSON renderer: flags passed to json_encode()
122+ // see https://www.php.net/manual/en/function.json-encode.php
123+ jsonEncodeFlags: \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,
124+ // word-level diff: adjacent segments joined by these characters are merged into one
125+ // e.g. "<del >good</del >-<del >looking</del >" → "<del >good-looking</del >"
126+ wordGlues: ['-', ' '],
127+ // return this string verbatim when the two inputs are identical; null = renderer default
128+ resultForIdenticals: null,
129+ // extra CSS classes added to the diff container <div > in HTML renderers
130+ wrapperClasses: ['diff-wrapper'],
131+ );
137132
138133// one-line simply compare two files
139134$result = DiffHelper::calculateFiles($oldFile, $newFile, $rendererName, $differOptions, $rendererOptions);
0 commit comments