Collection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Collection is a collection of data of one type.
  5. *
  6. * @see Users
  7. * @author Joachim M. Giaever (joachim[]giaever.org)
  8. * @version 0.1
  9. */
  10. abstract class Collection extends Base implements \Gogs\Lib\ArrayIterator {
  11. private $objs = array();
  12. /**
  13. * Add an object to the collection.
  14. *
  15. * When adding a key the object will be stored
  16. * on the particual key, also overwriting existing data.
  17. *
  18. * @param mixed $obj Element to store
  19. * @param mixed $key Index key to store on
  20. * @return mixed|int The index key. If key is null the returned value will be an integer.
  21. */
  22. public function add($obj, $key = null) {
  23. if (!isset($key))
  24. array_push($this->objs, $obj);
  25. else
  26. $this->objs[$key] = $obj;
  27. return $key == null ? $this->len - 1 : $key;
  28. }
  29. /**
  30. * Remove an element in collection.
  31. *
  32. * The function will first look for the element as a
  33. * index key, but if its not found it will look for the
  34. * element as a value.
  35. *
  36. * Deep functions only when the value is given and not the key.
  37. *
  38. * @param mixed $any Index key or element value
  39. * @param bool $deep Delete every item and not just the first
  40. * @return bool
  41. */
  42. public function remove($any, bool $deep = true) {
  43. if (isset($this->objs[$any])) {
  44. unset($this->objs[$any]);
  45. return true;
  46. } else if (in_array($any, $this->objs)) {
  47. $key = array_search($any, $this->objs, true);
  48. // No need to add deep ($key deletion)
  49. $val = $this->remove($key);
  50. if ($val && $deep) // Delete every object
  51. $this->remove($any, $deep);
  52. return $val;
  53. }
  54. return false;
  55. }
  56. /**
  57. * @see \Gogs\Lib\ArrayIterator
  58. */
  59. public function all() {
  60. return $this->objs;
  61. }
  62. /**
  63. * @see \Gogs\Lib\ArrayIterator
  64. */
  65. public function len() {
  66. return count($this->objs);
  67. }
  68. /**
  69. * @see \Gogs\Lib\ArrayIterator
  70. */
  71. public function by_key($idx) {
  72. return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
  73. }
  74. /**
  75. * @see \Gogs\Lib\ArrayIterator
  76. */
  77. public function next() {
  78. return next($this->objs);
  79. }
  80. /**
  81. * @see \Gogs\Lib\ArrayIterator
  82. */
  83. public function prev() {
  84. return prev($this->objs);
  85. }
  86. /**
  87. * @see \Gogs\Lib\ArrayIterator
  88. */
  89. public function current() {
  90. return current($this->objs);
  91. }
  92. /**
  93. * @see \Gogs\Lib\ArrayIterator
  94. */
  95. public function reset() {
  96. return reset($this->objs);
  97. }
  98. /**
  99. * Search for an object.
  100. *
  101. * @param array $params Parameters
  102. * @return \Gogs\Lib\Collection
  103. */
  104. abstract public function search(array $params = array());
  105. }
  106. }
  107. ?>