1
|
|
|
<?php |
2
|
|
|
namespace DataDiff; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
|
6
|
|
|
class DiffStorageStoreRow implements DiffStorageStoreRowInterface { |
7
|
|
|
/** @var array */ |
8
|
|
|
private $data = []; |
9
|
|
|
/** @var DiffStorageStoreRowData */ |
10
|
|
|
private $localData; |
11
|
|
|
/** @var DiffStorageStoreRowData */ |
12
|
|
|
private $foreignRowData; |
13
|
|
|
/** @var callable */ |
14
|
|
|
private $stringFormatter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param array $localData |
18
|
|
|
* @param array $foreignData |
19
|
|
|
* @param array $keys |
20
|
|
|
* @param array $valueKeys |
21
|
|
|
* @param array $converter |
22
|
|
|
* @param callable $stringFormatter |
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $localData = null, array $foreignData = null, array $keys, array $valueKeys, array $converter, $stringFormatter) { |
25
|
|
|
if($localData !== null) { |
26
|
|
|
$this->data = $localData; |
27
|
|
|
} elseif($foreignData !== null) { |
28
|
|
|
$this->data = $foreignData; |
29
|
|
|
} |
30
|
|
|
$localData = is_array($localData) ? $localData : []; |
31
|
|
|
$foreignData = is_array($foreignData) ? $foreignData : []; |
32
|
|
|
$this->localData = new DiffStorageStoreRowData($localData, $foreignData, $keys, $valueKeys, $converter); |
33
|
|
|
$this->foreignRowData = new DiffStorageStoreRowData($foreignData, $localData, $keys, $valueKeys, $converter); |
34
|
|
|
$this->stringFormatter = $stringFormatter; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return DiffStorageStoreRowData |
39
|
|
|
*/ |
40
|
|
|
public function getLocal() { |
41
|
|
|
return $this->localData; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return DiffStorageStoreRowData |
46
|
|
|
*/ |
47
|
|
|
public function getForeign() { |
48
|
|
|
return $this->foreignRowData; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* `$options` are: |
53
|
|
|
* * `keys`: Only these keys are considered and returned |
54
|
|
|
* * `ignore`: These keys are ignored and omitted |
55
|
|
|
* |
56
|
|
|
* @param array $options |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getData(array $options = []) { |
60
|
|
|
return $this->localData->getData($options); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* `$options` are: |
65
|
|
|
* * `keys`: Only these keys are considered and returned |
66
|
|
|
* * `ignore`: These keys are ignored and omitted |
67
|
|
|
* |
68
|
|
|
* @param array $options |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function getForeignData(array $options = []) { |
72
|
|
|
return $this->foreignRowData->getData($options); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $fields |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getDiff(array $fields = null) { |
80
|
|
|
return $this->localData->getDiff($fields); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $fields |
85
|
|
|
* @param mixed $format |
86
|
|
|
* @return array |
87
|
|
|
* @throws Exception |
88
|
|
|
*/ |
89
|
|
|
public function getDiffFormatted(array $fields = null, $format = null) { |
90
|
|
|
return $this->localData->getDiffFormatted($fields, $format); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function jsonSerialize() { |
97
|
|
|
return $this->data; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param mixed $offset |
102
|
|
|
* @return boolean true on success or false on failure. |
103
|
|
|
*/ |
104
|
|
|
public function offsetExists($offset) { |
105
|
|
|
return array_key_exists($offset, $this->data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param mixed $offset |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function offsetGet($offset) { |
113
|
|
|
if($this->offsetExists($offset)) { |
114
|
|
|
return $this->data[$offset]; |
115
|
|
|
} |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param mixed $offset |
121
|
|
|
* @param mixed $value |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function offsetSet($offset, $value) { |
125
|
|
|
$this->data[$offset] = $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param mixed $offset |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function offsetUnset($offset) { |
133
|
|
|
if($this->offsetExists($offset)) { |
134
|
|
|
unset($this->data[$offset]); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function __toString() { |
142
|
|
|
return (string) call_user_func($this->stringFormatter, $this); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|