Innmind /
Immutable
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace Innmind\Immutable; |
||
| 5 | |||
| 6 | use Innmind\Immutable\Exception\LogicException; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @deprecated To be removed in 2.0 |
||
| 10 | */ |
||
| 11 | class ObjectStorage implements PrimitiveInterface, \Countable, \Iterator, \ArrayAccess, \Serializable |
||
| 12 | { |
||
| 13 | private $objects; |
||
| 14 | |||
| 15 | 19 | public function __construct(\SplObjectStorage $objects = null) |
|
| 16 | { |
||
| 17 | 19 | $this->objects = $objects ? clone $objects : new \SplObjectStorage; |
|
| 18 | 19 | $this->objects->rewind(); |
|
| 19 | 19 | } |
|
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | 4 | public function toPrimitive(): \SplObjectStorage |
|
| 25 | { |
||
| 26 | //so the inner SplObjectStorage object can't be modified |
||
| 27 | 4 | return clone $this->objects; |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Merge storages |
||
| 32 | * |
||
| 33 | * @param self $storage |
||
| 34 | * |
||
| 35 | * @return self |
||
| 36 | */ |
||
| 37 | 1 | public function merge(self $storage): self |
|
| 38 | { |
||
| 39 | 1 | $objects = new \SplObjectStorage; |
|
| 40 | 1 | $objects->addAll($this->objects); |
|
| 41 | 1 | $objects->addAll($storage->objects); |
|
| 42 | |||
| 43 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
|
|||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Attach a new element to the storage |
||
| 48 | * |
||
| 49 | * @param object $object |
||
| 50 | * @param mixed $data |
||
| 51 | * |
||
| 52 | * @return self |
||
| 53 | */ |
||
| 54 | 16 | public function attach($object, $data = null): self |
|
| 55 | { |
||
| 56 | 16 | $objects = clone $this->objects; |
|
| 57 | 16 | $objects->attach($object, $data); |
|
| 58 | |||
| 59 | 16 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Check if the object is in this storage |
||
| 64 | * |
||
| 65 | * @param object $object |
||
| 66 | * |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | 4 | public function contains($object): bool |
|
| 70 | { |
||
| 71 | 4 | return $this->objects->contains($object); |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Remove the given object from the storage |
||
| 76 | * |
||
| 77 | * @param object $object |
||
| 78 | * |
||
| 79 | * @return self |
||
| 80 | */ |
||
| 81 | 1 | public function detach($object): self |
|
| 82 | { |
||
| 83 | 1 | $objects = clone $this->objects; |
|
| 84 | 1 | $objects->detach($object); |
|
| 85 | |||
| 86 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the internal has for the given object |
||
| 91 | * |
||
| 92 | * @param object $object |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | 1 | public function getHash($object): string |
|
| 97 | { |
||
| 98 | 1 | return $this->objects->getHash($object); |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Return the info associated to the current object pointed by the internal pointer |
||
| 103 | * |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | 1 | public function getInfo() |
|
| 107 | { |
||
| 108 | 1 | return $this->objects->getInfo(); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Remove all the elements contained in the given storage from the current one |
||
| 113 | * |
||
| 114 | * @param self $storage |
||
| 115 | * |
||
| 116 | * @return self |
||
| 117 | */ |
||
| 118 | 1 | public function removeAll(self $storage): self |
|
| 119 | { |
||
| 120 | 1 | $objects = clone $this->objects; |
|
| 121 | 1 | $objects->removeAll($storage->objects); |
|
| 122 | |||
| 123 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Remove all elements not contained in the given storage |
||
| 128 | * |
||
| 129 | * @param self $storage |
||
| 130 | * |
||
| 131 | * @return self |
||
| 132 | */ |
||
| 133 | 1 | public function removeAllExcept(self $storage): self |
|
| 134 | { |
||
| 135 | 1 | $objects = clone $this->objects; |
|
| 136 | 1 | $objects->removeAllExcept($storage->objects); |
|
| 137 | |||
| 138 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Associate data to the current object |
||
| 143 | * |
||
| 144 | * @param mixed $data |
||
| 145 | * |
||
| 146 | * @return self |
||
| 147 | */ |
||
| 148 | 1 | public function setInfo($data): self |
|
| 149 | { |
||
| 150 | 1 | $current = $this->objects->current(); |
|
| 151 | 1 | $objects = clone $this->objects; |
|
| 152 | 1 | $objects[$current] = $data; |
|
| 153 | |||
| 154 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | 9 | public function count(): int |
|
| 161 | { |
||
| 162 | 9 | return $this->objects->count(); |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | 4 | public function current() |
|
| 169 | { |
||
| 170 | 4 | return $this->objects->current(); |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | 3 | public function next() |
|
| 177 | { |
||
| 178 | 3 | $this->objects->next(); |
|
| 179 | 3 | } |
|
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | 1 | public function key() |
|
| 185 | { |
||
| 186 | 1 | return $this->objects->key(); |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | 4 | public function rewind() |
|
| 193 | { |
||
| 194 | 4 | $this->objects->rewind(); |
|
| 195 | 4 | } |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | 1 | public function valid(): bool |
|
| 201 | { |
||
| 202 | 1 | return $this->objects->valid(); |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | 1 | public function offsetExists($object): bool |
|
| 209 | { |
||
| 210 | 1 | return $this->objects->offsetExists($object); |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | 1 | public function offsetGet($object) |
|
| 217 | { |
||
| 218 | 1 | return $this->objects->offsetGet($object); |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | 1 | public function offsetSet($object, $data = null) |
|
| 225 | { |
||
| 226 | 1 | throw new LogicException( |
|
| 227 | 1 | 'You can\'t modify an immutable object storage' |
|
| 228 | ); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | 1 | public function offsetUnset($object) |
|
| 235 | { |
||
| 236 | 1 | throw new LogicException( |
|
| 237 | 1 | 'You can\'t modify an immutable object storage' |
|
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | 1 | public function serialize(): string |
|
| 245 | { |
||
| 246 | 1 | return $this->objects->serialize(); |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | 1 | public function unserialize($serialized): self |
|
| 253 | { |
||
| 254 | 1 | $objects = clone $this->objects; |
|
| 255 | 1 | $objects->unserialize($serialized); |
|
| 256 | |||
| 257 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Apply the given filter on the collection |
||
| 262 | * |
||
| 263 | * @param callable $filterer |
||
| 264 | * |
||
| 265 | * @return self |
||
| 266 | */ |
||
| 267 | 1 | public function filter(callable $filterer): self |
|
| 268 | { |
||
| 269 | 1 | $objects = new \SplObjectStorage; |
|
| 270 | |||
| 271 | 1 | foreach ($this->objects as $object) { |
|
| 272 | 1 | $data = $this->objects[$object]; |
|
| 273 | |||
| 274 | 1 | if ($filterer($object, $data) === true) { |
|
| 275 | 1 | $objects->attach($object, $data); |
|
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | 1 | $this->rewind(); |
|
| 280 | |||
| 281 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Run the given closure on each element |
||
| 286 | * |
||
| 287 | * @param callable $callback |
||
| 288 | * |
||
| 289 | * @return self |
||
| 290 | */ |
||
| 291 | 1 | public function each(callable $callback): self |
|
| 292 | { |
||
| 293 | 1 | foreach ($this->objects as $object) { |
|
| 294 | 1 | $callback($object, $this->objects[$object]); |
|
| 295 | } |
||
| 296 | |||
| 297 | 1 | $this->rewind(); |
|
| 298 | |||
| 299 | 1 | return $this; |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Generate a new storage based on the given mapper |
||
| 304 | * |
||
| 305 | * @param callable $mapper |
||
| 306 | * |
||
| 307 | * @return self |
||
| 308 | */ |
||
| 309 | 1 | public function map(callable $mapper): self |
|
| 310 | { |
||
| 311 | 1 | $objects = new \SplObjectStorage; |
|
| 312 | |||
| 313 | 1 | foreach ($this->objects as $object) { |
|
| 314 | 1 | $objects->attach( |
|
| 315 | 1 | $mapper($object, $this->objects[$object]) |
|
| 316 | ); |
||
| 317 | } |
||
| 318 | |||
| 319 | 1 | $this->rewind(); |
|
| 320 | |||
| 321 | 1 | return new self($objects); |
|
|
0 ignored issues
–
show
The class
Innmind\Immutable\ObjectStorage has been deprecated with message: To be removed in 2.0
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 322 | } |
||
| 323 | } |
||
| 324 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.