123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace Gogs\API\Request {
- /**
- * Base class for request types.
- *
- * Each request shall inherit this class to ensure
- * it will have the correct methods required by interface,
- * and get the cURL functionality.
- *
- * @deprecated since 0.1, to be removed in 3.0
- * @abstact
- */
- 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;
- }
- /**
- * @param string $api_url The URL to the API.
- * @param string $api_token A token for an authorized user
- */
- public function __construct(string $api_url, string $api_token) {
- $this->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() {}
- }
- }
- ?>
|