Base.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Gogs\API\Request {
  3. abstract class Base implements RequestInterface {
  4. protected $loaded = false;
  5. protected $scope;
  6. use \Gogs\Lib\Curl\Client {
  7. get as private mget;
  8. post as private mpost;
  9. delete as private mdelete;
  10. }
  11. public function __construct(string $api_url, string $api_token) {
  12. $this->url = $api_url;
  13. $this->token = $api_token;
  14. }
  15. public function load(bool $force = false) {
  16. $this->set_scope("get");
  17. if ($this->loaded && !$force)
  18. return $this;
  19. $jenc = $this->mget($this->scope);
  20. $this->json_set_property($this->json_decode($jenc));
  21. $this->loaded = true;
  22. return $this;
  23. }
  24. protected function method_get(array $params = array()) {
  25. return $this->mget($this->scope, $params);
  26. }
  27. protected function method_post(array $params = array()) {
  28. return $this->mpost($this->scope, $params);
  29. }
  30. protected function method_delete() {
  31. return $this->mdelete($this->scope);
  32. }
  33. public function get(string $s) {
  34. if (!$this->set_scope("get"))
  35. throw new Exception\NotImplementedException("::get:: Not implemented for class");
  36. }
  37. public function create(...$args) {
  38. if ($this->loaded)
  39. throw new Exception\InvalidMethodRequestException("::create:: Cant create on an git-initialized object. Create new object.");
  40. if (!$this->set_scope("create"))
  41. throw new Exception\NotImplementedException("::create:: Not implemented for class");
  42. $ret = $this->method_post(...$args);
  43. $this->json_set_property($this->json_decode($ret));
  44. return true;
  45. }
  46. public function patch() {
  47. if (!$this->loaded)
  48. throw new Exception\InvalidMethodRequestException("::patch:: Cant patch an git-uninitialized object. Load it first.");
  49. if (!$this->set_scope("patch"))
  50. throw new Exception\NotImplementedException("::patch:: Not implemented for class");
  51. }
  52. public function delete() {
  53. if (!$this->set_scope("delete"))
  54. throw new Exception\NotImplementedException("::delete:: Not implemented for class");
  55. return $this->method_delete();
  56. }
  57. protected function json_decode(string $jenc) {
  58. $obj = json_decode($jenc);
  59. $this->json_error();
  60. return $obj;
  61. }
  62. protected function json_encode(iterable $jdec) {
  63. $jenc = json_encode($jdec);
  64. $this->json_error();
  65. return $jenc;
  66. }
  67. protected function json_error() {
  68. if (($err = json_last_error()) != JSON_ERROR_NONE)
  69. throw new Exception\RequestErrorException(json_last_error_msg(), $err);
  70. }
  71. abstract protected function json_set_property($obj);
  72. abstract protected function set_scope(string $method);
  73. public function __destruct() {}
  74. }
  75. }
  76. ?>