earray.class.php 968 B

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