/
AbstractSnapshot.php
189 lines (161 loc) · 4.8 KB
/
AbstractSnapshot.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* This file is part of the Totem package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright Baptiste Clavié <clavie.b@gmail.com>
* @license http://www.opensource.org/licenses/MIT-License MIT License
*/
namespace Totem;
use ArrayAccess,
ReflectionClass,
BadMethodCallException,
InvalidArgumentException;
use Totem\Exception\IncomparableDataException;
/**
* Base class for a Snapshot
*
* Represent the data fixed at a given time
*
* @author Baptiste Clavié <clavie.b@gmail.com>
*/
abstract class AbstractSnapshot implements ArrayAccess
{
/** @var array data stored in an array */
protected $data = [];
/** @var mixed raw data stored */
protected $raw;
/** @var string Set class to use */
private $setClass = 'Totem\\Set';
public function setSetClass($class)
{
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable() || !$reflection->implementsInterface('Totem\\SetInterface')) {
throw new InvalidArgumentException('A Set Class should be instantiable and implement Totem\\SetInterface');
}
$this->setClass = $class;
}
/**
* Calculate the diff between two snapshots
*
* @param AbstractSnapshot $snapshot Snapshot to compare this one to
* @param mixed $id Compare the old and new entries by a given key
*
* @return Set Changeset between the two snapshots
* @throws IncomparableDataException If the two snapshots are not comparable
*/
public function diff(AbstractSnapshot $snapshot, $id = null)
{
if (!$this->isComparable($snapshot)) {
throw new IncomparableDataException;
}
$set = new $this->setClass;
$set->compute($this, $snapshot, $id);
return $set;
}
/**
* Get the raw data fed to this snapshot
*
* @return mixed
*/
final public function getRawData()
{
return $this->raw;
}
/**
* Get the computed data, transformed into an array by this constructor
*
* @return SnapshotInterface[]
* @throws InvalidArgumentException If the data is not an array
*/
final public function getComparableData()
{
if (!is_array($this->data)) {
throw new InvalidArgumentException('The computed data is not an array, "' . gettype($this->data) . '" given');
}
return $this->data;
}
/**
* Returns the keys of the data
*
* @return array
* @throws InvalidArgumentException If the frozen data is not an array
*/
final public function getDataKeys()
{
return array_keys($this->getComparableData());
}
/**
* Clone this object
*
* @codeCoverageIgnore
*/
final private function __clone()
{
}
/** {@inheritDoc} */
final public function offsetExists($offset)
{
return in_array($offset, $this->getDataKeys());
}
/** {@inheritDoc} */
final public function offsetGet($offset)
{
return $this->getComparableData()[$offset];
}
/**
* {@inheritDoc}
*
* @throws BadMethodCallException if a unset is tried on a snapshot property
*/
final public function offsetSet($offset, $value)
{
throw new BadMethodCallException('A snapshot is frozen by nature');
}
/**
* {@inheritDoc}
*
* @throws BadMethodCallException if a unset is tried on a snapshot property
*/
final public function offsetUnset($offset)
{
throw new BadMethodCallException('A snapshot is frozen by nature');
}
/**
* Check if the two snapshots are comparable
*
* @param AbstractSnapshot $snapshot Snapshot to be compared with
*
* @return boolean true if the two snapshots can be processed in a diff, false otherwise
*/
public function isComparable(AbstractSnapshot $snapshot)
{
if (!$snapshot instanceof static) {
return false;
}
return gettype($snapshot->raw) === gettype($this->raw);
}
/**
* Finish data initialization
*
* To be called by child classes *after* the data has been initialized
*/
protected function normalize()
{
if (!is_array($this->data)) {
throw new InvalidArgumentException('The computed data is not an array, "' . gettype($this->data) . '" given');
}
foreach ($this->data as &$value) {
switch (gettype($value)) {
case 'object':
$value = new Snapshot\ObjectSnapshot($value);
break;
case 'array':
$value = new Snapshot\ArraySnapshot($value);
break;
}
}
}
}