repos.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. default:
  11. $this->scope = ($this->owner ? $this->owner->scope : "" ) . "/repos";
  12. }
  13. }
  14. public function new(...$args) {
  15. $repo = new Repo($this->url, $this->token, $this->owner);
  16. if (count($args) > 0)
  17. $repo->create(...$args);
  18. return $repo;
  19. }
  20. public function search(array $params = array()) {
  21. $scope = substr($this->scope, 1, strpos($this->scope, "/", 1)-1);
  22. if (!isset($params["name"]) && !isset($params["q"]))
  23. throw new SearchParamException("Missing param <name>|<q>");
  24. if (isset($params["name"])) {
  25. $params["q"] = $params["name"];
  26. unset($params["name"]);
  27. }
  28. if (!isset($params["user"]))
  29. $params["user"] = 0;
  30. if (!isset($params["limit"]))
  31. $params["limit"] = 10;
  32. $repos = new CollectionResult();
  33. switch ($scope) {
  34. case "user":
  35. case "users":
  36. case "org":
  37. $this->load();
  38. foreach($this->all() as $key => $repo) {
  39. if ($repo->search($params["q"]))
  40. $repos->set($repo);
  41. if ($repos->len() == $params["limit"])
  42. break;
  43. }
  44. return $repos;
  45. default:
  46. $jenc = $this->method_get("/search", $params);
  47. $jdec = $this->json_decode($jenc);
  48. foreach($this->json_set_property($jdec) as $key)
  49. $repos->set($this->by_key($key), $key);
  50. return $repos;
  51. }
  52. }
  53. protected function json_set_property($obj) {
  54. if (isset($obj->data))
  55. return $this->json_set_property($obj->data);
  56. $rnames = array();
  57. if (is_array($obj)) {
  58. foreach ($obj as $key => $val) {
  59. $repo = new Repo($this->url, $this->token);
  60. $repo->json_set_property($val);
  61. $this->add($repo, $repo->rfull_name);
  62. array_push($rnames, $repo->rfull_name);
  63. }
  64. }
  65. return $rnames;
  66. }
  67. }