| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter; |
| 4 |
|
| 5 |
class ExporterRecord implements \ArrayAccess, \Iterator, \Countable |
| 6 |
{ |
| 7 |
private $_data = []; |
| 8 |
private $_mapper_type; |
| 9 |
private $keys = array(); |
| 10 |
private $position; |
| 11 |
|
| 12 |
public function __construct($data, $mapper_type) |
| 13 |
{ |
| 14 |
$this->_data = $data; |
| 15 |
$this->_mapper_type = $mapper_type; |
| 16 |
|
| 17 |
$this->keys = array_keys($this->_data); |
| 18 |
} |
| 19 |
|
| 20 |
public function current() |
| 21 |
{ |
| 22 |
return $this->_data[$this->keys[$this->position]]; |
| 23 |
} |
| 24 |
|
| 25 |
/** @return void */ |
| 26 |
public function next() |
| 27 |
{ |
| 28 |
$this->position++; |
| 29 |
} |
| 30 |
|
| 31 |
public function key() |
| 32 |
{ |
| 33 |
return $this->keys[$this->position]; |
| 34 |
} |
| 35 |
|
| 36 |
/** @return bool */ |
| 37 |
public function valid() |
| 38 |
{ |
| 39 |
return isset($this->keys[$this->position]); |
| 40 |
} |
| 41 |
|
| 42 |
/** @return void */ |
| 43 |
public function rewind() |
| 44 |
{ |
| 45 |
$this->position = 0; |
| 46 |
} |
| 47 |
|
| 48 |
/** @return int<0, \max> */ |
| 49 |
public function count() |
| 50 |
{ |
| 51 |
return count($this->keys); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param mixed $offset |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public function offsetExists($offset) |
| 59 |
{ |
| 60 |
if (!isset($this->_data[$offset])) { |
| 61 |
$value = apply_filters('iwp/exporter_record/' . $this->_mapper_type, null, $offset, $this->_data); |
| 62 |
if (!is_null($value)) { |
| 63 |
$this->_data[$offset] = $value; |
| 64 |
$this->keys = array_keys($this->_data); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return isset($this->_data[$offset]); |
| 69 |
} |
| 70 |
|
| 71 |
public function offsetGet($offset) |
| 72 |
{ |
| 73 |
return $this->offsetExists($offset) ? $this->_data[$offset] : null; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param mixed $offset |
| 78 |
* @param mixed $value |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function offsetSet($offset, $value) |
| 82 |
{ |
| 83 |
if (is_null($offset)) { |
| 84 |
$this->_data[] = $value; |
| 85 |
} else { |
| 86 |
$this->_data[$offset] = $value; |
| 87 |
} |
| 88 |
|
| 89 |
$this->keys = array_keys($this->_data); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @param mixed $offset |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function offsetUnset($offset) |
| 97 |
{ |
| 98 |
unset($this->_data[$offset]); |
| 99 |
$this->keys = array_keys($this->_data); |
| 100 |
} |
| 101 |
} |
| 102 |
|