Base.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Base class for request types.
  5. *
  6. * Each request shall inherit this class to ensure
  7. * it will have the correct methods required by interface,
  8. * and get the cURL functionality.
  9. *
  10. * @deprecated since 0.1, to be removed in 3.0
  11. * @abstact
  12. */
  13. abstract class Base implements RequestInterface {
  14. protected $loaded = false;
  15. protected $scope;
  16. use \Gogs\Lib\Curl\Client {
  17. get as private mget;
  18. post as private mpost;
  19. delete as private mdelete;
  20. }
  21. /**
  22. * @param string $api_url The URL to the API.
  23. * @param string $api_token A token for an authorized user
  24. */
  25. public function __construct(string $api_url, string $api_token) {
  26. $this->url = $api_url;
  27. $this->token = $api_token;
  28. }
  29. /**
  30. * Load an object.
  31. *
  32. * If `$force = true` the object will be fetched
  33. * from the Gogs API again.
  34. *
  35. * @deprecated since 2.0 to be removed in 3.0
  36. * @return object
  37. */
  38. final public function load(bool $force = false) {
  39. $this->set_scope("get");
  40. if ($this->loaded && !$force)
  41. return $this;
  42. $jenc = $this->mget($this->scope);
  43. $this->json_set_property($this->json_decode($jenc));
  44. $this->loaded = true;
  45. return $this;
  46. }
  47. /**
  48. * Perform a GET-request against the Gogs API.
  49. *
  50. * Ensure the correct scope i set first, with
  51. * ```php
  52. * $this->set_scope("*valid scope*"); // e.g create
  53. * ```
  54. *
  55. * @param array $params The parameters
  56. * @return string
  57. */
  58. final protected function method_get(array $params = array()) {
  59. return $this->mget($this->scope, $params);
  60. }
  61. /**
  62. * Perform a POST-request against the Gogs API.
  63. *
  64. * Ensure the correct scope i set first, with
  65. * ```php
  66. * $this->set_scope("*valid scope*"); // e.g create
  67. * ```
  68. *
  69. * @param array $params The parameters
  70. * @return string
  71. */
  72. final protected function method_post(array $params = array()) {
  73. return $this->mpost($this->scope, $params);
  74. }
  75. /**
  76. * Perform a DELETE-request against the Gogs API.
  77. *
  78. * Ensure the correct scope i set first, with
  79. * ```php
  80. * $this->set_scope("*valid scope*"); // e.g delete
  81. * ```
  82. *
  83. * @return string
  84. */
  85. final protected function method_delete() {
  86. return $this->mdelete($this->scope);
  87. }
  88. /**
  89. * Get object references by identifier.
  90. *
  91. * @param string $s Identifier to look up.
  92. * @return null
  93. */
  94. public function get(string $s) {
  95. if (!$this->set_scope("get"))
  96. throw new Exception\NotImplementedException("::get:: Not implemented for class");
  97. return null;
  98. }
  99. /**
  100. * Create object inherited by class.
  101. *
  102. * Child class must add a scope for 'create' and ensure child is not *loaded*,
  103. * otherwise will `create` throw an exception.
  104. *
  105. * @param string $args yeah, well
  106. * @return true
  107. */
  108. public function create(...$args) {
  109. if ($this->loaded)
  110. throw new Exception\InvalidMethodRequestException("::create:: Cant create on an git-initialized object. Create new object.");
  111. if (!$this->set_scope("create"))
  112. throw new Exception\NotImplementedException("::create:: Not implemented for class");
  113. $ret = $this->method_post(...$args);
  114. $this->json_set_property($this->json_decode($ret));
  115. return true;
  116. }
  117. /**
  118. * Patch (update) object
  119. *
  120. * @throws Exception\InvalidMethodRequestException
  121. * @throws Exception\NotImplementedException
  122. */
  123. public function patch() {
  124. if (!$this->loaded)
  125. throw new Exception\InvalidMethodRequestException("::patch:: Cant patch an git-uninitialized object. Load it first.");
  126. if (!$this->set_scope("patch"))
  127. throw new Exception\NotImplementedException("::patch:: Not implemented for class");
  128. }
  129. public function delete() {
  130. if (!$this->set_scope("delete"))
  131. throw new Exception\NotImplementedException("::delete:: Not implemented for class");
  132. return $this->method_delete();
  133. }
  134. protected function json_decode(string $jenc) {
  135. $obj = json_decode($jenc);
  136. $this->json_error();
  137. return $obj;
  138. }
  139. protected function json_encode(iterable $jdec) {
  140. $jenc = json_encode($jdec);
  141. $this->json_error();
  142. return $jenc;
  143. }
  144. protected function json_error() {
  145. if (($err = json_last_error()) != JSON_ERROR_NONE)
  146. throw new Exception\RequestErrorException(json_last_error_msg(), $err);
  147. }
  148. abstract protected function json_set_property($obj);
  149. abstract protected function set_scope(string $method);
  150. public function __destruct() {}
  151. }
  152. }
  153. ?>