HasDataTrait.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Aws;
  3. /**
  4. * Trait implementing ToArrayInterface, \ArrayAccess, \Countable, and
  5. * \IteratorAggregate
  6. */
  7. trait HasDataTrait
  8. {
  9. /** @var array */
  10. private $data = [];
  11. public function getIterator()
  12. {
  13. return new \ArrayIterator($this->data);
  14. }
  15. /**
  16. * This method returns a reference to the variable to allow for indirect
  17. * array modification (e.g., $foo['bar']['baz'] = 'qux').
  18. *
  19. * @param $offset
  20. *
  21. * @return mixed|null
  22. */
  23. public function & offsetGet($offset)
  24. {
  25. if (isset($this->data[$offset])) {
  26. return $this->data[$offset];
  27. }
  28. $value = null;
  29. return $value;
  30. }
  31. public function offsetSet($offset, $value)
  32. {
  33. $this->data[$offset] = $value;
  34. }
  35. public function offsetExists($offset)
  36. {
  37. return isset($this->data[$offset]);
  38. }
  39. public function offsetUnset($offset)
  40. {
  41. unset($this->data[$offset]);
  42. }
  43. public function toArray()
  44. {
  45. return $this->data;
  46. }
  47. public function count()
  48. {
  49. return count($this->data);
  50. }
  51. }