-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathListDiffer.php
More file actions
69 lines (57 loc) · 1.54 KB
/
ListDiffer.php
File metadata and controls
69 lines (57 loc) · 1.54 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
<?php
declare( strict_types = 1 );
namespace Diff\Differ;
use Diff\ArrayComparer\ArrayComparer;
use Diff\ArrayComparer\StrictArrayComparer;
use Diff\DiffOp\DiffOp;
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpRemove;
/**
* Differ that only looks at the values of the arrays (and thus ignores key differences).
*
* By default values are compared using a StrictArrayComparer.
* You can alter the ArrayComparer used by providing one in the constructor.
*
* @since 0.4
*
* @license BSD-3-Clause
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ListDiffer implements Differ {
/**
* @var ArrayComparer
*/
private $arrayComparer;
public function __construct( ?ArrayComparer $arrayComparer = null ) {
$this->arrayComparer = $arrayComparer ?? new StrictArrayComparer();
}
/**
* @see Differ::doDiff
*
* @since 0.4
*
* @param array $oldValues The first array
* @param array $newValues The second array
*
* @return DiffOp[]
*/
public function doDiff( array $oldValues, array $newValues ): array {
$operations = [];
foreach ( $this->diffArrays( $newValues, $oldValues ) as $addition ) {
$operations[] = new DiffOpAdd( $addition );
}
foreach ( $this->diffArrays( $oldValues, $newValues ) as $removal ) {
$operations[] = new DiffOpRemove( $removal );
}
return $operations;
}
/**
* @param array $arrayOne
* @param array $arrayTwo
*
* @return array
*/
private function diffArrays( array $arrayOne, array $arrayTwo ): array {
return $this->arrayComparer->diffArrays( $arrayOne, $arrayTwo );
}
}