Repos.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Repos is a collection of repos.
  5. *
  6. * @author Joachim M. Giaever (joachim[]giaever.org)
  7. * @version 0.1.1
  8. */
  9. final class Repos extends Collection {
  10. const SORT_UPDATED = Collection::SORT_INDEX << 1;
  11. const SORT_CREATED = Collection::SORT_INDEX << 2;
  12. const SORT_OWNER = Collection::SORT_INDEX << 3;
  13. protected $owner;
  14. /**
  15. * Initialize a repos collection
  16. *
  17. * If owner is not set it will query the whole
  18. * repo archive on Gogs.
  19. *
  20. * @param string $api_url The api-url
  21. * @param string $api_token The api-token
  22. * @param User $owner The owner of the collection
  23. */
  24. public function __construct(string $api_url, string $api_token, User $owner = null) {
  25. parent::__construct($api_url, $api_token);
  26. $this->owner = $owner;
  27. }
  28. /**
  29. * @see Base
  30. */
  31. protected function set_scope(string $method) {
  32. switch ($method) {
  33. case "get":
  34. case "load":
  35. if ($this->owner instanceof Org)
  36. $this->scope = "/orgs/" . $this->owner->username . "/repos";
  37. else
  38. $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->username ) . "/repos";
  39. break;
  40. case "search":
  41. $this->scope = "/repos/search";
  42. break;
  43. default:
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * @see Collection
  50. */
  51. public function create(...$args) {
  52. $repo = new Repo($this->url, $this->token, $this->owner);
  53. if (count($args) > 0) {
  54. $repo->create(...$args);
  55. $this->add($repo, $repo->full_name);
  56. }
  57. return $repo;
  58. }
  59. /**
  60. * Searches for a repo.
  61. *
  62. * If the owner is specified the search will be
  63. * limited to the actual user.
  64. *
  65. * @see Collection
  66. * @return Repos
  67. */
  68. public function search(array $params = array()) {
  69. if (!isset($params["name"]) && !isset($params["q"]))
  70. throw new Exception\SearchParamException("Missing param <name>|<q>");
  71. if (isset($params["name"])) {
  72. $params["q"] = $params["name"];
  73. unset($params["name"]);
  74. }
  75. if (!isset($params["user"]) || isset($this->owner))
  76. $params["user"] = isset($this->owner) ? $this->owner->id : 0;
  77. if (!isset($params["limit"]))
  78. $params["limit"] = 10;
  79. $repos = new Repos($this->url, $this->token, $this->owner);
  80. if ($this->loaded) {
  81. foreach($this->all() as $key => $repo) {
  82. if ($repo->search($params["q"]))
  83. $repos->add($repo, $key);
  84. if ($repos->len() == $params["limit"])
  85. break;
  86. }
  87. } else {
  88. $this->set_scope("search");
  89. $jenc = $this->method_get($params);
  90. $jdec = $this->json_decode($jenc);
  91. foreach($this->json_set_property($jdec) as $key)
  92. $repos->add($this->by_key($key), $key);
  93. }
  94. return $repos;
  95. }
  96. /**
  97. * Sort repos by `method`.
  98. *
  99. * Valid methods:
  100. *
  101. * * SORT_UPDATED: Sort on `updated_at` value
  102. * * SORT_CREATED: Sort on `created_at` value
  103. * * SORT_OWNER: Sort on `owner` (organization repos etc may appear)
  104. *
  105. * @param int $flag Defines sorting algorithm to use
  106. * @param bool $asc Ascending order
  107. * @return \Gogs\Lib\Collection
  108. */
  109. public function sort_by(int $flag = Collection::SORT_INDEX, bool $asc = false) {
  110. switch ($flag) {
  111. case self::SORT_CREATED:
  112. return ($sort = $this->sort(function(Repo $a, Repo $b) {
  113. $adate = new \DateTime($a->created_at);
  114. $bdate = new \DateTime($b->created_at);
  115. return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1));
  116. })) ? ($asc ? $sort->reverse() : $sort) : false;
  117. case self::SORT_UPDATED:
  118. return ($sort = $this->sort(function(Repo $a, Repo $b) {
  119. $adate = new \DateTime($a->updated_at);
  120. $bdate = new \DateTime($b->updated_at);
  121. return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1));
  122. })) ? ($asc ? $sort->reverse() : $sort) : false;
  123. case self::SORT_OWNER:
  124. return ($sort = $this->sort(function(Repo $a, Repo $b) {
  125. return strcmp($a->owner->username, $b->owner->username);
  126. })) ? ($asc ? $sort->reverse() : $sort) : false;
  127. default:
  128. return ($sort = $this->sort("ksort")) ? ($asc ? $sort->reverse() : $sort) : false;
  129. }
  130. }
  131. /**
  132. * @see Base
  133. */
  134. protected function json_set_property($obj) {
  135. if (isset($obj->data))
  136. return $this->json_set_property($obj->data);
  137. $rnames = array();
  138. if (is_array($obj)) {
  139. foreach ($obj as $key => $val) {
  140. $repo = new Repo($this->url, $this->token);
  141. $repo->json_set_property($val);
  142. $this->add($repo, $repo->full_name);
  143. $rnames[] = $repo->full_name;
  144. }
  145. }
  146. return $rnames;
  147. }
  148. }
  149. }