-
Notifications
You must be signed in to change notification settings - Fork 15
/
ListPatcher.php
62 lines (52 loc) · 1.5 KB
/
ListPatcher.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
<?php
declare( strict_types = 1 );
namespace Diff\Patcher;
use Diff\DiffOp\Diff\Diff;
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpRemove;
/**
* @since 0.4
*
* @license BSD-3-Clause
* @author Jeroen De Dauw < [email protected] >
*/
class ListPatcher extends ThrowingPatcher {
/**
* @see Patcher::patch
*
* Applies the provided diff to the provided array and returns the result.
* The provided diff needs to be non-associative. In other words, calling
* isAssociative on it should return false.
*
* Note that remove operations can introduce gaps into the input array $base.
* For instance, when the input is [ 0 => 'a', 1 => 'b', 2 => 'c' ], and there
* is one remove operation for 'b', the result will be [ 0 => 'a', 2 => 'c' ].
*
* @since 0.4
*
* @param array $base
* @param Diff $diff
*
* @return array
* @throws PatcherException
*/
public function patch( array $base, Diff $diff ): array {
if ( $diff->looksAssociative() ) {
$this->handleError( 'ListPatcher can only patch using non-associative diffs' );
}
foreach ( $diff as $diffOp ) {
if ( $diffOp instanceof DiffOpAdd ) {
$base[] = $diffOp->getNewValue();
} elseif ( $diffOp instanceof DiffOpRemove ) {
$needle = $diffOp->getOldValue();
$key = array_search( $needle, $base, !is_object( $needle ) );
if ( $key === false ) {
$this->handleError( 'Cannot remove an element from a list if it is not present' );
continue;
}
unset( $base[$key] );
}
}
return $base;
}
}