Collection.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.1
  9. */
  10. abstract class Collection extends Base implements \Gogs\Lib\ArrayIterator {
  11. private $objs;
  12. public function __construct(string $api_url, string $api_token, Collection $other = null) {
  13. parent::__construct($api_url, $api_token);
  14. if ($other != null)
  15. $this->objs = $others->copy();
  16. else
  17. $this->objs = new \Gogs\Lib\Collection();
  18. }
  19. public function copy() {
  20. return new Collection($this);
  21. }
  22. /**
  23. * Add an object to the collection.
  24. *
  25. * When adding a key the object will be stored
  26. * on the particual key, also overwriting existing data.
  27. *
  28. * @param mixed $obj Element to store
  29. * @param mixed $key Index key to store on
  30. * @return mixed|int The index key. If key is null the returned value will be an integer.
  31. */
  32. public function add($obj, $key = null) {
  33. $this->objs->set($obj, $key);
  34. return $key == null ? $this->objs->len() - 1 : $key;
  35. }
  36. /**
  37. * Remove an element in collection.
  38. *
  39. * The function will first look for the element as a
  40. * index key, but if its not found it will look for the
  41. * element as a value.
  42. *
  43. * Deep functions only when the value is given and not the key.
  44. *
  45. * @deprecated 0.1.1 Will be removed in future release
  46. * @param mixed $any Index key or element value
  47. * @param bool $deep Delete every item and not just the first
  48. * @return bool
  49. */
  50. protected function remove($any, bool $deep = true) {
  51. return $objs->remove($any, $deep);
  52. }
  53. /**
  54. * @see \Gogs\Lib\ArrayIterator
  55. */
  56. public function all() {
  57. return $this->objs->copy()->all();
  58. }
  59. /**
  60. * @see \Gogs\Lib\ArrayIterator
  61. */
  62. public function len() {
  63. return $this->objs->len();
  64. }
  65. /**
  66. * @see \Gogs\Lib\ArrayIterator
  67. */
  68. public function by_key($idx) {
  69. return $this->objs->by_key($idx);
  70. }
  71. /**
  72. * @see \Gogs\Lib\ArrayIterator
  73. */
  74. public function next() {
  75. return $this->objs->next();
  76. }
  77. /**
  78. * @see \Gogs\Lib\ArrayIterator
  79. */
  80. public function prev() {
  81. return $this->objs->prev();
  82. }
  83. /**
  84. * @see \Gogs\Lib\ArrayIterator
  85. */
  86. public function current() {
  87. return $this->objs->current();
  88. }
  89. /**
  90. * @see \Gogs\Lib\ArrayIterator
  91. */
  92. public function reset() {
  93. return $this->objs->reset();
  94. }
  95. /**
  96. * @see \Gogs\Lib\ArrayIterator
  97. */
  98. public function sort(callable $f) {
  99. return $this->objs->copy()->sort($f);
  100. }
  101. /**
  102. * @see \Gogs\Lib\ArrayIterator
  103. */
  104. public function limit(int $lim) {
  105. return $this->objs->copy()->limit($lim);
  106. }
  107. /**
  108. * @see \Gogs\Lib\ArrayIterator
  109. */
  110. public function offset(int $off) {
  111. return $this->objs->copy()->offset($off);
  112. }
  113. /**
  114. * @see \Gogs\Lib\ArrayIterator
  115. */
  116. public function reverse() {
  117. return $this->objs->copy()->reverse();
  118. }
  119. /**
  120. * Search for an object.
  121. *
  122. * @param array $params Parameters
  123. * @return \Gogs\Lib\Collection
  124. */
  125. abstract public function search(array $params = array());
  126. /**
  127. * Sort the object
  128. *
  129. * Should call sort on parent with the specified sort method,
  130. * given by $flag
  131. *
  132. * @param int $flag Sorting flag
  133. * @return \Gogs\Lib\Collection
  134. */
  135. abstract public function sort_by(int $flag = \Gogs\Lib\ArrayIterator::SORT_INDEX);
  136. }
  137. }
  138. ?>