HasDataTrait.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace GuzzleHttp\Command;
  3. /**
  4. * Basic collection behavior for Command and Result objects.
  5. *
  6. * The methods in the class are primarily for implementing the ArrayAccess,
  7. * Countable, and IteratorAggregate interfaces.
  8. */
  9. trait HasDataTrait
  10. {
  11. /** @var array Data stored in the collection. */
  12. protected $data;
  13. public function __toString()
  14. {
  15. return print_r($this, true);
  16. }
  17. public function __debugInfo()
  18. {
  19. return $this->data;
  20. }
  21. public function offsetExists($offset)
  22. {
  23. return array_key_exists($offset, $this->data);
  24. }
  25. public function offsetGet($offset)
  26. {
  27. return isset($this->data[$offset]) ? $this->data[$offset] : null;
  28. }
  29. public function offsetSet($offset, $value)
  30. {
  31. $this->data[$offset] = $value;
  32. }
  33. public function offsetUnset($offset)
  34. {
  35. unset($this->data[$offset]);
  36. }
  37. public function count()
  38. {
  39. return count($this->data);
  40. }
  41. public function getIterator()
  42. {
  43. return new \ArrayIterator($this->data);
  44. }
  45. public function toArray()
  46. {
  47. return $this->data;
  48. }
  49. }