|
49 | 49 | */
|
50 | 50 | class Diff
|
51 | 51 | {
|
52 |
| - /** |
53 |
| - * @var array The "old" sequence to use as the basis for the comparison. |
54 |
| - */ |
55 |
| - private $a = null; |
56 |
| - |
57 |
| - /** |
58 |
| - * @var array The "new" sequence to generate the changes for. |
59 |
| - */ |
60 |
| - private $b = null; |
61 |
| - |
62 |
| - /** |
63 |
| - * @var array Array containing the generated op codes for the differences between the two items. |
64 |
| - */ |
65 |
| - private $groupedCodes = null; |
66 |
| - |
67 |
| - /** |
68 |
| - * @var array Associative array of the default options available for the diff class and their default value. |
69 |
| - */ |
70 |
| - private $defaultOptions = array( |
71 |
| - 'context' => 3, |
72 |
| - 'ignoreNewLines' => false, |
73 |
| - 'ignoreWhitespace' => false, |
74 |
| - 'ignoreCase' => false, |
75 |
| - 'labelDifferences'=>'Differences' |
76 |
| - ); |
77 |
| - |
78 |
| - /** |
79 |
| - * @var array Array of the options that have been applied for generating the diff. |
80 |
| - */ |
81 |
| - public $options = array(); |
82 |
| - |
83 |
| - /** |
84 |
| - * The constructor. |
85 |
| - * |
86 |
| - * @param array $a Array containing the lines of the first string to compare. |
87 |
| - * @param array $b Array containing the lines for the second string to compare. |
88 |
| - * @param array $options Array for the options |
89 |
| - */ |
90 |
| - public function __construct($a, $b, $options = array()) |
91 |
| - { |
92 |
| - $this->a = $a; |
93 |
| - $this->b = $b; |
94 |
| - |
95 |
| - if (is_array($options)) { |
96 |
| - $this->options = array_merge($this->defaultOptions, $options); |
97 |
| - } else { |
98 |
| - $this->options = $this->defaultOptions; |
99 |
| - } |
100 |
| - } |
101 |
| - |
102 |
| - |
103 |
| - /** |
104 |
| - * Render a diff using the supplied rendering class and return it. |
105 |
| - * |
106 |
| - * @param object $renderer object $renderer An instance of the rendering object to use for generating the diff. |
107 |
| - * @return mixed The generated diff. Exact return value depends on the rendered. |
108 |
| - */ |
109 |
| - public function render($renderer) |
110 |
| - { |
111 |
| - $renderer->diff = $this; |
112 |
| - return $renderer->render(); |
113 |
| - } |
114 |
| - |
115 |
| - /** |
116 |
| - * Get a range of lines from $start to $end from the first comparison string |
117 |
| - * and return them as an array. If no values are supplied, the entire string |
118 |
| - * is returned. It's also possible to specify just one line to return only |
119 |
| - * that line. |
120 |
| - * |
121 |
| - * @param int $start The starting number. |
122 |
| - * @param int $end The ending number. If not supplied, only the item in $start will be returned. |
123 |
| - * @return array Array of all of the lines between the specified range. |
124 |
| - */ |
125 |
| - public function getA($start = 0, $end = null) : array |
126 |
| - { |
127 |
| - if ($start == 0 && $end === null) { |
128 |
| - return $this->a; |
129 |
| - } |
130 |
| - |
131 |
| - if ($end === null) { |
132 |
| - $length = 1; |
133 |
| - } else { |
134 |
| - $length = $end - $start; |
135 |
| - } |
136 |
| - |
137 |
| - return array_slice($this->a, $start, $length); |
138 |
| - } |
139 |
| - |
140 |
| - /** |
141 |
| - * Get a range of lines from $start to $end from the second comparison string |
142 |
| - * and return them as an array. If no values are supplied, the entire string |
143 |
| - * is returned. It's also possible to specify just one line to return only |
144 |
| - * that line. |
145 |
| - * |
146 |
| - * @param int $start The starting number. |
147 |
| - * @param int $end The ending number. If not supplied, only the item in $start will be returned. |
148 |
| - * @return array Array of all of the lines between the specified range. |
149 |
| - */ |
150 |
| - public function getB($start = 0, $end = null) : array |
151 |
| - { |
152 |
| - if ($start == 0 && $end === null) { |
153 |
| - return $this->b; |
154 |
| - } |
155 |
| - |
156 |
| - if ($end === null) { |
157 |
| - $length = 1; |
158 |
| - } else { |
159 |
| - $length = $end - $start; |
160 |
| - } |
161 |
| - |
162 |
| - return array_slice($this->b, $start, $length); |
163 |
| - } |
164 |
| - |
165 |
| - /** |
166 |
| - * Generate a list of the compiled and grouped op codes for the differences between the |
167 |
| - * two strings. Generally called by the renderer, this class instantiates the sequence |
168 |
| - * matcher and performs the actual diff generation and return an array of the op codes |
169 |
| - * for it. Once generated, the results are cached in the diff class instance. |
170 |
| - * |
171 |
| - * @return array Array of the grouped op codes for the generated diff. |
172 |
| - */ |
173 |
| - public function getGroupedOpcodes() : array |
174 |
| - { |
175 |
| - if (!is_null($this->groupedCodes)) { |
176 |
| - return $this->groupedCodes; |
177 |
| - } |
178 |
| - |
179 |
| - $sequenceMatcher = new SequenceMatcher($this->a, $this->b, $this->options, null); |
180 |
| - $this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']); |
181 |
| - return $this->groupedCodes; |
182 |
| - } |
| 52 | + /** |
| 53 | + * @var array The "old" sequence to use as the basis for the comparison. |
| 54 | + */ |
| 55 | + private $a = null; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var array The "new" sequence to generate the changes for. |
| 59 | + */ |
| 60 | + private $b = null; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var array Array containing the generated op codes for the differences between the two items. |
| 64 | + */ |
| 65 | + private $groupedCodes = null; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var array Associative array of the default options available for the diff class and their default value. |
| 69 | + */ |
| 70 | + private $defaultOptions = array( |
| 71 | + 'context' => 3, |
| 72 | + 'ignoreNewLines' => false, |
| 73 | + 'ignoreWhitespace' => false, |
| 74 | + 'ignoreCase' => false, |
| 75 | + 'labelDifferences'=>'Differences' |
| 76 | + ); |
| 77 | + |
| 78 | + /** |
| 79 | + * @var array Array of the options that have been applied for generating the diff. |
| 80 | + */ |
| 81 | + public $options = array(); |
| 82 | + |
| 83 | + /** |
| 84 | + * The constructor. |
| 85 | + * |
| 86 | + * @param array $a Array containing the lines of the first string to compare. |
| 87 | + * @param array $b Array containing the lines for the second string to compare. |
| 88 | + * @param array $options Array for the options |
| 89 | + */ |
| 90 | + public function __construct($a, $b, $options = array()) |
| 91 | + { |
| 92 | + $this->a = $a; |
| 93 | + $this->b = $b; |
| 94 | + |
| 95 | + if (is_array($options)) { |
| 96 | + $this->options = array_merge($this->defaultOptions, $options); |
| 97 | + } else { |
| 98 | + $this->options = $this->defaultOptions; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + /** |
| 104 | + * Render a diff using the supplied rendering class and return it. |
| 105 | + * |
| 106 | + * @param object $renderer object $renderer An instance of the rendering object to use for generating the diff. |
| 107 | + * @return mixed The generated diff. Exact return value depends on the rendered. |
| 108 | + */ |
| 109 | + public function render($renderer) |
| 110 | + { |
| 111 | + $renderer->diff = $this; |
| 112 | + return $renderer->render(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get a range of lines from $start to $end from the first comparison string |
| 117 | + * and return them as an array. If no values are supplied, the entire string |
| 118 | + * is returned. It's also possible to specify just one line to return only |
| 119 | + * that line. |
| 120 | + * |
| 121 | + * @param int $start The starting number. |
| 122 | + * @param int $end The ending number. If not supplied, only the item in $start will be returned. |
| 123 | + * @return array Array of all of the lines between the specified range. |
| 124 | + */ |
| 125 | + public function getA($start = 0, $end = null) : array |
| 126 | + { |
| 127 | + if ($start == 0 && $end === null) { |
| 128 | + return $this->a; |
| 129 | + } |
| 130 | + |
| 131 | + if ($end === null) { |
| 132 | + $length = 1; |
| 133 | + } else { |
| 134 | + $length = $end - $start; |
| 135 | + } |
| 136 | + |
| 137 | + return array_slice($this->a, $start, $length); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get a range of lines from $start to $end from the second comparison string |
| 142 | + * and return them as an array. If no values are supplied, the entire string |
| 143 | + * is returned. It's also possible to specify just one line to return only |
| 144 | + * that line. |
| 145 | + * |
| 146 | + * @param int $start The starting number. |
| 147 | + * @param int $end The ending number. If not supplied, only the item in $start will be returned. |
| 148 | + * @return array Array of all of the lines between the specified range. |
| 149 | + */ |
| 150 | + public function getB($start = 0, $end = null) : array |
| 151 | + { |
| 152 | + if ($start == 0 && $end === null) { |
| 153 | + return $this->b; |
| 154 | + } |
| 155 | + |
| 156 | + if ($end === null) { |
| 157 | + $length = 1; |
| 158 | + } else { |
| 159 | + $length = $end - $start; |
| 160 | + } |
| 161 | + |
| 162 | + return array_slice($this->b, $start, $length); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Generate a list of the compiled and grouped op codes for the differences between the |
| 167 | + * two strings. Generally called by the renderer, this class instantiates the sequence |
| 168 | + * matcher and performs the actual diff generation and return an array of the op codes |
| 169 | + * for it. Once generated, the results are cached in the diff class instance. |
| 170 | + * |
| 171 | + * @return array Array of the grouped op codes for the generated diff. |
| 172 | + */ |
| 173 | + public function getGroupedOpcodes() : array |
| 174 | + { |
| 175 | + if (!is_null($this->groupedCodes)) { |
| 176 | + return $this->groupedCodes; |
| 177 | + } |
| 178 | + |
| 179 | + $sequenceMatcher = new SequenceMatcher($this->a, $this->b, $this->options, null); |
| 180 | + $this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']); |
| 181 | + return $this->groupedCodes; |
| 182 | + } |
183 | 183 | }
|
0 commit comments