-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectStorage.php
More file actions
186 lines (162 loc) · 4.13 KB
/
ObjectStorage.php
File metadata and controls
186 lines (162 loc) · 4.13 KB
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
<?php
declare(strict_types=1);
namespace Brick\Std;
/**
* Provides a map from objects to data.
*
* This class is iterable, with objects as keys and data as values.
*
* In this respect, this class is different from the SplObjectStorage class,
* which exhibits a different behaviour due to backwards compatibility reasons.
*/
class ObjectStorage implements \Countable, \IteratorAggregate, \ArrayAccess
{
/**
* The objects contained in the storage, indexed by object hash.
*
* @var array
*/
private $objects = [];
/**
* The data in the storage, indexed by object hash.
*
* @var array
*/
private $data = [];
/**
* Returns whether this storage contains the given object.
*
* @param object $object The object to test.
*
* @return bool True if this storage contains the object, false otherwise.
*/
public function has($object) : bool
{
$hash = spl_object_hash($object);
return isset($this->objects[$hash]);
}
/**
* Returns the data associated to the given object.
*
* If the given object is not in the storage, or has no associated data, NULL is returned.
*
* @param object $object The object.
*
* @return mixed The stored data.
*/
public function get($object)
{
$hash = spl_object_hash($object);
return isset($this->data[$hash]) ? $this->data[$hash] : null;
}
/**
* Stores an object with associated data.
*
* @param object $object The object.
* @param mixed $data The data to store.
*
* @return void
*/
public function set($object, $data = null) : void
{
$hash = spl_object_hash($object);
$this->objects[$hash] = $object;
$this->data[$hash] = $data;
}
/**
* Removes the given object from this storage, along with associated data.
*
* If this storage does not contain the given object, this method does nothing.
*
* @param object $object The object to remove.
*
* @return void
*/
public function remove($object) : void
{
$hash = spl_object_hash($object);
unset($this->objects[$hash]);
unset($this->data[$hash]);
}
/**
* Returns the number of objects in this storage.
*
* This method is part of the Countable interface.
*
* @return int
*/
public function count() : int
{
return count($this->objects);
}
/**
* Returns the objects contained in this storage.
*
* @return object[]
*/
public function getObjects() : array
{
return array_values($this->objects);
}
/**
* Returns an iterator for this storage.
*
* This method is part of the IteratorAggregate interface.
*
* @return \Traversable
*/
public function getIterator() : \Traversable
{
foreach ($this->objects as $hash => $object) {
yield $object => $this->data[$hash];
}
}
/**
* @param object $object
*
* @return mixed
*
* @throws \UnexpectedValueException If the object cannot be found.
*/
public function offsetGet($object)
{
$hash = spl_object_hash($object);
if (! isset($this->objects[$hash])) {
throw new \UnexpectedValueException('Object not found.');
}
return $this->data[$hash];
}
/**
* @param object $object
* @param mixed $value
*
* @return void
*/
public function offsetSet($object, $value) : void
{
$hash = spl_object_hash($object);
$this->objects[$hash] = $object;
$this->data[$hash] = $value;
}
/**
* @param object $object
*
* @return void
*/
public function offsetUnset($object) : void
{
$hash = spl_object_hash($object);
unset($this->objects[$hash]);
unset($this->data[$hash]);
}
/**
* @param object $object
*
* @return bool
*/
public function offsetExists($object) : bool
{
$hash = spl_object_hash($object);
return isset($this->objects[$hash]);
}
}