Collection.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.3
  9. */
  10. abstract class Collection extends Base implements \Gogs\Lib\ArrayIterator {
  11. private $objs;
  12. /**
  13. * Initialize a collection.
  14. *
  15. * @see Base
  16. * @param string $api_url The API URL
  17. * @param string $api_token The API token
  18. * @param Collection $other Collection to initialize from
  19. */
  20. public function __construct(string $api_url, string $api_token, Collection $other = null) {
  21. parent::__construct($api_url, $api_token);
  22. if ($other != null)
  23. $this->objs = $others->copy();
  24. else
  25. $this->objs = new \Gogs\Lib\Collection();
  26. }
  27. /**
  28. * Add an object to the collection.
  29. *
  30. * When adding a key the object will be stored
  31. * on the particual key, also overwriting existing data.
  32. *
  33. * @param mixed $obj Element to store
  34. * @param mixed $key Index key to store on
  35. * @return mixed|int The index key. If key is null the returned value will be an integer.
  36. */
  37. protected function add($obj, $key = null) {
  38. $this->objs->set($obj, $key);
  39. return $key == null ? $this->objs->len() - 1 : $key;
  40. }
  41. /**
  42. * Remove an element in collection.
  43. *
  44. * The function will first look for the element as a
  45. * index key, but if its not found it will look for the
  46. * element as a value.
  47. *
  48. * Deep functions only when the value is given and not the key.
  49. *
  50. * @deprecated 0.1.1 Will be removed in future release
  51. * @param mixed $any Index key or element value
  52. * @param bool $deep Delete every item and not just the first
  53. * @return bool
  54. */
  55. protected function remove($any, bool $deep = true) {
  56. return $objs->remove($any, $deep);
  57. }
  58. /**
  59. * @see \Gogs\Lib\ArrayIterator
  60. */
  61. public function copy() {
  62. return new Collection($this);
  63. }
  64. /**
  65. * @see \Gogs\Lib\ArrayIterator
  66. */
  67. public function all() {
  68. return $this->objs->copy()->all();
  69. }
  70. /**
  71. * @see \Gogs\Lib\ArrayIterator
  72. */
  73. public function len() {
  74. return $this->objs->len();
  75. }
  76. /**
  77. * @see \Gogs\Lib\ArrayIterator
  78. */
  79. public function by_key($idx) {
  80. return $this->objs->by_key($idx);
  81. }
  82. /**
  83. * @see \Gogs\Lib\ArrayIterator
  84. */
  85. public function next() {
  86. return $this->objs->next();
  87. }
  88. /**
  89. * @see \Gogs\Lib\ArrayIterator
  90. */
  91. public function prev() {
  92. return $this->objs->prev();
  93. }
  94. /**
  95. * @see \Gogs\Lib\ArrayIterator
  96. */
  97. public function current() {
  98. return $this->objs->current();
  99. }
  100. /**
  101. * @see \Gogs\Lib\ArrayIterator
  102. */
  103. public function reset() {
  104. return $this->objs->reset();
  105. }
  106. /**
  107. * @see \Gogs\Lib\ArrayIterator
  108. */
  109. public function sort(callable $f) {
  110. return $this->objs->copy()->sort($f);
  111. }
  112. /**
  113. * @see \Gogs\Lib\ArrayIterator
  114. */
  115. public function limit(int $lim) {
  116. return $this->objs->copy()->limit($lim);
  117. }
  118. /**
  119. * @see \Gogs\Lib\ArrayIterator
  120. */
  121. public function offset(int $off) {
  122. return $this->objs->copy()->offset($off);
  123. }
  124. /**
  125. * @see \Gogs\Lib\ArrayIterator
  126. */
  127. public function reverse() {
  128. return $this->objs->copy()->reverse();
  129. }
  130. /**
  131. * @see Base
  132. * @return array
  133. */
  134. protected function json_set_property(\stdClass $obj) {
  135. $keys = array();
  136. if (!is_object($obj) && !is_array($obj))
  137. return array();
  138. if (isset($obj->data))
  139. return $this->json_set_property((object)$obj->data);
  140. foreach($obj as $key => $val)
  141. $keys[] = $this->add_object($val);
  142. return $keys;
  143. }
  144. /**
  145. * Search an collection.
  146. *
  147. * @see Base
  148. * @throws Exception\NotImplementedException When not implemented by Collection class.
  149. * @return Collection
  150. */
  151. public function search(array $params = array(), bool $strict = false) {
  152. throw new NotImplementedException("::search:: Not implemented by class '" . get_class($this) . "'");
  153. }
  154. /**
  155. * Add an object to the collection with the specific type.
  156. *
  157. * Typically it will create an instance of the type that
  158. * the collection will consist of.
  159. *
  160. * Should call json set property
  161. *
  162. * @param \stdClass $object
  163. * @return array Key of entry in collection
  164. */
  165. abstract protected function add_object(\stdClass $object);
  166. /**
  167. * Sort the object
  168. *
  169. * Should call sort on parent with the specified sort method,
  170. * given by $flag
  171. *
  172. * @param int $flag Sorting flag
  173. * @return \Gogs\Lib\Collection
  174. */
  175. abstract public function sort_by(int $flag = \Gogs\Lib\ArrayIterator::SORT_INDEX);
  176. }
  177. }
  178. ?>