Skip to content

Commit 064b896

Browse files
committed
movve array access interface implementation into a separate class
1 parent d97392f commit 064b896

File tree

2 files changed

+90
-65
lines changed

2 files changed

+90
-65
lines changed

src/AccessibleArray.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* ----------------------------------------------------------------------------
4+
* This code is part of the Sclable Business Application Development Platform
5+
* and is subject to the provisions of your License Agreement with
6+
* Sclable Business Solutions GmbH.
7+
*
8+
* @copyright (c) 2016 Sclable Business Solutions GmbH
9+
* ----------------------------------------------------------------------------
10+
*/
11+
12+
namespace sclable\arrayFunctions;
13+
14+
/**
15+
* Class AccessibleArray
16+
*
17+
*
18+
* @package sclable\arrayFunctions
19+
* @author Michael Rutz <[email protected]>
20+
*
21+
*/
22+
class AccessibleArray implements \ArrayAccess
23+
{
24+
/**
25+
* the raw array data container
26+
* @var array
27+
*/
28+
protected $data;
29+
30+
31+
/**
32+
* ArrayWrap constructor.
33+
* @param array $data
34+
*/
35+
public function __construct($data)
36+
{
37+
if (is_array($data) !== true) {
38+
throw new \InvalidArgumentException('The first argument $data is not an array.');
39+
}
40+
41+
$this->data = $data;
42+
}
43+
44+
/**
45+
* Whether a offset exists
46+
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
47+
* @param mixed $offset An offset to check for.
48+
* @return boolean true on success or false on failure.
49+
* The return value will be casted to boolean if non-boolean was returned.
50+
*/
51+
public function offsetExists($offset)
52+
{
53+
return array_key_exists($offset, $this->data);
54+
}
55+
56+
/**
57+
* Offset to retrieve
58+
* @link http://php.net/manual/en/arrayaccess.offsetget.php
59+
* @param mixed $offset The offset to retrieve.
60+
* @return mixed Can return all value types.
61+
*/
62+
public function offsetGet($offset)
63+
{
64+
return array_key_exists($offset, $this->data) ? $this->data[$offset] : null;
65+
}
66+
67+
/**
68+
* Offset to set
69+
* @link http://php.net/manual/en/arrayaccess.offsetset.php
70+
* @param mixed $offset The offset to assign the value to.
71+
* @param mixed $value The value to set.
72+
* @return void
73+
*/
74+
public function offsetSet($offset, $value)
75+
{
76+
$this->data[$offset] = $value;
77+
}
78+
79+
/**
80+
* Offset to unset
81+
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
82+
* @param mixed $offset The offset to unset.
83+
* @return void
84+
*/
85+
public function offsetUnset($offset)
86+
{
87+
unset($this->data[$offset]);
88+
}
89+
}

src/ArrayWrap.php

+1-65
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@
3434
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
3535
*
3636
*/
37-
class ArrayWrap implements \ArrayAccess
37+
class ArrayWrap extends AccessibleArray
3838
{
39-
/**
40-
* the raw array data container
41-
* @var array
42-
*/
43-
private $data;
44-
4539
/**
4640
* Factory method
4741
*
@@ -71,18 +65,7 @@ public static function range($start, $end)
7165
return static::create(range($start, $end));
7266
}
7367

74-
/**
75-
* ArrayWrap constructor.
76-
* @param array $data
77-
*/
78-
public function __construct($data)
79-
{
80-
if (is_array($data) !== true) {
81-
throw new \InvalidArgumentException('The first argument $data is not an array.');
82-
}
8368

84-
$this->data = $data;
85-
}
8669

8770
/**
8871
* return the raw data array
@@ -104,53 +87,6 @@ protected function reply(array $data)
10487
return static::create($data);
10588
}
10689

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-
15490
/**
15591
* Pad the array to the specified length with a value
15692
* @see array_pad()

0 commit comments

Comments
 (0)