@@ -14,9 +14,12 @@ If you only need this library during development, for instance to run your proje
14
14
15
15
### Usage
16
16
17
+ #### Generating diff
18
+
17
19
The ` Differ ` class can be used to generate a textual representation of the difference between two strings:
18
20
19
21
``` php
22
+ <?php
20
23
use SebastianBergmann\Diff\Differ;
21
24
22
25
$differ = new Differ;
@@ -31,6 +34,76 @@ The code above yields the output below:
31
34
-foo
32
35
+bar
33
36
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
+
34
107
The ` Parser ` class can be used to parse a unified diff into an object graph:
35
108
36
109
``` php
@@ -114,13 +187,8 @@ The code above yields the output below:
114
187
[type:SebastianBergmann\Diff\Line:private] => 3
115
188
[content:SebastianBergmann\Diff\Line:private] => $b = new Money(2, new Currency('EUR'));
116
189
)
117
-
118
190
)
119
-
120
191
)
121
-
122
192
)
123
-
124
193
)
125
-
126
194
)
0 commit comments