-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL-CompareArrays.php
273 lines (245 loc) · 8.55 KB
/
XBRL-CompareArrays.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* CompareArrays
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2018 Lyquidity Solutions Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The class can be used with arrays or JSON strings that can be converted
* to arrays or file names of files that contain JSON strings.
*
* JSON strings will be converted to associative arrays. Objects within
* arrays will not be compared. This constraint could be changed but I
* have not real world examples to work with.
*
* Takes about 4 seconds to process pairs of 10MB JSON strings on a 2.3GHz
* dual core processor. Appears to scale linearly with the size of the
* input arrays and has been tested on real world examples of upto 125MB.
*/
/**
* Class to find similarities and differences between two arrays
* Only arrays or primitives will be considered. Objects will not be compared.
*/
class XBRL_CompareArrays
{
public $differences = array();
public $onlyA = array();
public $onlyB = array();
/**
* Static function to create an instance that compares two arrays
* @return XBRL_CompareArrays
*/
public static function createDiff()
{
return new XBRL_CompareArrays();
}
/**
* An example of comparing JSON files. This will generate 4 files:
* onlyA.json, onlyB.json, diffA.json, diffB.json
* @return void
*/
public static function example()
{
try
{
$directory = 'c:/LyquidityWeb/XBRLQuery/';
// $a = "{$directory}xbrl-validate-files/temp/copy/us-gaap-entryPoint-all-wotmp-2018-01-31 - Copy.json";
$a = "{$directory}xbrl-validate-files/temp/copy/us-gaap-entryPoint-all-wotmp-2018-01-31.json";
$b = "{$directory}xbrl-validate-files/temp/copy/us-gaap-entryPoint-all-wotmp-2018-01-31.json";
$diff = self::createDiff()
->diffJSONFiles( $a, $b )
->save( "{$directory}xbrl-validate-files/temp/copy/", '', true );
}
catch( Exception $ex )
{
echo $ex->getMessage() . "\n";
}
}
/**
* Used as a glue to create a path
*
* @var string
*/
private static $separator = '/|';
/**
* Compare two JSON files. The companison will be doing by create assoc arrays not objects
* @param string $jsonA
* @param string $jsonB
* @return XBRL_CompareArrays
*/
function diffJSONFiles( &$fileA, &$fileB )
{
if ( ! file_exists( $fileA ) || ! ( $a = file_get_contents( $fileA, true ) ) ) throw new Exception("Error opening file: '$fileA'");
if ( ! file_exists( $fileB ) || ! ( $b = file_get_contents( $fileB, true ) ) ) throw new Exception("Error opening file: '$fileB'");
return $this->diffJSON( $a, $b );
}
/**
* Compare two JSON strings. The companison will be doing by create assoc arrays not objects
* @param string $jsonA
* @param string $jsonB
* @return XBRL_CompareArrays
*/
function diffJSON( &$jsonA, &$jsonB )
{
if ( ! ( $a = json_decode( $jsonA, true ) ) ) throw new Exception( \XBRL::json_last_error_msg() );
$jsonA = '';
unset( $jsonA );
if ( ! ( $b = json_decode( $jsonB, true ) ) ) throw new Exception( \XBRL::json_last_error_msg() );
$jsonB = '';
unset( $jsonB );
return $this->diff( $a, $b );
}
/**
* Performs the diff. Fills info arrays.
* @return XBRL_CompareArrays
*/
function diff( &$currentA, &$currentB, $path = '', $depth = 0 )
{
if ( ! is_array( $currentA ) ) throw new \Exception('The first parameter is not an array');
if ( ! is_array( $currentB ) ) throw new \Exception('The second parameter is not an array');
$diffA = array_diff_key( $currentA, $currentB );
$diffB = array_diff_key( $currentB, $currentA );
if ( $diffA ) $this->onlyA[ $path ] = array_keys( $diffA );
if ( $diffB ) $this->onlyB[ $path ] = array_keys( $diffB );
$intersect = array_intersect_key( $currentA, $currentB );
$currentDifference =& $this->differences;
if ( $path )
{
$parts = explode( self::$separator, $path );
foreach( $parts as $part )
{
if ( ! isset( $currentDifference[ $part ] ) )
{
$currentDifference[ $part ] = array();
}
$currentDifference =& $currentDifference[ $part ];
}
}
foreach( $intersect as $key => &$elementA )
{
$elementB = $currentB[ $key ];
if ( is_array( $elementA ) && is_array( $intersect[ $key ] ) )
{
$newPath = $path ? $path . self::$separator . $key : $key;
$this->diff( $elementA, $elementB, $newPath, $depth + 1 );
if ( ! count( $currentDifference[ $key ] ) ) unset( $currentDifference[ $key ] );
continue;
}
$valueA = is_array( $elementA ) ? 'array' : $elementA;
$valueB = is_array( $elementB ) ? 'array' : $elementB;
switch( $key )
{
// Use case switchs to ignore keys
default:
if ( $valueA == $valueB )
{
// Should do something
}
else
{
$currentDifference[ $key ] = array( 'A' => $valueA, 'B' => $valueB );
}
}
}
return $this;
}
/**
* Saves the diff content to files
*
* @param string $dir The folder in which files will be saved
* @param string $prefix This prefix will be added to the file names so the source can be identified
* @param bool $twoFiles (Default: true) When true the diff files will be saved as two files so they can be compared with each other
* @return void
* @throws Exception If there is a problem saving
*/
function save( $dir, $prefix = '', $twoFiles = true )
{
// Make sure there is trailing slash
$dir = rtrim( $dir, '/' ) . '/';
if ( ! is_dir( $dir ) )
{
if ( mkdir( $dir ) ) throw new \Exception("Failed to created folder: '$dir'");
}
$filename = $dir . ( $prefix ? "$prefix-" : '' );
if ( file_put_contents( $filename . "onlyA.json", $this->renderOnly( $this->onlyA ) ) === false ) throw new \Exception("Error saving file 'onlyA'");
if ( file_put_contents( $filename . "onlyB.json", $this->renderOnly( $this->onlyB ) ) === false ) throw new \Exception("Error saving file 'onlyB'");
if ( $twoFiles )
{
if ( file_put_contents( $filename . "diffA.json", $this->renderDiff('A') ) === false ) throw new \Exception("Error saving file 'diffA'");
if ( file_put_contents( $filename . "diffB.json", $this->renderDiff('B') ) === false ) throw new \Exception("Error saving file 'diffB'");
}
else
{
if ( file_put_contents( $filename . "diff.json", self::json_encode( $this->differences ) ) === false ) throw new \Exception("Error saving file 'diff'");
if ( json_last_error() != JSON_ERROR_NONE ) throw new \Exception( "Error generating JSON for diff: " . \XBRL::json_last_error_msg() );
}
}
/**
* Creates a text rendering of the 'only' results
* @param string $only
* @return string
*/
private function &renderOnly( &$only )
{
$json = self::json_encode( $only );
if ( json_last_error() != JSON_ERROR_NONE ) throw new \Exception( "Error generating JSON for only" );
return $json;
}
/**
* Renders the differences
* @source string 'A' or 'B' to select the contents of the diff information
* @return string
*/
private function &renderDiff( $source )
{
$json = '';
switch( $source )
{
case 'A':
break;
case 'B':
break;
}
$change = function( &$array, $source ) use( &$change )
{
if ( count( $array ) == 2 && isset( $array['A'] ) && isset( $array['B'] ) )
{
return $array[ $source ];
}
$result = array();
foreach( $array as $key => $element )
{
$result[ $key ] = $change( $element, $source );
}
return $result;
};
$changed = $change( $this->differences, $source );
// Use the same variable so the memory for the constucted array can be released
$changed = self::json_encode( $changed );
return $change;
}
/**
* Create a JSON encoded string. Removes the special 'glue' used to create paths
*
* @param [type] $array
* @return void
*/
private static function json_encode( &$array )
{
return str_replace( self::$separator, '/', json_encode( $array, JSON_PRETTY_PRINT ) );
}
}