1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 16.06.2017 14:32 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Common; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AccessLogged |
11
|
|
|
* @package Common |
12
|
|
|
*/ |
13
|
|
|
class AccessLogged extends AccessMagic { |
14
|
|
|
const ACCESS_SET = null; |
15
|
|
|
const ACCESS_DELTA_INC = +1; |
16
|
|
|
const ACCESS_DELTA_DEC = -1; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Starting values of properties |
20
|
|
|
* |
21
|
|
|
* @var array $_startValues |
22
|
|
|
*/ |
23
|
|
|
protected $_startValues = []; |
24
|
|
|
/** |
25
|
|
|
* Changed values |
26
|
|
|
* |
27
|
|
|
* @var array $_changes |
28
|
|
|
*/ |
29
|
|
|
protected $_changes = []; |
30
|
|
|
/** |
31
|
|
|
* Increment/Decrement results AKA deltas |
32
|
|
|
* |
33
|
|
|
* @var array $_deltas |
34
|
|
|
*/ |
35
|
|
|
protected $_deltas = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Stored delta value which would be applied on next __set() call |
39
|
|
|
* |
40
|
|
|
* @var int|float $_currentDelta |
41
|
|
|
*/ |
42
|
|
|
protected $_currentDelta = self::ACCESS_SET; |
43
|
|
|
|
44
|
1 |
|
protected function blockChange($name) { |
45
|
1 |
|
if (array_key_exists($name, $this->_deltas)) { |
46
|
1 |
|
throw new \Exception(get_called_class() . '::' . $name . ' already INCREMENTED/DECREMENTED - can not CHANGE', ERR_ERROR); |
47
|
|
|
} |
48
|
1 |
|
} |
49
|
|
|
|
50
|
1 |
|
protected function blockDelta($name) { |
51
|
1 |
|
if (array_key_exists($name, $this->_changes)) { |
52
|
1 |
|
throw new \Exception(get_called_class() . '::' . $name . ' already changed - can not use DELTA', ERR_ERROR); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
2 |
|
protected function valueSet($name, $value) { |
57
|
2 |
|
if ($this->__isset($name)) { |
58
|
2 |
|
$this->blockChange($name); |
59
|
|
|
|
60
|
2 |
|
$this->_changes[$name] = $value; |
61
|
2 |
|
} else { |
62
|
2 |
|
$this->_startValues[$name] = $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
parent::__set($name, $value); |
66
|
2 |
|
} |
67
|
|
|
|
68
|
1 |
|
protected function valueDelta($name, $value) { |
69
|
1 |
|
$this->blockDelta($name); |
70
|
|
|
|
71
|
1 |
|
!isset($this->_deltas[$name]) ? $this->_deltas[$name] = 0 : false; |
72
|
1 |
|
!isset($this->_startValues[$name]) ? $this->_startValues[$name] = 0 : false; |
73
|
|
|
|
74
|
1 |
|
$value *= $this->_currentDelta === self::ACCESS_DELTA_DEC ? -1 : +1; |
75
|
|
|
|
76
|
1 |
|
$this->_deltas[$name] += $value; |
77
|
|
|
|
78
|
1 |
|
parent::__set($name, parent::__get($name) + $value); |
79
|
|
|
|
80
|
1 |
|
$this->_currentDelta = self::ACCESS_SET; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
2 |
|
public function __set($name, $value) { |
84
|
2 |
|
if ($this->_currentDelta === self::ACCESS_SET) { |
85
|
2 |
|
$this->valueSet($name, $value); |
86
|
2 |
|
} else { |
87
|
2 |
|
$this->valueDelta($name, $value); |
88
|
|
|
} |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
// /** |
92
|
|
|
// * Changes field by value |
93
|
|
|
// * |
94
|
|
|
// * Changes only $values and $_deltas |
95
|
|
|
// * |
96
|
|
|
// * @param string $name |
97
|
|
|
// * @param int|float $value Default: 1 |
98
|
|
|
// */ |
99
|
|
|
// public function delta($name, $value = 1) { |
100
|
|
|
// $this->valueDelta($name, $value); |
101
|
|
|
// } |
102
|
|
|
// |
103
|
|
|
// /** |
104
|
|
|
// * Increments property by value |
105
|
|
|
// * |
106
|
|
|
// * Changes only $values and $_deltas |
107
|
|
|
// * |
108
|
|
|
// * @param string $name |
109
|
|
|
// * @param int|float $value Default: 1 |
110
|
|
|
// */ |
111
|
|
|
// public function inc($name, $value = 1) { |
112
|
|
|
// $this->delta($name, +$value); |
113
|
|
|
// } |
114
|
|
|
// |
115
|
|
|
// /** |
116
|
|
|
// * Decrements property by value |
117
|
|
|
// * |
118
|
|
|
// * Changes only $values and $_deltas |
119
|
|
|
// * |
120
|
|
|
// * @param string $name |
121
|
|
|
// * @param int|float $value Default: 1 |
122
|
|
|
// */ |
123
|
|
|
// public function dec($name, $value = 1) { |
124
|
|
|
// $this->delta($name, -$value); |
125
|
|
|
// } |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Mark next set operation as delta increment |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
1 |
|
public function inc() { |
133
|
1 |
|
$this->_currentDelta = self::ACCESS_DELTA_INC; |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Mark next set operation as delta decrement |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
1 |
|
public function dec() { |
144
|
1 |
|
$this->_currentDelta = self::ACCESS_DELTA_DEC; |
145
|
|
|
|
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
public function getChanges() { |
150
|
1 |
|
return $this->_changes; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
public function getDeltas() { |
154
|
1 |
|
return $this->_deltas; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Accepts changes |
159
|
|
|
* |
160
|
|
|
* Makes current values a start one and resets changes/deltas |
161
|
|
|
*/ |
162
|
1 |
|
public function acceptChanges() { |
163
|
1 |
|
$this->_startValues = $this->values; |
164
|
1 |
|
$this->_changes = []; |
165
|
1 |
|
$this->_deltas = []; |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Rolls changes back |
170
|
|
|
*/ |
171
|
|
|
public function rollback() { |
172
|
|
|
$this->values = $this->_startValues; |
173
|
|
|
$this->_changes = []; |
174
|
|
|
$this->_deltas = []; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Is container was changed? |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function isChanged() { |
183
|
|
|
return empty($this->_changes) && empty($this->_deltas); |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
public function clear() { |
187
|
1 |
|
parent::clear(); |
188
|
1 |
|
$this->acceptChanges(); |
189
|
1 |
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|