Repos.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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
  8. */
  9. final class Repos extends Collection {
  10. protected $owner;
  11. /**
  12. * Initialize a repos collection
  13. *
  14. * If owner is not set it will query the whole
  15. * repo archive on Gogs.
  16. *
  17. * @param string $api_url The api-url
  18. * @param string $api_token The api-token
  19. * @param User $owner The owner of the collection
  20. */
  21. public function __construct(string $api_url, string $api_token, User $owner = null) {
  22. parent::__construct($api_url, $api_token);
  23. $this->owner = $owner;
  24. }
  25. /**
  26. * @see Base
  27. */
  28. protected function set_scope(string $method) {
  29. switch ($method) {
  30. case "get":
  31. if ($this->owner instanceof Org)
  32. $this->scope = "/orgs/" . $this->owner->username . "/repos";
  33. else
  34. $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->username ) . "/repos";
  35. break;
  36. case "search":
  37. $this->scope = "/repos/search";
  38. break;
  39. default:
  40. return false;
  41. }
  42. return true;
  43. }
  44. /**
  45. * @see Collection
  46. */
  47. public function create(...$args) {
  48. $repo = new Repo($this->url, $this->token, $this->owner);
  49. if (count($args) > 0)
  50. $repo->create(...$args);
  51. return $repo;
  52. }
  53. /**
  54. * Searches for a repo.
  55. *
  56. * If the owner is specified the search will be
  57. * limited to the actual user.
  58. *
  59. * @todo Should rewrite now since set_scope is now the deal
  60. * @see Collection
  61. */
  62. public function search(array $params = array()) {
  63. $scope = substr($this->scope, 1, strpos($this->scope, "/", 1)-1);
  64. if (!isset($params["name"]) && !isset($params["q"]))
  65. throw new Exception\SearchParamException("Missing param <name>|<q>");
  66. if (isset($params["name"])) {
  67. $params["q"] = $params["name"];
  68. unset($params["name"]);
  69. }
  70. if (!isset($params["user"]))
  71. $params["user"] = isset($this->owner) ? $this->owner->id : 0;
  72. if (!isset($params["limit"]))
  73. $params["limit"] = 10;
  74. $repos = new \Gogs\Lib\Collection();
  75. switch ($scope) {
  76. case "user":
  77. case "users":
  78. case "org":
  79. $this->load();
  80. foreach($this->all() as $key => $repo) {
  81. if ($repo->search($params["q"]))
  82. $repos->set($repo);
  83. if ($repos->len() == $params["limit"])
  84. break;
  85. }
  86. default:
  87. $this->set_scope("search");
  88. $jenc = $this->method_get($params);
  89. $jdec = $this->json_decode($jenc);
  90. foreach($this->json_set_property($jdec) as $key)
  91. $repos->set($this->by_key($key), $key);
  92. }
  93. return $repos;
  94. }
  95. /**
  96. * @see Base
  97. */
  98. protected function json_set_property($obj) {
  99. if (isset($obj->data))
  100. return $this->json_set_property($obj->data);
  101. $rnames = array();
  102. if (is_array($obj)) {
  103. foreach ($obj as $key => $val) {
  104. $repo = new Repo($this->url, $this->token);
  105. $repo->json_set_property($val);
  106. $this->add($repo, $repo->rfull_name);
  107. $rnames[] = $repo->rfull_name;
  108. }
  109. }
  110. return $rnames;
  111. }
  112. }
  113. }