123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace Juju\Data;
- require_once(realpath(dirname(__DIR__).'/events.trait.php'));
- use Juju\Events;
- class EArray implements \ArrayAccess, \Countable, \Iterator, \Serializable, \JsonSerializable {
- use Events;
- protected $data = [];
- private $index = 0;
- public static function from(array $data){
- return new static($data);
- }
- protected function __construct(array $data){
- $this->data = $data;
- }
- public function offsetExists($offset){
- $this->fire('exists', $offset);
- return isset($this->data[$offset]);
- }
- public function offsetGet($offset){
- $this->fire('get', $offset);
- return $this->data[$offset];
- }
- public function offsetSet($offset, $value){
- $this->fire('set', $offset, $value);
- $this->data[$offset] = $value;
- }
- public function offsetUnset($offset){
- if($this->offsetExists($offset)){
- $this->fire('unset', $offset, $this[$offset]);
- unset($this->data[$offset]);
- }
- }
- public function count(){
- return count($this->data);
- }
- public function serialize(){
- return serialize($this->data);
- }
- public function unserialize($data){
- $this->data = unserialize($data);
- }
- public function JsonSerialize(){
- return $this->data;
- }
- public function each(callable $fn){
- foreach($this->data as $offset => $model){
- $fn($model, $offset);
- }
- return $this;
- }
- public function to_array(){
- return $this->data;
- }
- public function rewind(){
- $this->index = 0;
- }
- public function current(){
- return $this[$this->keys()[$this->index]];
- }
- public function key(){
- return $this->keys()[$this->index];
- }
- public function next(){
- $keys = $this->keys();
- return isset($keys[++$this->index]) ? $this->list[$keys[$this->index]] : false;
- }
- public function valid(){
- return isset($this->keys()[$this->index]);
- }
- public function keys(){
- return array_keys($this->data);
- }
- }
- ?>
|