-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVersionComparison.php
More file actions
91 lines (71 loc) · 2.22 KB
/
VersionComparison.php
File metadata and controls
91 lines (71 loc) · 2.22 KB
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
<?php
/**
* Compare Versions from a list to find missing, different and extra packages
*/
class VersionComparison {
/**
* Compare Version
*
* @param array $list_array Array of group (key) and json encoded item : version data (value)
* @param string $base_group Which group to use as base comparison (if not set GetBaseGroup)
*/
public static function CompareVersions($list_array = array(), $base_group = ''){
$result = array();
if(!empty($list_array)){
// Check for base_group, if not found use key of first group
if($base_group == ''){
$base_group = self::GetBaseGroup($list_array);
}
$base_list = json_decode($list_array[$base_group], true);
foreach($list_array as $key => $value){
if($key != $base_group){
$compare_list = json_decode($value, true);
$result[$key] = self::Compare($base_list, $compare_list);
}
}
}else{
die('No Data Given');
}
return $result;
}
// Get base group from 1st item in list array
public static function GetBaseGroup($list_array){
reset($list_array);
$result = key($list_array);
return $result;
}
// Compare listA and listB and return missing, different and extra items
private static function Compare($listA, $listB){
$result = array();
$result['missing'] = self::getMissing($listA, $listB);
$result['different'] = self::getDifferent($listA, $listB);
$result['extra'] = self::getExtra($listA, $listB);
return $result;
}
// Get the diff between array keys (A->B) (package names)
private static function getMissing($listA, $listB){
$keysA = array_keys($listA);
$keysB = array_keys($listB);
$diff = array_diff($keysA, $keysB);
return array_values($diff);
}
// Get diff between array key and value, remove non-existing in non-base
private static function getDifferent($listA, $listB){
$diff = array_diff_assoc($listA, $listB);
$result = array();
foreach($diff as $key => $value){
if(isset($listB[$key])){
$result[$key] = $value;
}
}
return $result;
}
// Get the diff between array keys (B->A) (package names)
private static function getExtra($listA, $listB){
$keysA = array_keys($listA);
$keysB = array_keys($listB);
$diff = array_diff($keysB, $keysA);
return array_values($diff);
}
}
?>