ListResult.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. class ChargeBee_ListResult implements Countable, ArrayAccess, Iterator
  3. {
  4. private $response;
  5. private $nextOffset;
  6. protected $_items;
  7. private $_index;
  8. function __construct($response, $nextOffset)
  9. {
  10. $this->response = $response;
  11. $this->nextOffset = $nextOffset;
  12. $this->_items = array();
  13. $this->_initItems();
  14. }
  15. private function _initItems()
  16. {
  17. foreach($this->response as $r)
  18. {
  19. array_push($this->_items, new ChargeBee_Result($r));
  20. }
  21. }
  22. public function nextOffset()
  23. {
  24. return $this->nextOffset;
  25. }
  26. public function count()
  27. {
  28. return count($this->_items);
  29. }
  30. //Implementation for ArrayAccess functions
  31. public function offsetSet($k, $v)
  32. {
  33. $this->$k = $v;
  34. }
  35. public function offsetExists($k)
  36. {
  37. return isset($this->$k);
  38. }
  39. public function offsetUnset($k)
  40. {
  41. unset($this->$k);
  42. }
  43. public function offsetGet($k)
  44. {
  45. return isset($this->list[$k]) ? $this->list[$k] : null;
  46. }
  47. //Implementation for Iterator functions
  48. public function current()
  49. {
  50. return $this->_items[$this->_index];
  51. }
  52. public function key()
  53. {
  54. return $this->_index;
  55. }
  56. public function next()
  57. {
  58. ++$this->_index;
  59. }
  60. public function rewind()
  61. {
  62. $this->_index = 0;
  63. }
  64. public function valid()
  65. {
  66. if ($this->_index < count($this->_items)) {
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. }
  72. public function toJson() {
  73. return json_encode($this->response);
  74. }
  75. }
  76. ?>