| | 1 | <?php |
| | 2 | /** |
| | 3 | * Utility class for implementing array access: WP_Array_Object class. |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage WP_Array_Object |
| | 7 | * @since 4.7.0 |
| | 8 | */ |
| | 9 | |
| | 10 | class WP_Array_Object implements Iterator, ArrayAccess { |
| | 11 | private $data = array(); |
| | 12 | |
| | 13 | /** |
| | 14 | * Converts passed data to an array. |
| | 15 | * @param String|stdClass|Array $data Data to be made available both using array and class methods. |
| | 16 | */ |
| | 17 | function __construct( $data ) { |
| | 18 | if ( is_object( $data ) ) { |
| | 19 | $r = get_object_vars( $data ); |
| | 20 | } elseif ( is_string( $data ) ) { |
| | 21 | wp_parse_str( $data, $r ); |
| | 22 | } else { |
| | 23 | $r = (array) $data; |
| | 24 | } |
| | 25 | |
| | 26 | $this->data = $r; |
| | 27 | } |
| | 28 | |
| | 29 | /** |
| | 30 | * Gets a specified key |
| | 31 | * |
| | 32 | * @since 4.7.0 |
| | 33 | * @access public |
| | 34 | * |
| | 35 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.get |
| | 36 | * |
| | 37 | * @param mixed $key The key to get the value for. |
| | 38 | */ |
| | 39 | public function &__get( $key ) { |
| | 40 | return $this->data[ $key ]; |
| | 41 | } |
| | 42 | |
| | 43 | /** |
| | 44 | * Sets a specified key |
| | 45 | * |
| | 46 | * @since 4.7.0 |
| | 47 | * @access public |
| | 48 | * |
| | 49 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.set |
| | 50 | * |
| | 51 | * @param mixed $key The key to assign the value to. |
| | 52 | * @param mixed $value The value to set. |
| | 53 | */ |
| | 54 | public function __set( $key, $value ) { |
| | 55 | $this->data[ $key ] = $value; |
| | 56 | } |
| | 57 | |
| | 58 | /** |
| | 59 | * Determines whether a key value exists. |
| | 60 | * |
| | 61 | * @since 4.7.0 |
| | 62 | * @access public |
| | 63 | * |
| | 64 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.isset |
| | 65 | * |
| | 66 | * @param mixed $key A key to check for. |
| | 67 | * @return bool True if the key exists, false otherwise. |
| | 68 | */ |
| | 69 | public function __isset( $key ) { |
| | 70 | return isset( $this->data[ $key ] ); |
| | 71 | } |
| | 72 | |
| | 73 | /** |
| | 74 | * Unsets a specified key |
| | 75 | * |
| | 76 | * @since 4.7.0 |
| | 77 | * @access public |
| | 78 | * |
| | 79 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.unset |
| | 80 | * |
| | 81 | * @param mixed $key The key to unset. |
| | 82 | */ |
| | 83 | public function __unset( $key ) { |
| | 84 | unset( $this->data[ $key ] ); |
| | 85 | } |
| | 86 | |
| | 87 | /** |
| | 88 | * Sets a value at a specified offset. |
| | 89 | * |
| | 90 | * @since 4.7.0 |
| | 91 | * @access public |
| | 92 | * |
| | 93 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| | 94 | * |
| | 95 | * @param mixed $offset The offset to assign the value to. |
| | 96 | * @param mixed $value The value to set. |
| | 97 | */ |
| | 98 | public function offsetSet( $offset, $value ) { |
| | 99 | if ( is_null( $offset ) ) { |
| | 100 | $this->data[] = $value; |
| | 101 | } else { |
| | 102 | $this->data[ $offset ] = $value; |
| | 103 | } |
| | 104 | } |
| | 105 | |
| | 106 | /** |
| | 107 | * Determines whether an offset value exists. |
| | 108 | * |
| | 109 | * @since 4.7.0 |
| | 110 | * @access public |
| | 111 | * |
| | 112 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| | 113 | * |
| | 114 | * @param mixed $offset An offset to check for. |
| | 115 | * @return bool True if the offset exists, false otherwise. |
| | 116 | */ |
| | 117 | public function offsetExists( $offset ) { |
| | 118 | return isset( $this->data[ $offset ] ); |
| | 119 | } |
| | 120 | |
| | 121 | /** |
| | 122 | * Unsets a specified offset. |
| | 123 | * |
| | 124 | * @since 4.7.0 |
| | 125 | * @access public |
| | 126 | * |
| | 127 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| | 128 | * |
| | 129 | * @param mixed $offset The offset to unset. |
| | 130 | */ |
| | 131 | public function offsetUnset( $offset ) { |
| | 132 | unset( $this->data[ $offset ] ); |
| | 133 | } |
| | 134 | |
| | 135 | /** |
| | 136 | * Retrieves a value at a specified offset. |
| | 137 | * |
| | 138 | * @since 4.7.0 |
| | 139 | * @access public |
| | 140 | * |
| | 141 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| | 142 | * |
| | 143 | * @param mixed $offset The offset to retrieve. |
| | 144 | * @return mixed If set, the value at the specified offset, null otherwise. |
| | 145 | */ |
| | 146 | public function offsetGet( $offset ) { |
| | 147 | return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null; |
| | 148 | } |
| | 149 | |
| | 150 | /** |
| | 151 | * Returns the current element. |
| | 152 | * |
| | 153 | * @since 4.7.0 |
| | 154 | * @access public |
| | 155 | * |
| | 156 | * @link http://php.net/manual/en/iterator.current.php |
| | 157 | * |
| | 158 | * @return array Of callbacks at current priority. |
| | 159 | */ |
| | 160 | public function current() { |
| | 161 | return current( $this->data ); |
| | 162 | } |
| | 163 | |
| | 164 | /** |
| | 165 | * Moves forward to the next element. |
| | 166 | * |
| | 167 | * @since 4.7.0 |
| | 168 | * @access public |
| | 169 | * |
| | 170 | * @link http://php.net/manual/en/iterator.next.php |
| | 171 | * |
| | 172 | * @return array Of callbacks at next priority. |
| | 173 | */ |
| | 174 | public function next() { |
| | 175 | return next( $this->data ); |
| | 176 | } |
| | 177 | |
| | 178 | /** |
| | 179 | * Returns the key of the current element. |
| | 180 | * |
| | 181 | * @since 4.7.0 |
| | 182 | * @access public |
| | 183 | * |
| | 184 | * @link http://php.net/manual/en/iterator.key.php |
| | 185 | * |
| | 186 | * @return mixed Returns current priority on success, or NULL on failure |
| | 187 | */ |
| | 188 | public function key() { |
| | 189 | return key( $this->data ); |
| | 190 | } |
| | 191 | |
| | 192 | /** |
| | 193 | * Checks if current position is valid. |
| | 194 | * |
| | 195 | * @since 4.7.0 |
| | 196 | * @access public |
| | 197 | * |
| | 198 | * @link http://php.net/manual/en/iterator.valid.php |
| | 199 | * |
| | 200 | * @return boolean |
| | 201 | */ |
| | 202 | public function valid() { |
| | 203 | return key( $this->data ) !== null; |
| | 204 | } |
| | 205 | |
| | 206 | /** |
| | 207 | * Rewinds the Iterator to the first element. |
| | 208 | * |
| | 209 | * @since 4.7.0 |
| | 210 | * @access public |
| | 211 | * |
| | 212 | * @link http://php.net/manual/en/iterator.rewind.php |
| | 213 | */ |
| | 214 | public function rewind() { |
| | 215 | reset( $this->data ); |
| | 216 | } |
| | 217 | } |