Repository navigation
Expand file tree
/
Copy pathArrayComparator.php
More file actions
executable file
·210 lines (180 loc) · 5.66 KB
/
Copy pathArrayComparator.php
File metadata and controls
executable file
·210 lines (180 loc) · 5.66 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
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
<?php
/**
* @author Matthieu Napoli <matthieu@mnapoli.fr>
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/
namespace ArrayComparator;
/**
* Compares 2 arrays
*/
class ArrayComparator
{
/**
* Closure comparing 2 items and returning if they have the same identity
* @var callable function($key1, $key2, $item1, $item2) returns true or false
*/
private $itemIdentityComparator;
/**
* Closure comparing 2 items and returning if there are differences
* @var callable function($item1, $item2) returns true or false
*/
private $itemComparator;
/**
* Closure executed when items are equal
* @var callable function($item1, $item2)
*/
private $whenEqual;
/**
* Closure executed when items are different
* @var callable function($item1, $item2)
*/
private $whenDifferent;
/**
* Closure executed when an item is missing from the left array (first array)
* @var callable function($item2)
*/
private $whenMissingLeft;
/**
* Closure executed when an item is missing from the right array (second array)
* @var callable function($item1)
*/
private $whenMissingRight;
/**
* Constructor
*/
public function __construct()
{
// Default behaviors
$this->itemIdentityComparator = array($this, 'areSame');
$this->itemComparator = array($this, 'areEqual');
}
/**
* Run the comparison over the arrays
*/
public function compare(array $array1, array $array2)
{
$compareItems = $this->itemComparator;
$whenEqual = $this->whenEqual;
$whenDifferent = $this->whenDifferent;
$whenMissingLeft = $this->whenMissingLeft;
$whenMissingRight = $this->whenMissingRight;
foreach ($array1 as $key1 => $item1) {
$item2 = $this->searchItem($key1, $item1, $array2);
if ($item2 !== null) {
// Compare 2 items
$itemsAreEqual = call_user_func($compareItems, $item1, $item2);
if (!$itemsAreEqual && $whenDifferent) {
// Items are different
call_user_func($whenDifferent, $item1, $item2);
} elseif ($itemsAreEqual && $whenEqual) {
// Items are equal
call_user_func($whenEqual, $item1, $item2);
}
} elseif ($whenMissingRight) {
// Item from left array is missing from right array
call_user_func($whenMissingRight, $item1);
}
}
foreach ($array2 as $key2 => $item2) {
$item1 = $this->searchItem($key2, $item2, $array1);
if ($item1 === null && $whenMissingLeft) {
// Item from right array is missing from left array
call_user_func($whenMissingLeft, $item2);
}
}
}
/**
* Closure comparing 2 items and returning if they have the same identity
* @var callable $callback function($key1, $key2, $item1, $item2) returns true or false
* @return $this
*/
public function setItemIdentityComparator($callback)
{
$this->itemIdentityComparator = $callback;
return $this;
}
/**
* Closure comparing 2 items and returning if there are differences
* @var callable $callback function($item1, $item2) returning true or false
* @return $this
*/
public function setItemComparator($callback)
{
$this->itemComparator = $callback;
return $this;
}
/**
* Closure executed when items are equal
* @param callable $callback function($item1, $item2)
* @return $this
*/
public function whenEqual($callback)
{
$this->whenEqual = $callback;
return $this;
}
/**
* Closure executed when items are different
* @param callable $callback function($item1, $item2)
* @return $this
*/
public function whenDifferent($callback)
{
$this->whenDifferent = $callback;
return $this;
}
/**
* Closure executed when an item is missing from the left array (first array)
* @param callable $callback function($item2)
* @return $this
*/
public function whenMissingLeft($callback)
{
$this->whenMissingLeft = $callback;
return $this;
}
/**
* Closure executed when an item is missing from the right array (second array)
* @var callable $callback function($item1)
* @return $this
*/
public function whenMissingRight($callback)
{
$this->whenMissingRight = $callback;
return $this;
}
/**
* Compares 2 items and returns if they have the same identity (if they represent the same item)
* @param mixed $key1 Array key of the first item
* @param mixed $key2 Array key of the second item
* @param mixed $item1
* @param mixed $item2
* @return boolean
*/
protected function areSame($key1, $key2, $item1, $item2)
{
// Default behavior
return $key1 === $key2;
}
/**
* Compares 2 items and returns if there are differences
* @param mixed $item1
* @param mixed $item2
* @return boolean
*/
protected function areEqual($item1, $item2)
{
// Default behavior
return $item1 == $item2;
}
private function searchItem($key1, $item1, array $array)
{
$areSameItem = $this->itemIdentityComparator;
foreach ($array as $key2 => $item2) {
if (call_user_func($areSameItem, $key1, $key2, $item1, $item2)) {
return $item2;
}
}
return null;
}
}