57
57
class Diff
58
58
{
59
59
/**
60
- * The original string
61
- *
62
60
* @var string
63
61
*/
64
- private $ from ;
65
-
66
- /**
67
- * The new string
68
- *
69
- * @var string
70
- */
71
- private $ to ;
72
-
73
- /**
74
- * The diff header
75
- *
76
- * @var string
77
- */
78
- private $ header = "--- Original \n+++ New \n" ;
79
-
80
- /**
81
- * Constructs a new diff for two given strings
82
- *
83
- * @param mixed $from The original string
84
- * @param mixed $to The new string
85
- * @param mixed $header The diff header
86
- */
87
- public function __construct ($ from , $ to , $ header = '' )
88
- {
89
- $ this ->from = $ from ;
90
- $ this ->to = $ to ;
91
-
92
- if ($ header ) {
93
- $ this ->header = $ header ;
94
- }
95
- }
62
+ private $ header ;
96
63
97
64
/**
98
- * Exports a value into a string.
65
+ * Constructor
99
66
*
100
- * @return string
101
- * @see PHP_Exporter\Diff::diff
67
+ * @param string $header
102
68
*/
103
- public function __toString ( )
69
+ public function __construct ( $ header = " --- Original \n +++ New \n" )
104
70
{
105
- return $ this ->diff () ;
71
+ $ this ->header = $ header ;
106
72
}
107
73
108
74
/**
109
- * Returns the diff between two strings as a string.
75
+ * Returns the diff between two arrays or strings as string.
110
76
*
77
+ * @param array|string $from
78
+ * @param array|string $to
111
79
* @return string
112
80
*/
113
- public function diff ()
81
+ public function diff ($ from , $ to )
114
82
{
115
- if ($ this ->from === $ this ->to ) {
116
- return '' ;
117
- }
118
-
119
83
$ buffer = $ this ->header ;
120
- $ diff = $ this ->toArray ( );
84
+ $ diff = $ this ->diffToArray ( $ from , $ to );
121
85
122
86
$ inOld = FALSE ;
123
87
$ i = 0 ;
@@ -179,7 +143,7 @@ public function diff()
179
143
}
180
144
181
145
/**
182
- * Returns the diff between two strings as an array.
146
+ * Returns the diff between two arrays or strings as array.
183
147
*
184
148
* every array-entry containts two elements:
185
149
* - [0] => string $token
@@ -189,16 +153,22 @@ public function diff()
189
153
* - 1: ADDED: $token was added to $from
190
154
* - 0: OLD: $token is not changed in $to
191
155
*
156
+ * @param array|string $from
157
+ * @param array|string $to
192
158
* @return array
193
159
*/
194
- public function toArray ( )
160
+ public function diffToArray ( $ from , $ to )
195
161
{
196
- if ($ this ->from === $ this ->to ) {
197
- return array ();
162
+ preg_match_all ('(\r\n|\r|\n) ' , $ from , $ fromMatches );
163
+ preg_match_all ('(\r\n|\r|\n) ' , $ to , $ toMatches );
164
+
165
+ if (is_string ($ from )) {
166
+ $ from = preg_split ('(\r\n|\r|\n) ' , $ from );
198
167
}
199
168
200
- $ from = preg_split ('(\r\n|\r|\n) ' , $ this ->from );
201
- $ to = preg_split ('(\r\n|\r|\n) ' , $ this ->to );
169
+ if (is_string ($ to )) {
170
+ $ to = preg_split ('(\r\n|\r|\n) ' , $ to );
171
+ }
202
172
203
173
$ start = array ();
204
174
$ end = array ();
@@ -226,12 +196,20 @@ public function toArray()
226
196
}
227
197
}
228
198
229
- $ common = self :: longestCommonSubsequence (
199
+ $ common = $ this -> longestCommonSubsequence (
230
200
array_values ($ from ), array_values ($ to )
231
201
);
232
202
233
203
$ diff = array ();
234
204
205
+ if (isset ($ fromMatches [0 ]) && $ toMatches [0 ] &&
206
+ count ($ fromMatches [0 ]) === count ($ toMatches [0 ]) &&
207
+ $ fromMatches [0 ] !== $ toMatches [0 ]) {
208
+ $ diff [] = array (
209
+ '#Warning: Strings contain different line endings! ' , 0
210
+ );
211
+ }
212
+
235
213
foreach ($ start as $ token ) {
236
214
$ diff [] = array ($ token , 0 /* OLD */ );
237
215
}
@@ -276,7 +254,7 @@ public function toArray()
276
254
* @param array $to
277
255
* @return array
278
256
*/
279
- protected static function longestCommonSubsequence (array $ from , array $ to )
257
+ protected function longestCommonSubsequence (array $ from , array $ to )
280
258
{
281
259
$ common = array ();
282
260
$ matrix = array ();
0 commit comments