| 1 |
<?php |
| 2 |
/** |
| 3 |
* Multidimensional ArrayAccess |
| 4 |
* |
| 5 |
* Allows ArrayAccess-like functionality with multidimensional arrays. Fully supports |
| 6 |
* both sets and unsets. |
| 7 |
* |
| 8 |
* @package WordPress |
| 9 |
* @subpackage Session |
| 10 |
* @since 3.7.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Recursive array class to allow multidimensional array access. |
| 15 |
* |
| 16 |
* @package WordPress |
| 17 |
* @since 3.7.0 |
| 18 |
*/ |
| 19 |
class Recursive_ArrayAccess implements ArrayAccess, Iterator, Countable { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- This is a bundled library class. Changing it would break existing functionality. |
| 20 |
/** |
| 21 |
* Internal data collection. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $container = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Flag whether or not the internal collection has been changed. |
| 29 |
* |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
protected $dirty = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* Default object constructor. |
| 36 |
* |
| 37 |
* @param array $data |
| 38 |
*/ |
| 39 |
protected function __construct( $data = array() ) { |
| 40 |
foreach ( $data as $key => $value ) { |
| 41 |
$this[ $key ] = $value; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Allow deep copies of objects |
| 47 |
*/ |
| 48 |
public function __clone() { |
| 49 |
foreach ( $this->container as $key => $value ) { |
| 50 |
if ( $value instanceof self ) { |
| 51 |
$this[ $key ] = clone $value; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Output the data container as a multidimensional array. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function toArray() { |
| 62 |
$data = $this->container; |
| 63 |
foreach ( $data as $key => $value ) { |
| 64 |
if ( $value instanceof self ) { |
| 65 |
$data[ $key ] = $value->toArray(); |
| 66 |
} |
| 67 |
} |
| 68 |
return $data; |
| 69 |
} |
| 70 |
|
| 71 |
/*****************************************************************/ |
| 72 |
/* ArrayAccess Implementation */ |
| 73 |
/*****************************************************************/ |
| 74 |
|
| 75 |
/** |
| 76 |
* Whether a offset exists |
| 77 |
* |
| 78 |
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 79 |
* |
| 80 |
* @param mixed $offset An offset to check for. |
| 81 |
* |
| 82 |
* @return boolean true on success or false on failure. |
| 83 |
*/ |
| 84 |
#[\ReturnTypeWillChange] |
| 85 |
public function offsetExists( $offset ) { |
| 86 |
return isset( $this->container[ $offset ]) ; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Offset to retrieve |
| 91 |
* |
| 92 |
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 93 |
* |
| 94 |
* @param mixed $offset The offset to retrieve. |
| 95 |
* |
| 96 |
* @return mixed Can return all value types. |
| 97 |
*/ |
| 98 |
#[\ReturnTypeWillChange] |
| 99 |
public function offsetGet( $offset ) { |
| 100 |
return isset( $this->container[ $offset ] ) ? $this->container[ $offset ] : null; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Offset to set |
| 105 |
* |
| 106 |
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 107 |
* |
| 108 |
* @param mixed $offset The offset to assign the value to. |
| 109 |
* @param mixed $value The value to set. |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
#[\ReturnTypeWillChange] |
| 114 |
public function offsetSet( $offset, $data ) { |
| 115 |
if ( is_array( $data ) ) { |
| 116 |
$data = new self( $data ); |
| 117 |
} |
| 118 |
if ( $offset === null ) { // don't forget this! |
| 119 |
$this->container[] = $data; |
| 120 |
} else { |
| 121 |
$this->container[ $offset ] = $data; |
| 122 |
} |
| 123 |
|
| 124 |
$this->dirty = true; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Offset to unset |
| 129 |
* |
| 130 |
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 131 |
* |
| 132 |
* @param mixed $offset The offset to unset. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
#[\ReturnTypeWillChange] |
| 137 |
public function offsetUnset( $offset ) { |
| 138 |
unset( $this->container[ $offset ] ); |
| 139 |
|
| 140 |
$this->dirty = true; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/*****************************************************************/ |
| 145 |
/* Iterator Implementation */ |
| 146 |
/*****************************************************************/ |
| 147 |
|
| 148 |
/** |
| 149 |
* Current position of the array. |
| 150 |
* |
| 151 |
* @link http://php.net/manual/en/iterator.current.php |
| 152 |
* |
| 153 |
* @return mixed |
| 154 |
*/ |
| 155 |
#[\ReturnTypeWillChange] |
| 156 |
public function current() { |
| 157 |
return current( $this->container ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Key of the current element. |
| 162 |
* |
| 163 |
* @link http://php.net/manual/en/iterator.key.php |
| 164 |
* |
| 165 |
* @return mixed |
| 166 |
*/ |
| 167 |
#[\ReturnTypeWillChange] |
| 168 |
public function key() { |
| 169 |
return key( $this->container ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Move the internal point of the container array to the next item |
| 174 |
* |
| 175 |
* @link http://php.net/manual/en/iterator.next.php |
| 176 |
* |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
#[\ReturnTypeWillChange] |
| 180 |
public function next() { |
| 181 |
next( $this->container ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Rewind the internal point of the container array. |
| 186 |
* |
| 187 |
* @link http://php.net/manual/en/iterator.rewind.php |
| 188 |
* |
| 189 |
* @return void |
| 190 |
*/ |
| 191 |
#[\ReturnTypeWillChange] |
| 192 |
public function rewind() { |
| 193 |
reset( $this->container ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Is the current key valid? |
| 198 |
* |
| 199 |
* @link http://php.net/manual/en/iterator.rewind.php |
| 200 |
* |
| 201 |
* @return bool |
| 202 |
*/ |
| 203 |
#[\ReturnTypeWillChange] |
| 204 |
public function valid() { |
| 205 |
return $this->offsetExists( $this->key() ); |
| 206 |
} |
| 207 |
|
| 208 |
/*****************************************************************/ |
| 209 |
/* Countable Implementation */ |
| 210 |
/*****************************************************************/ |
| 211 |
|
| 212 |
/** |
| 213 |
* Get the count of elements in the container array. |
| 214 |
* |
| 215 |
* @link http://php.net/manual/en/countable.count.php |
| 216 |
* |
| 217 |
* @return int |
| 218 |
*/ |
| 219 |
#[\ReturnTypeWillChange] |
| 220 |
public function count() { |
| 221 |
return count( $this->container ); |
| 222 |
} |
| 223 |
} |
| 224 |
|