|
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
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Starting values of properties |
|
17
|
|
|
* |
|
18
|
|
|
* @var array $_startValues |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $_startValues = []; |
|
21
|
|
|
/** |
|
22
|
|
|
* Changed values |
|
23
|
|
|
* |
|
24
|
|
|
* @var array $_changes |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_changes = []; |
|
27
|
|
|
/** |
|
28
|
|
|
* Increment/Decrement results AKA deltas |
|
29
|
|
|
* |
|
30
|
|
|
* @var array $_deltas |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $_deltas = []; |
|
33
|
|
|
|
|
34
|
|
|
public function __set($name, $value) { |
|
35
|
|
|
if (array_key_exists($name, $this->values)) { |
|
36
|
|
|
if(array_key_exists($name, $this->_deltas)) { |
|
37
|
|
|
throw new \Exception(get_called_class() . '::' . $name . ' already INCREMENTED/DECREMENTED - can not CHANGE', ERR_ERROR); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->_changes[$name] = $value; |
|
41
|
|
|
} else { |
|
42
|
|
|
$this->_startValues[$name] = $value; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
parent::__set($name, $value); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Increments field by value |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @param int|float $value Default: 1 |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function inc($name, $value = 1) { |
|
|
|
|
|
|
55
|
|
|
if(array_key_exists($name, $this->_changes)) { |
|
56
|
|
|
throw new \Exception(get_called_class() . '::' . $name . ' already changed - can not use INCREMENT', ERR_ERROR); |
|
57
|
|
|
} |
|
58
|
|
|
$this->_deltas[$name] += $value; |
|
59
|
|
|
parent::__set($name, parent::__get($name) + $value); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Decrements field by value |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @param int|float $value Default: 1 |
|
67
|
|
|
*/ |
|
68
|
|
View Code Duplication |
public function dec($name, $value = 1) { |
|
|
|
|
|
|
69
|
|
|
if(array_key_exists($name, $this->_changes)) { |
|
70
|
|
|
throw new \Exception(get_called_class() . '::' . $name . ' already changed - can not use DECREMENT', ERR_ERROR); |
|
71
|
|
|
} |
|
72
|
|
|
$this->_deltas[$name] -= $value; |
|
73
|
|
|
parent::__set($name, parent::__get($name) - $value); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getChanges() { |
|
77
|
|
|
return $this->_changes; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getDeltas() { |
|
81
|
|
|
return $this->_deltas; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Flushes changes |
|
86
|
|
|
* |
|
87
|
|
|
* Makes current values a start one and resets changes/deltas |
|
88
|
|
|
*/ |
|
89
|
|
|
public function flush() { |
|
90
|
|
|
$this->_startValues = $this->values; |
|
91
|
|
|
$this->_changes = []; |
|
92
|
|
|
$this->_deltas = []; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function clear() { |
|
96
|
|
|
parent::clear(); |
|
97
|
|
|
$this->flush(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.