repos.php 2.6 KB

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