1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPCollections; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
use ArrayIterator; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A class for storing and managing data. |
13
|
|
|
*/ |
14
|
|
|
class DataHolder implements ArrayAccess, IteratorAggregate |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The array for storing data. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $container; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Initializes the container property. |
25
|
|
|
* |
26
|
|
|
* @param array $data |
27
|
|
|
*/ |
28
|
80 |
|
public function __construct(array $data = []) |
29
|
|
|
{ |
30
|
80 |
|
$this->container = $data; |
31
|
80 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns the container array. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
51 |
|
public function getContainer(): array |
39
|
|
|
{ |
40
|
51 |
|
return $this->container; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns an array iterator for |
45
|
|
|
* the container property. |
46
|
|
|
* |
47
|
|
|
* @return \ArrayIterator |
48
|
|
|
*/ |
49
|
24 |
|
public function getIterator(): ArrayIterator |
50
|
|
|
{ |
51
|
24 |
|
return new ArrayIterator($this->container); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks if an offset exists in the container. |
56
|
|
|
* |
57
|
|
|
* @param mixed $offset |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
23 |
|
public function offsetExists($offset): bool |
62
|
|
|
{ |
63
|
23 |
|
return isset($this->container[$offset]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets a value from the container. |
68
|
|
|
* |
69
|
|
|
* @param mixed $offset |
70
|
|
|
* |
71
|
|
|
* @return mixed|null |
72
|
|
|
*/ |
73
|
21 |
|
public function offsetGet($offset) |
74
|
|
|
{ |
75
|
21 |
|
return $this->container[$offset] ?? null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets a value into the container. |
80
|
|
|
* |
81
|
|
|
* @param mixed $offset |
82
|
|
|
* @param mixed $value |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
30 |
|
public function offsetSet($offset, $value): void |
87
|
|
|
{ |
88
|
30 |
|
$this->container[$offset] = $value; |
89
|
30 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Unsets an offset from the container. |
93
|
|
|
* |
94
|
|
|
* @param mixed $offset |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
4 |
|
public function offsetUnset($offset): void |
99
|
|
|
{ |
100
|
4 |
|
unset($this->container[$offset]); |
101
|
4 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets the container array. |
105
|
|
|
* |
106
|
|
|
* @param array $data |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
51 |
|
public function setContainer(array $data): void |
111
|
|
|
{ |
112
|
51 |
|
$this->container = $data; |
113
|
51 |
|
} |
114
|
|
|
} |
115
|
|
|
|