| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Array_Access |
| 5 |
*/ |
| 6 |
class LP_Array_Access { |
| 7 |
|
| 8 |
/** |
| 9 |
* @var array |
| 10 |
*/ |
| 11 |
protected $_data = array(); |
| 12 |
|
| 13 |
/** |
| 14 |
* @var int |
| 15 |
*/ |
| 16 |
protected $_position = 0; |
| 17 |
|
| 18 |
/** |
| 19 |
* LP_Array_Access constructor. |
| 20 |
* |
| 21 |
* @param $data |
| 22 |
*/ |
| 23 |
public function __construct( $data ) { |
| 24 |
$this->_data = is_array( $data ) ? $data : (array) $data; |
| 25 |
} |
| 26 |
|
| 27 |
public function offsetExists( $offset ) { |
| 28 |
if ( $offset ) { |
| 29 |
return array_key_exists( $offset, $this->_data ); |
| 30 |
} |
| 31 |
|
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
public function offsetSet( $offset, $value ) { |
| 36 |
if ( $offset ) { |
| 37 |
$this->_data[ $offset ] = $value; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public function offsetGet( $offset ) { |
| 42 |
return $this->offsetExists( $offset ) ? $this->_data[ $offset ] : false; |
| 43 |
} |
| 44 |
|
| 45 |
public function offsetUnset( $offset ) { |
| 46 |
if ( $this->offsetExists( $offset ) ) { |
| 47 |
unset( $this->_data[ $offset ] ); |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Reset current position of answer options. |
| 57 |
*/ |
| 58 |
public function rewind() { |
| 59 |
$this->_position = 0; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @return mixed |
| 64 |
*/ |
| 65 |
public function current() { |
| 66 |
$values = array_values( $this->_data ); |
| 67 |
|
| 68 |
return $values[ $this->_position ]; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return mixed |
| 73 |
*/ |
| 74 |
public function key() { |
| 75 |
$keys = array_keys( $this->_data ); |
| 76 |
|
| 77 |
return $keys[ $this->_position ]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Nex question. |
| 82 |
*/ |
| 83 |
public function next() { |
| 84 |
++ $this->_position; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public function valid() { |
| 91 |
$values = array_values( $this->_data ); |
| 92 |
|
| 93 |
return isset( $values[ $this->_position ] ); |
| 94 |
} |
| 95 |
|
| 96 |
public function count() { |
| 97 |
return sizeof( $this->_data ); |
| 98 |
} |
| 99 |
} |
| 100 |
|