owner = $owner; } /** * @see Base */ protected function set_scope(string $method) { switch ($method) { case "get": case "load": if ($this->owner instanceof Org) $this->scope = "/orgs/" . $this->owner->username . "/repos"; else $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->username ) . "/repos"; break; case "search": $this->scope = "/repos/search"; break; default: return false; } return true; } /** * @see Collection */ public function create(...$args) { $repo = new Repo($this->url, $this->token, $this->owner); if (count($args) > 0) { $repo->create(...$args); $this->add($repo, $repo->full_name); } return $repo; } /** * Searches for a repo. * * If the owner is specified the search will be * limited to the actual user. * * @see Collection * @return Repos */ public function search(array $params = array()) { if (!isset($params["name"]) && !isset($params["q"])) throw new Exception\SearchParamException("Missing param |"); if (isset($params["name"])) { $params["q"] = $params["name"]; unset($params["name"]); } if (!isset($params["user"]) || isset($this->owner)) $params["user"] = isset($this->owner) ? $this->owner->id : 0; if (!isset($params["limit"])) $params["limit"] = 10; $repos = new Repos($this->url, $this->token, $this->owner); if ($this->loaded) { foreach($this->all() as $key => $repo) { if ($repo->search($params["q"])) $repos->add($repo, $key); if ($repos->len() == $params["limit"]) break; } } else { $this->set_scope("search"); $jenc = $this->method_get($params); $jdec = $this->json_decode($jenc); foreach($this->json_set_property($jdec) as $key) $repos->add($this->by_key($key), $key); } return $repos; } /** * Sort repos by `method`. * * Valid methods: * * * SORT_UPDATED: Sort on `updated_at` value * * SORT_CREATED: Sort on `created_at` value * * SORT_OWNER: Sort on `owner` (organization repos etc may appear) * * @param int $flag Defines sorting algorithm to use * @param bool $asc Ascending order * @return \Gogs\Lib\Collection */ public function sort_by(int $flag = Collection::SORT_INDEX, bool $asc = false) { switch ($flag) { case self::SORT_CREATED: return ($sort = $this->sort(function(Repo $a, Repo $b) { $adate = new \DateTime($a->created_at); $bdate = new \DateTime($b->created_at); return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1)); })) ? ($asc ? $sort->reverse() : $sort) : false; case self::SORT_UPDATED: return ($sort = $this->sort(function(Repo $a, Repo $b) { $adate = new \DateTime($a->updated_at); $bdate = new \DateTime($b->updated_at); return ($adate == $bdate ? 0 : ($adate > $bdate ? 1 : -1)); })) ? ($asc ? $sort->reverse() : $sort) : false; case self::SORT_OWNER: return ($sort = $this->sort(function(Repo $a, Repo $b) { return strcmp($a->owner->username, $b->owner->username); })) ? ($asc ? $sort->reverse() : $sort) : false; default: return ($sort = $this->sort("ksort")) ? ($asc ? $sort->reverse() : $sort) : false; } } /** * @see Base */ 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->full_name); $rnames[] = $repo->full_name; } } return $rnames; } } }