Repos.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Gogs\API\Request {
  3. final class Repos extends Collection {
  4. protected $owner;
  5. public function __construct(string $api_url, string $api_token, User $owner = null) {
  6. $this->owner = $owner;
  7. parent::__construct($api_url, $api_token);
  8. }
  9. protected function set_scope(string $method) {
  10. switch ($method) {
  11. case "get":
  12. if ($this->owner instanceof Org)
  13. $this->scope = "/orgs/" . $this->owner->uusername . "/repos";
  14. else
  15. $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->uusername ) . "/repos";
  16. break;
  17. case "search":
  18. $this->scope = "/repos/search";
  19. break;
  20. default:
  21. return false;
  22. }
  23. return true;
  24. }
  25. public function create(...$args) {
  26. $repo = new Repo($this->url, $this->token, $this->owner);
  27. if (count($args) > 0)
  28. $repo->create(...$args);
  29. return $repo;
  30. }
  31. public function search(array $params = array()) {
  32. $scope = substr($this->scope, 1, strpos($this->scope, "/", 1)-1);
  33. if (!isset($params["name"]) && !isset($params["q"]))
  34. throw new Exception\SearchParamException("Missing param <name>|<q>");
  35. if (isset($params["name"])) {
  36. $params["q"] = $params["name"];
  37. unset($params["name"]);
  38. }
  39. if (!isset($params["user"]))
  40. $params["user"] = 0;
  41. if (!isset($params["limit"]))
  42. $params["limit"] = 10;
  43. $repos = new \Gogs\Lib\Collection();
  44. switch ($scope) {
  45. case "user":
  46. case "users":
  47. case "org":
  48. $this->load();
  49. foreach($this->all() as $key => $repo) {
  50. if ($repo->search($params["q"]))
  51. $repos->set($repo);
  52. if ($repos->len() == $params["limit"])
  53. break;
  54. }
  55. default:
  56. $this->set_scope("search");
  57. $jenc = $this->method_get($params);
  58. $jdec = $this->json_decode($jenc);
  59. foreach($this->json_set_property($jdec) as $key)
  60. $repos->set($this->by_key($key), $key);
  61. }
  62. return $repos;
  63. }
  64. protected function json_set_property($obj) {
  65. if (isset($obj->data))
  66. return $this->json_set_property($obj->data);
  67. $rnames = array();
  68. if (is_array($obj)) {
  69. foreach ($obj as $key => $val) {
  70. $repo = new Repo($this->url, $this->token);
  71. $repo->json_set_property($val);
  72. $this->add($repo, $repo->rfull_name);
  73. array_push($rnames, $repo->rfull_name);
  74. }
  75. }
  76. return $rnames;
  77. }
  78. }
  79. }