|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Gorlum 17.08.2016 23:08 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Common; |
|
7
|
|
|
|
|
8
|
|
|
use \SplObjectStorage; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class IndexedObjectStorage |
|
12
|
|
|
* @package Common |
|
13
|
|
|
*/ |
|
14
|
|
|
class IndexedObjectStorage extends \SplObjectStorage { |
|
15
|
|
|
|
|
16
|
|
|
protected $index = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Unassign index from object |
|
20
|
|
|
* |
|
21
|
|
|
* @param object $object |
|
22
|
|
|
*/ |
|
23
|
1 |
|
protected function indexUnset($object) { |
|
24
|
1 |
|
if (($oldData = SplObjectStorage::offsetGet($object)) !== null) { |
|
25
|
1 |
|
unset($this->index[$oldData]); |
|
26
|
1 |
|
} |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Function to handle assigning keys |
|
31
|
|
|
* |
|
32
|
|
|
* Primary for dealing with duplicates - child can overwrite function to handle duplicates by itself |
|
33
|
|
|
* |
|
34
|
|
|
* @param object $object |
|
35
|
|
|
* @param mixed|null $data - null means remove index |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \Exception |
|
38
|
|
|
*/ |
|
39
|
2 |
|
protected function indexSet($object, $data = null) { |
|
40
|
|
|
// When calling indexSet - $object already SHOULD not have index associated in this concrete implementation |
|
41
|
|
|
|
|
42
|
|
|
// Checking if index free. NULL index is always free |
|
43
|
2 |
|
if (isset($this->index[$data])) { |
|
44
|
1 |
|
throw new \Exception('Duplicate index [' . $data . '] in ' . __CLASS__); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
if ($data !== null) { |
|
48
|
|
|
// Assigning index |
|
49
|
2 |
|
$this->index[$data] = $object; |
|
50
|
2 |
|
} |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param mixed $index |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed|null |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function indexGetObject($index) { |
|
59
|
1 |
|
return array_key_exists($index, $this->index) ? $this->index[$index] : null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Rebuild index |
|
64
|
|
|
*/ |
|
65
|
2 |
|
protected function indexRebuild() { |
|
66
|
2 |
|
$this->index = array(); |
|
67
|
2 |
|
$this->rewind(); |
|
68
|
2 |
|
while ($this->valid()) { |
|
69
|
2 |
|
$this->indexSet($this->current(), $this->getInfo()); |
|
70
|
2 |
|
$this->next(); |
|
71
|
2 |
|
} |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function attach($object, $data = null) { |
|
75
|
|
|
// If $object in storage - removing it's index from $indexes |
|
76
|
1 |
|
if ($this->contains($object)) { |
|
77
|
1 |
|
$this->indexUnset($object); |
|
78
|
1 |
|
} |
|
79
|
1 |
|
$this->indexSet($object, $data); |
|
80
|
1 |
|
parent::attach($object, $data); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function offsetSet($object, $data = null) { |
|
84
|
1 |
|
$this->attach($object, $data); |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
1 |
|
public function detach($object) { |
|
89
|
1 |
|
$this->indexUnset($object); |
|
90
|
1 |
|
parent::detach($object); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function offsetUnset($object) { |
|
94
|
1 |
|
$this->detach($object); |
|
95
|
1 |
|
parent::offsetUnset($object); |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
1 |
|
public function addAll($storage) { |
|
100
|
|
|
// Adding new elements from storage |
|
101
|
1 |
|
parent::addAll($storage); |
|
102
|
|
|
// Original addAll overwrites indexes with those in input $storage - so we must refresh current indexes |
|
103
|
1 |
|
$this->indexRebuild(); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
public function removeAll($storage) { |
|
107
|
1 |
|
parent::removeAll($storage); |
|
108
|
1 |
|
$this->indexRebuild(); |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public function removeAllExcept($storage) { |
|
112
|
1 |
|
parent::removeAllExcept($storage); |
|
113
|
|
|
|
|
114
|
1 |
|
$this->indexRebuild(); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
public function unserialize($serialized) { |
|
118
|
|
|
// Unserialize DOES NOT removes existing objects AND creates object copies making duplicates |
|
119
|
|
|
// So be careful |
|
120
|
1 |
|
parent::unserialize($serialized); |
|
121
|
|
|
|
|
122
|
1 |
|
$this->indexRebuild(); |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
public function setInfo($data) { |
|
126
|
1 |
|
if ($this->valid()) { |
|
127
|
1 |
|
$this->indexUnset($this->current()); |
|
128
|
1 |
|
$this->indexSet($this->current(), $data); |
|
129
|
1 |
|
} |
|
130
|
|
|
// Changing data in storage |
|
131
|
1 |
|
parent::setInfo($data); |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|