-
Notifications
You must be signed in to change notification settings - Fork 15
/
ThrowingPatcher.php
98 lines (80 loc) · 1.75 KB
/
ThrowingPatcher.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
<?php
declare( strict_types = 1 );
namespace Diff\Patcher;
use Diff\Differ\ListDiffer;
use Diff\Differ\MapDiffer;
use Diff\DiffOp\Diff\Diff;
/**
* Base class for patchers that have the ability to throw errors
* when they encounter diff operations they can not handle or
* ignore them if specified.
*
* @since 0.4
*
* @license BSD-3-Clause
* @author Jeroen De Dauw < [email protected] >
*/
abstract class ThrowingPatcher implements PreviewablePatcher {
/**
* @var bool
*/
private $throwErrors;
/**
* @since 0.4
*
* @param bool $throwErrors
*/
public function __construct( bool $throwErrors = false ) {
$this->throwErrors = $throwErrors;
}
/**
* @since 0.4
*
* @param string $message
*
* @throws PatcherException
*/
protected function handleError( string $message ) {
if ( $this->throwErrors ) {
throw new PatcherException( $message );
}
}
/**
* Set the patcher to ignore errors.
*
* @since 0.4
*/
public function ignoreErrors() {
$this->throwErrors = false;
}
/**
* Set the patcher to throw errors.
*
* @since 0.4
*/
public function throwErrors() {
$this->throwErrors = true;
}
/**
* @see PreviewablePatcher::getApplicableDiff
*
* @since 0.4
*
* @param array $base
* @param Diff $diff
*
* @return Diff
* @throws PatcherException
*/
public function getApplicableDiff( array $base, Diff $diff ): Diff {
$throwErrors = $this->throwErrors;
$this->throwErrors = false;
$patched = $this->patch( $base, $diff );
$this->throwErrors = $throwErrors;
$treatAsMap = $diff->looksAssociative();
$differ = $treatAsMap ? new MapDiffer( true ) : new ListDiffer();
$diffOps = $differ->doDiff( $base, $patched );
$diff = new Diff( $diffOps, $treatAsMap );
return $diff;
}
}