This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectStorage.php
145 lines (131 loc) · 2.66 KB
/
ObjectStorage.php
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
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Storage\Component;
use Ulrack\Storage\Common\StorageInterface;
use Ulrack\Storage\Exception\DataNotFoundException;
use IteratorAggregate;
use ArrayIterator;
class ObjectStorage implements IteratorAggregate, StorageInterface
{
/**
* Contains the data of the storage.
*
* @var array
*/
private $data;
/**
* Constructor
*
* @param array $data
*/
public function __construct(array $data = [])
{
$this->data = $data;
}
/**
* Gets data from a specific key within the storage.
*
* @param string|int $key
*
* @return mixed
*/
public function get($key)
{
if ($this->has($key)) {
return $this->data[$key];
}
throw new DataNotFoundException($key);
}
/**
* Sets data on a specific key within the storage.
*
* @param string|int $key
* @param mixed $data
*
* @return void
*/
public function set($key, $data): void
{
$this->data[$key] = $data;
}
/**
* Removes data from a key within a storage.
*
* @param string|int $key
*
* @return void
*/
public function unset($key): void
{
unset($this->data[$key]);
}
/**
* Checks whether the storage has data on this key.
*
* @param string|int $key
*
* @return bool
*/
public function has($key): bool
{
return array_key_exists($key, $this->data);
}
/**
* Retrieves an array of keys from the storage.
*
* @return string[]|int[]
*/
public function keys(): array
{
return array_keys($this->data);
}
/**
* @param string|int $offset
*
* @return bool
*/
public function offsetExists($offset): bool
{
return $this->has($offset);
}
/**
* @param string|int $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* @param string|int $offset
* @param mixed $value
*
* @return void
*/
public function offsetSet($offset, $value) : void
{
$this->set($offset, $value);
}
/**
* @param string|int $offset
*
* @return void
*/
public function offsetUnset($offset) : void
{
$this->unset($offset);
}
/**
* Retrieves the iterator.
*
* @return ArrayIterator
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->data);
}
}