|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright (c) 2017-present brian ridley |
|
5
|
|
|
* @author brian ridley <[email protected]> |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace ptlis\GrepDb\Replace\Result; |
|
10
|
|
|
|
|
11
|
|
|
use ptlis\GrepDb\Search\Result\RowSearchResult; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Result of row replacements. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class RowReplaceResult |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var FieldReplaceResult[] */ |
|
19
|
|
|
private $fieldResultList; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string[] */ |
|
22
|
|
|
private $errorList; |
|
23
|
|
|
|
|
24
|
|
|
/** @var RowSearchResult */ |
|
25
|
|
|
private $rowSearchResult; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param FieldReplaceResult[] $fieldResultList |
|
30
|
|
|
* @param string[] $errorList |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( |
|
33
|
|
|
RowSearchResult $rowSearchResult, |
|
34
|
|
|
array $fieldResultList, |
|
35
|
|
|
array $errorList |
|
36
|
|
|
) { |
|
37
|
|
|
$this->fieldResultList = $fieldResultList; |
|
38
|
|
|
$this->errorList = $errorList; |
|
39
|
|
|
$this->rowSearchResult = $rowSearchResult; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the number of replacements in all fields in this row. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getReplacedCount(): int |
|
46
|
|
|
{ |
|
47
|
|
|
return intval(array_reduce( |
|
48
|
|
|
$this->fieldResultList, |
|
49
|
|
|
function (int $count, FieldReplaceResult $fieldResult) { |
|
50
|
|
|
return $count + $fieldResult->getReplacedCount(); |
|
51
|
|
|
}, |
|
52
|
|
|
0 |
|
53
|
|
|
)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get any replacement errors. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string[] |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getErrorList(): array |
|
62
|
|
|
{ |
|
63
|
|
|
$errorList = $this->errorList; |
|
64
|
|
|
foreach ($this->fieldResultList as $fieldResult) { |
|
65
|
|
|
foreach ($fieldResult->getErrorList() as $error) { |
|
66
|
|
|
$errorList[] = $error; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $errorList; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return FieldReplaceResult[] |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getFieldResultList(): array |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->fieldResultList; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getRowSearchResult(): RowSearchResult |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->rowSearchResult; |
|
84
|
|
|
} |
|
85
|
|
|
} |