16
16
*
17
17
* PHP version 7.2 or greater
18
18
*
19
- * @package jblond
20
- * @author Chris Boulton <chris.boulton@interspire.com>
19
+ * @package jblond
20
+ * @author Chris Boulton <chris.boulton@interspire.com>
21
21
* @copyright (c) 2009 Chris Boulton
22
- * @license New BSD License http://www.opensource.org/licenses/bsd-license.php
23
- * @version 1.15
24
- * @link https://github.com/JBlond/php-diff
22
+ * @license New BSD License http://www.opensource.org/licenses/bsd-license.php
23
+ * @version 1.15
24
+ * @link https://github.com/JBlond/php-diff
25
25
*/
26
26
class Diff
27
27
{
28
28
/**
29
- * @var array The "old" string to compare to .
29
+ * @var array The first version to compare.
30
30
* Each element contains a line of this string.
31
31
*/
32
- private $ old ;
32
+ private $ version1 ;
33
33
34
34
/**
35
- * @var array The "new" string to compare.
35
+ * @var array The second version to compare.
36
36
* Each element contains a line of this string.
37
37
*/
38
- private $ new ;
38
+ private $ version2 ;
39
39
40
40
/**
41
- * @var array Contains generated op-codes which represent the differences between "old" and "new" .
41
+ * @var array Contains generated op-codes which represent the differences between version1 and version2 .
42
42
*/
43
43
private $ groupedCodes ;
44
44
@@ -47,18 +47,20 @@ class Diff
47
47
* value.
48
48
* - context The amount of lines to include around blocks that differ.
49
49
* - ignoreWhitespace When true, tabs and spaces are ignored while comparing.
50
+ * The spacing of version1 is leading.
50
51
* - ignoreCase When true, character casing is ignored while comparing.
52
+ * The casing of version1 is leading.
51
53
*/
52
54
private $ defaultOptions = [
53
- 'context ' => 3 ,
54
- 'ignoreWhitespace ' => false ,
55
- 'ignoreCase ' => false ,
55
+ 'context ' => 3 ,
56
+ 'ignoreWhitespace ' => false ,
57
+ 'ignoreCase ' => false ,
56
58
];
57
59
58
60
/**
59
61
* @var array Associative array containing the options that will be applied for generating the diff.
60
62
* The key-value pairs are set at the constructor of this class.
61
- * @see Diff::setOptions()
63
+ * @see Diff::setOptions()
62
64
*/
63
65
private $ options = [];
64
66
@@ -73,28 +75,55 @@ class Diff
73
75
* associative array where each key-value pair represents an option and its value (E.g. ['context' => 3], ...).
74
76
* When a keyName matches the name of a default option, that option's value will be overridden by the key's value.
75
77
* Any other keyName (and it's value) can be added as an option, but will not be used if not implemented.
78
+ *
79
+ * @param string|array $version1 Data to compare to.
80
+ * @param string|array $version2 Data to compare.
81
+ * @param array $options User defined option values.
82
+ *
76
83
* @see Diff::$defaultOptions
77
84
*
78
- * @param string|array $old Data to compare to.
79
- * @param string|array $new Data to compare.
80
- * @param array $options User defined option values.
81
85
*/
82
- public function __construct ($ old , $ new , array $ options = [])
86
+ public function __construct ($ version1 , $ version2 , array $ options = [])
83
87
{
84
88
//Convert "old" and "new" into an array of lines when they are strings.
85
- $ this ->old = $ this ->getArgumentType ($ old ) ? preg_split ("/ \r\n| \n| \r/ " , $ old ) : $ old ;
86
- $ this ->new = $ this ->getArgumentType ($ new ) ? preg_split ("/ \r\n| \n| \r/ " , $ new ) : $ new ;
89
+ $ this ->version1 = $ this ->getArgumentType ($ version1 ) ? preg_split ("/ \r\n| \n| \r/ " , $ version1 ) : $ version1 ;
90
+ $ this ->version2 = $ this ->getArgumentType ($ version2 ) ? preg_split ("/ \r\n| \n| \r/ " , $ version2 ) : $ version2 ;
87
91
88
92
//Override the default options, define others.
89
93
$ this ->setOptions ($ options );
90
94
}
91
95
96
+ /**
97
+ * Get the type of a variable.
98
+ *
99
+ * The return value depend on the type of variable:
100
+ * 0 If the type is 'array'
101
+ * 1 if the type is 'string'
102
+ *
103
+ * @param mixed $var Variable to get type from.
104
+ *
105
+ * @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
106
+ * @throws InvalidArgumentException When the type isn't 'array' or 'string'.
107
+ *
108
+ */
109
+ public function getArgumentType ($ var ): int
110
+ {
111
+ switch (true ) {
112
+ case (is_array ($ var )):
113
+ return 0 ;
114
+ case (is_string ($ var )):
115
+ return 1 ;
116
+ default :
117
+ throw new InvalidArgumentException ('Invalid argument type! Argument must be of type array or string. ' );
118
+ }
119
+ }
120
+
92
121
/**
93
122
* Set the options to be used by the sequence matcher, called by this class.
94
123
*
95
124
* @param array $options User defined option names and values.
96
125
*
97
- *@see Diff::$defaultOptions
126
+ * @see Diff::$defaultOptions
98
127
*
99
128
* @see Diff::getGroupedOpCodes()
100
129
*
@@ -111,22 +140,21 @@ public function setOptions(array $options)
111
140
*
112
141
* @return array Contains the lines of the "old" string to compare to.
113
142
*/
114
- public function getOld (): array
143
+ public function getVersion1 (): array
115
144
{
116
- return $ this ->old ;
145
+ return $ this ->version1 ;
117
146
}
118
147
119
148
/**
120
149
* Get the lines of "new".
121
150
*
122
151
* @return array Contains the lines of the "new" string to compare.
123
152
*/
124
- public function getNew (): array
153
+ public function getVersion2 (): array
125
154
{
126
- return $ this ->new ;
155
+ return $ this ->version2 ;
127
156
}
128
157
129
-
130
158
/**
131
159
* Render a diff-view using a rendering class and get its results.
132
160
*
@@ -151,14 +179,14 @@ public function render(object $renderer)
151
179
* If the arguments for both parameters are omitted, the entire array will be returned.
152
180
* If the argument for the second parameter is ommitted, the element defined as start will be returned.
153
181
*
154
- * @param array $array The source array.
155
- * @param int $start The first element of the range to get.
156
- * @param int|null $end The last element of the range to get.
182
+ * @param array $array The source array.
183
+ * @param int $start The first element of the range to get.
184
+ * @param int|null $end The last element of the range to get.
157
185
* If not supplied, only the element at start will be returned.
158
186
*
187
+ * @return array Array containing all of the elements of the specified range.
159
188
* @throws OutOfRangeException When the value of start or end are invalid to define a range.
160
189
*
161
- * @return array Array containing all of the elements of the specified range.
162
190
*/
163
191
public function getArrayRange (array $ array , int $ start = 0 , $ end = null ): array
164
192
{
@@ -182,31 +210,6 @@ public function getArrayRange(array $array, int $start = 0, $end = null): array
182
210
return array_slice ($ array , $ start , $ length );
183
211
}
184
212
185
- /**
186
- * Get the type of a variable.
187
- *
188
- * The return value depend on the type of variable:
189
- * 0 If the type is 'array'
190
- * 1 if the type is 'string'
191
- *
192
- * @param mixed $var Variable to get type from.
193
- *
194
- * @throws InvalidArgumentException When the type isn't 'array' or 'string'.
195
- *
196
- * @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
197
- */
198
- public function getArgumentType ($ var ): int
199
- {
200
- switch (true ) {
201
- case (is_array ($ var )):
202
- return 0 ;
203
- case (is_string ($ var )):
204
- return 1 ;
205
- default :
206
- throw new InvalidArgumentException ('Invalid argument type! Argument must be of type array or string. ' );
207
- }
208
- }
209
-
210
213
/**
211
214
* Generate a list of the compiled and grouped op-codes for the differences between two strings.
212
215
*
@@ -224,7 +227,7 @@ public function getGroupedOpCodes(): array
224
227
}
225
228
226
229
//Get and cache the grouped op-codes.
227
- $ sequenceMatcher = new SequenceMatcher ($ this ->old , $ this ->new , $ this ->options );
230
+ $ sequenceMatcher = new SequenceMatcher ($ this ->version1 , $ this ->version2 , $ this ->options );
228
231
$ this ->groupedCodes = $ sequenceMatcher ->getGroupedOpCodes ($ this ->options ['context ' ]);
229
232
230
233
return $ this ->groupedCodes ;
0 commit comments