earray.class.php 863 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. class EArray implements ArrayAccess {
  3. use Events;
  4. private $data = [];
  5. public static function from(array $data){
  6. return new static($data);
  7. }
  8. protected function __construct(array $data){
  9. $this->data = array_values($data);
  10. }
  11. public function offsetExists($offset){
  12. $this->fire('exists', $offset);
  13. return isset($this->data[$offset]);
  14. }
  15. public function offsetGet($offset){
  16. $this->fire('get', $offset);
  17. return $this->data[$offset];
  18. }
  19. public function offsetSet($offset, $value){
  20. $this->fire('set', $offset, $value);
  21. $this->data[$offset] = $value;
  22. }
  23. public function offsetUnset($offset){
  24. $this->fire('unset', $offset, $this[$offset]);
  25. unset($this->data[$offset]);
  26. }
  27. public function each(callable $fn){
  28. foreach($this->data as $offset => $model){
  29. $fn($model, $offset);
  30. }
  31. return $this;
  32. }
  33. }
  34. ?>