12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace Juju\Data;
- require_once(realpath(dirname(__DIR__).'/events.trait.php'));
- use Juju\Events;
- class EArray implements \ArrayAccess, \Countable {
- use Events;
- protected $data = [];
- public static function from(array $data){
- return new static($data);
- }
- protected function __construct(array $data){
- $this->data = array_values($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 each(callable $fn){
- foreach($this->data as $offset => $model){
- $fn($model, $offset);
- }
- return $this;
- }
- }
- ?>
|