Skip to content

Commit d97392f

Browse files
committed
implement array access interface
1 parent a6c5f42 commit d97392f

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

src/ArrayWrap.php

+49-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* // Number: 0
2525
* ```
2626
*
27-
* @todo implement Iterator, Countable, ArrayAccess interfaces
27+
* @todo implement Iterator, Countable interfaces
2828
*
2929
* @package sclable\arrayFunctions
3030
* @author Michael Rutz <[email protected]>
@@ -34,7 +34,7 @@
3434
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
3535
*
3636
*/
37-
class ArrayWrap
37+
class ArrayWrap implements \ArrayAccess
3838
{
3939
/**
4040
* the raw array data container
@@ -104,6 +104,53 @@ protected function reply(array $data)
104104
return static::create($data);
105105
}
106106

107+
/**
108+
* Whether a offset exists
109+
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
110+
* @param mixed $offset An offset to check for.
111+
* @return boolean true on success or false on failure.
112+
* The return value will be casted to boolean if non-boolean was returned.
113+
*/
114+
public function offsetExists($offset)
115+
{
116+
return $this->hasKey($offset);
117+
}
118+
119+
/**
120+
* Offset to retrieve
121+
* @link http://php.net/manual/en/arrayaccess.offsetget.php
122+
* @param mixed $offset The offset to retrieve.
123+
* @return mixed Can return all value types.
124+
*/
125+
public function offsetGet($offset)
126+
{
127+
return $this->hasKey($offset) ? $this->data[$offset] : null;
128+
}
129+
130+
/**
131+
* Offset to set
132+
* @link http://php.net/manual/en/arrayaccess.offsetset.php
133+
* @param mixed $offset The offset to assign the value to.
134+
* @param mixed $value The value to set.
135+
* @return void
136+
*/
137+
public function offsetSet($offset, $value)
138+
{
139+
$this->data[$offset] = $value;
140+
}
141+
142+
/**
143+
* Offset to unset
144+
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
145+
* @param mixed $offset The offset to unset.
146+
* @return void
147+
*/
148+
public function offsetUnset($offset)
149+
{
150+
unset($this->data[$offset]);
151+
}
152+
153+
107154
/**
108155
* Pad the array to the specified length with a value
109156
* @see array_pad()

0 commit comments

Comments
 (0)