Collection.php 5.5 KB

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