Collection.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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->all();
  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->url, $this->token, $this);
  63. }
  64. //abstract public function copy();
  65. /**
  66. * @see \Gogs\Lib\ArrayIterator
  67. */
  68. public function all() {
  69. return $this->objs->copy()->all();
  70. }
  71. /**
  72. * @see \Gogs\Lib\ArrayIterator
  73. */
  74. public function len() {
  75. return $this->objs->len();
  76. }
  77. /**
  78. * @see \Gogs\Lib\ArrayIterator
  79. */
  80. public function by_key($idx) {
  81. return $this->objs->by_key($idx);
  82. }
  83. /**
  84. * @see \Gogs\Lib\ArrayIterator
  85. */
  86. public function next() {
  87. return $this->objs->next();
  88. }
  89. /**
  90. * @see \Gogs\Lib\ArrayIterator
  91. */
  92. public function prev() {
  93. return $this->objs->prev();
  94. }
  95. /**
  96. * @see \Gogs\Lib\ArrayIterator
  97. */
  98. public function current() {
  99. return $this->objs->current();
  100. }
  101. /**
  102. * @see \Gogs\Lib\ArrayIterator
  103. */
  104. public function reset() {
  105. return $this->objs->reset();
  106. }
  107. /**
  108. * @see \Gogs\Lib\ArrayIterator
  109. */
  110. public function sort(callable $f) {
  111. return $this->objs->copy()->sort($f);
  112. }
  113. /**
  114. * @see \Gogs\Lib\ArrayIterator
  115. */
  116. public function filter(callable $f) {
  117. return $this->objs->copy()->filter($f);
  118. }
  119. /**
  120. * @see \Gogs\Lib\ArrayIterator
  121. */
  122. public function limit(int $lim) {
  123. return $this->objs->copy()->limit($lim);
  124. }
  125. /**
  126. * @see \Gogs\Lib\ArrayIterator
  127. */
  128. public function offset(int $off) {
  129. return $this->objs->copy()->offset($off);
  130. }
  131. /**
  132. * @see \Gogs\Lib\ArrayIterator
  133. */
  134. public function reverse() {
  135. return $this->objs->copy()->reverse();
  136. }
  137. /**
  138. * @see Base
  139. * @return array
  140. */
  141. protected function json_set_property(\stdClass $obj) {
  142. $keys = array();
  143. if (!is_object($obj) && !is_array($obj))
  144. return array();
  145. if (isset($obj->data))
  146. return $this->json_set_property((object)$obj->data);
  147. foreach($obj as $key => $val)
  148. $keys[] = $this->add_object($val);
  149. return $keys;
  150. }
  151. /**
  152. * Search an collection.
  153. *
  154. * @see Base
  155. * @throws Exception\NotImplementedException When not implemented by Collection class.
  156. * @return Collection
  157. */
  158. public function search(array $params = array(), bool $strict = false) {
  159. throw new NotImplementedException("::search:: Not implemented by class '" . get_class($this) . "'");
  160. }
  161. /**
  162. * Add an object to the collection with the specific type.
  163. *
  164. * Typically it will create an instance of the type that
  165. * the collection will consist of.
  166. *
  167. * Should call json set property
  168. *
  169. * @param \stdClass $object
  170. * @return array Key of entry in collection
  171. */
  172. abstract protected function add_object(\stdClass $object);
  173. /**
  174. * Sort the object
  175. *
  176. * Should call sort on parent with the specified sort method,
  177. * given by $flag
  178. *
  179. * @param int $flag Sorting flag
  180. * @return \Gogs\Lib\Collection
  181. */
  182. abstract public function sort_by(int $flag = \Gogs\Lib\ArrayIterator::SORT_INDEX);
  183. }
  184. }
  185. ?>