1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php namespace Client\Request;
- class Repos extends Collection {
- protected $owner;
- public function __construct(string $api_url, string $api_token, User $owner = null) {
- $this->owner = $owner;
- parent::__construct($api_url, $api_token);
- }
- protected function set_scope(string $method) {
- switch ($method) {
- default:
- $this->scope = ($this->owner ? $this->owner->scope : "" ) . "/repos";
- }
- }
- public function new(...$args) {
- $repo = new Repo($this->url, $this->token, $this->owner);
- if (count($args) > 0)
- $repo->create(...$args);
- return $repo;
- }
- public function search(array $params = array()) {
- $scope = substr($this->scope, 1, strpos($this->scope, "/", 1)-1);
- if (!isset($params["name"]) && !isset($params["q"]))
- throw new SearchParamException("Missing param <name>|<q>");
- if (isset($params["name"])) {
- $params["q"] = $params["name"];
- unset($params["name"]);
- }
- if (!isset($params["user"]))
- $params["user"] = 0;
- if (!isset($params["limit"]))
- $params["limit"] = 10;
- $repos = new CollectionResult();
-
- switch ($scope) {
- case "user":
- case "users":
- case "org":
- $this->load();
- foreach($this->all() as $key => $repo) {
- if ($repo->search($params["q"]))
- $repos->set($repo);
- if ($repos->len() == $params["limit"])
- break;
- }
- return $repos;
- default:
- $jenc = $this->method_get("/search", $params);
- $jdec = $this->json_decode($jenc);
- foreach($this->json_set_property($jdec) as $key)
- $repos->set($this->by_key($key), $key);
- return $repos;
- }
- }
- protected function json_set_property($obj) {
- if (isset($obj->data))
- return $this->json_set_property($obj->data);
- $rnames = array();
- if (is_array($obj)) {
- foreach ($obj as $key => $val) {
- $repo = new Repo($this->url, $this->token);
- $repo->json_set_property($val);
- $this->add($repo, $repo->rfull_name);
- array_push($rnames, $repo->rfull_name);
- }
- }
- return $rnames;
- }
- }
|