Collection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Braintree;
  3. use Countable;
  4. use IteratorAggregate;
  5. use ArrayAccess;
  6. use OutOfRangeException;
  7. use ArrayIterator;
  8. /**
  9. * Braintree Generic collection
  10. *
  11. * PHP Version 5
  12. *
  13. * Based on Generic Collection class from:
  14. * {@link http://codeutopia.net/code/library/CU/Collection.php}
  15. *
  16. * @package Braintree
  17. * @subpackage Utility
  18. */
  19. class Collection implements Countable, IteratorAggregate, ArrayAccess
  20. {
  21. /**
  22. *
  23. * @var array collection storage
  24. */
  25. protected $_collection = [];
  26. /**
  27. * Add a value into the collection
  28. * @param string $value
  29. */
  30. public function add($value)
  31. {
  32. $this->_collection[] = $value;
  33. }
  34. /**
  35. * Set index's value
  36. * @param integer $index
  37. * @param mixed $value
  38. * @throws OutOfRangeException
  39. */
  40. public function set($index, $value)
  41. {
  42. if($index >= $this->count())
  43. throw new OutOfRangeException('Index out of range');
  44. $this->_collection[$index] = $value;
  45. }
  46. /**
  47. * Remove a value from the collection
  48. * @param integer $index index to remove
  49. * @throws OutOfRangeException if index is out of range
  50. */
  51. public function remove($index)
  52. {
  53. if($index >= $this->count())
  54. throw new OutOfRangeException('Index out of range');
  55. array_splice($this->_collection, $index, 1);
  56. }
  57. /**
  58. * Return value at index
  59. * @param integer $index
  60. * @return mixed
  61. * @throws OutOfRangeException
  62. */
  63. public function get($index)
  64. {
  65. if($index >= $this->count())
  66. throw new OutOfRangeException('Index out of range');
  67. return $this->_collection[$index];
  68. }
  69. /**
  70. * Determine if index exists
  71. * @param integer $index
  72. * @return boolean
  73. */
  74. public function exists($index)
  75. {
  76. if($index >= $this->count())
  77. return false;
  78. return true;
  79. }
  80. /**
  81. * Return count of items in collection
  82. * Implements countable
  83. * @return integer
  84. */
  85. public function count()
  86. {
  87. return count($this->_collection);
  88. }
  89. /**
  90. * Return an iterator
  91. * Implements IteratorAggregate
  92. * @return ArrayIterator
  93. */
  94. public function getIterator()
  95. {
  96. return new ArrayIterator($this->_collection);
  97. }
  98. /**
  99. * Set offset to value
  100. * Implements ArrayAccess
  101. * @see set
  102. * @param integer $offset
  103. * @param mixed $value
  104. */
  105. public function offsetSet($offset, $value)
  106. {
  107. $this->set($offset, $value);
  108. }
  109. /**
  110. * Unset offset
  111. * Implements ArrayAccess
  112. * @see remove
  113. * @param integer $offset
  114. */
  115. public function offsetUnset($offset)
  116. {
  117. $this->remove($offset);
  118. }
  119. /**
  120. * get an offset's value
  121. * Implements ArrayAccess
  122. * @see get
  123. * @param integer $offset
  124. * @return mixed
  125. */
  126. public function offsetGet($offset)
  127. {
  128. return $this->get($offset);
  129. }
  130. /**
  131. * Determine if offset exists
  132. * Implements ArrayAccess
  133. * @see exists
  134. * @param integer $offset
  135. * @return boolean
  136. */
  137. public function offsetExists($offset)
  138. {
  139. return $this->exists($offset);
  140. }
  141. }
  142. class_alias('Braintree\Collection', 'Braintree_Collection');