123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace Gogs\API\Request {
- abstract class Base implements RequestInterface {
- protected $loaded = false;
- protected $scope;
- use \Gogs\Lib\Curl\Client {
- get as private mget;
- post as private mpost;
- delete as private mdelete;
- }
- public function __construct(string $api_url, string $api_token) {
- $this->url = $api_url;
- $this->token = $api_token;
- }
- 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;
- }
- protected function method_get(array $params = array()) {
- return $this->mget($this->scope, $params);
- }
- protected function method_post(array $params = array()) {
- return $this->mpost($this->scope, $params);
- }
- protected function method_delete() {
- return $this->mdelete($this->scope);
- }
- public function get(string $s) {
- if (!$this->set_scope("get"))
- throw new Exception\NotImplementedException("::get:: Not implemented for class");
- }
- 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;
- }
- 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() {}
- }
- }
- ?>
|