url = $api_url; $this->token = $api_token; } /** * Load an object. * * If `$force = true` the object will be fetched * from the Gogs API again. * * @deprecated since 2.0 to be removed in 3.0 * @return object */ final public function load(bool $force = false) { $this->set_scope("get"); if ($this->loaded && !$force) return $this; $jenc = $this->mget($this->scope); $this->json_set_property($this->json_decode($jenc)); $this->loaded = true; return $this; } /** * Perform a GET-request against the Gogs API. * * Ensure the correct scope i set first, with * ```php * $this->set_scope("*valid scope*"); // e.g create * ``` * * @param array $params The parameters * @return string */ final protected function method_get(array $params = array()) { return $this->mget($this->scope, $params); } /** * Perform a POST-request against the Gogs API. * * Ensure the correct scope i set first, with * ```php * $this->set_scope("*valid scope*"); // e.g create * ``` * * @param array $params The parameters * @return string */ final protected function method_post(array $params = array()) { return $this->mpost($this->scope, $params); } /** * Perform a DELETE-request against the Gogs API. * * Ensure the correct scope i set first, with * ```php * $this->set_scope("*valid scope*"); // e.g delete * ``` * * @return string */ final protected function method_delete() { return $this->mdelete($this->scope); } /** * Get object references by identifier. * * @param string $s Identifier to look up. * @return null */ public function get(string $s) { if (!$this->set_scope("get")) throw new Exception\NotImplementedException("::get:: Not implemented for class"); return null; } /** * Create object inherited by class. * * Child class must add a scope for 'create' and ensure child is not *loaded*, * otherwise will `create` throw an exception. * * @param string $args yeah, well * @return true */ public function create(...$args) { if ($this->loaded) throw new Exception\InvalidMethodRequestException("::create:: Cant create on an git-initialized object. Create new object."); if (!$this->set_scope("create")) throw new Exception\NotImplementedException("::create:: Not implemented for class"); $ret = $this->method_post(...$args); $this->json_set_property($this->json_decode($ret)); return true; } /** * Patch (update) object * * @throws Exception\InvalidMethodRequestException * @throws Exception\NotImplementedException */ public function patch() { if (!$this->loaded) throw new Exception\InvalidMethodRequestException("::patch:: Cant patch an git-uninitialized object. Load it first."); if (!$this->set_scope("patch")) throw new Exception\NotImplementedException("::patch:: Not implemented for class"); } public function delete() { if (!$this->set_scope("delete")) throw new Exception\NotImplementedException("::delete:: Not implemented for class"); return $this->method_delete(); } protected function json_decode(string $jenc) { $obj = json_decode($jenc); $this->json_error(); return $obj; } protected function json_encode(iterable $jdec) { $jenc = json_encode($jdec); $this->json_error(); return $jenc; } protected function json_error() { if (($err = json_last_error()) != JSON_ERROR_NONE) throw new Exception\RequestErrorException(json_last_error_msg(), $err); } abstract protected function json_set_property($obj); abstract protected function set_scope(string $method); public function __destruct() {} } } ?>