owner = $owner; parent::__construct($api_url, $api_token); } /** * @see Base */ protected function set_scope(string $method) { switch ($method) { case "get": case "load": $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->username) . "/orgs"; return true; default: return false; } } /** * Create a new organization * * If arguments are given, the User will be created, * otherise it will return an initialized object, * leaving the programmer to create the user. * * @see Base * @return User */ public function create(...$args) { $org = new Org($this->url, $this->token, $this->owner); if (count($args) > 0) { $org->create(...$args); $this->add($org, $org->username); } return $org; } /** * Get an organization by indentifier. * * Method will first look through organizations * already loaded. If not found it will return a * new object. * * Method does not ensure the organization in loaded * from Gogs so the user should call `->load()` on * returned element. * * @param string $s * @return Org */ public function get(string $s) { if (($org = $this->by_key($s))) return $org; return new Org($this->url, $this->token, $this->owner, $s); } /** * Search for an organization. * * Params can be an array of * ```php * $orgs->search(array( * "name" => "name", // alt. "q". required * "limit" => 10, // not required, default: 10 * )); * ``` * By now, this method can be intensive, as it will load * every organization and then do a match on each entry. * * @param array $params Search parameters * @return Orgs * @throws Exception\SearchParamException on missing parameters */ public function search(array $params = array()) { if (!isset($params["name"]) && !isset($params["q"])) throw new Exception\SearchParamException("Missing param |"); $q = isset($params["name"]) ? $params["name"] : $params["q"]; $l = isset($params["limit"]) ? $params["limit"] : 10; $this->load(); $orgs = new Orgs($this->url, $this->token, $this->owner); foreach ($this->all() as $key => $org) { if ($org->search($q)) $orgs->add($org, $org->username); if ($orgs->len() == $l) break; } return $orgs; } /** * @see Base */ protected function json_set_property($obj) { $rnames = array(); foreach($obj as $val) { $org = new Org($this->url, $this->token, $this->owner, $val->username); $org->json_set_property($val); $this->add($org, $val->username); $rnames[] = $val->username; } return $rnames; } /** * @see Collection */ public function sort_by(int $flag = Collection::SORT_INDEX) { return $this->sort("ksort"); } } }