@@ -14,9 +14,12 @@ If you only need this library during development, for instance to run your proje
1414
1515### Usage
1616
17+ #### Generating diff
18+
1719The ` Differ ` class can be used to generate a textual representation of the difference between two strings:
1820
1921``` php
22+ <?php
2023use SebastianBergmann\Diff\Differ;
2124
2225$differ = new Differ;
@@ -31,6 +34,76 @@ The code above yields the output below:
3134 -foo
3235 +bar
3336
37+ There are three output builders available in this package:
38+
39+ #### UnifiedDiffOutputBuilder
40+
41+ This is default builder, which generates the output close to udiff and is used by PHPUnit.
42+
43+ ``` php
44+ <?php
45+
46+ use SebastianBergmann\Diff\Differ;
47+ use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
48+
49+ $builder = new UnifiedDiffOutputBuilder(
50+ "--- Original\n+++ New\n", // custom header
51+ false // do not add line numbers to the diff
52+ );
53+
54+ $differ = new Differ($builder);
55+ print $differ->diff('foo', 'bar');
56+ ```
57+
58+ #### StrictUnifiedDiffOutputBuilder
59+
60+ Generates (strict) Unified diff's (unidiffs) with hunks,
61+ similar to ` diff -u ` and compatible with ` patch ` and ` git apply ` .
62+
63+ ``` php
64+ <?php
65+
66+ use SebastianBergmann\Diff\Differ;
67+ use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
68+
69+ $builder = new StrictUnifiedDiffOutputBuilder([
70+ 'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1`
71+ 'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed)
72+ 'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3
73+ 'fromFile' => null,
74+ 'fromFileDate' => null,
75+ 'toFile' => null,
76+ 'toFileDate' => null,
77+ ]);
78+
79+ $differ = new Differ($builder);
80+ print $differ->diff('foo', 'bar');
81+ ```
82+
83+ #### DiffOnlyOutputBuilder
84+
85+ Output only the lines that differ.
86+
87+ ``` php
88+ <?php
89+
90+ use SebastianBergmann\Diff\Differ;
91+ use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder;
92+
93+ $builder = new DiffOnlyOutputBuilder(
94+ "--- Original\n+++ New\n"
95+ );
96+
97+ $differ = new Differ($builder);
98+ print $differ->diff('foo', 'bar');
99+ ```
100+
101+ #### DiffOutputBuilderInterface
102+
103+ You can pass any output builder to the ` Differ ` class as longs as it implements the ` DiffOutputBuilderInterface ` .
104+
105+ #### Parsing diff
106+
34107The ` Parser ` class can be used to parse a unified diff into an object graph:
35108
36109``` php
@@ -114,13 +187,8 @@ The code above yields the output below:
114187 [type:SebastianBergmann\Diff\Line:private] => 3
115188 [content:SebastianBergmann\Diff\Line:private] => $b = new Money(2, new Currency('EUR'));
116189 )
117-
118190 )
119-
120191 )
121-
122192 )
123-
124193 )
125-
126194 )
0 commit comments