requestbase.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php namespace Client\Request;
  2. abstract class RequestBase implements RequestInterface {
  3. protected $loaded = false;
  4. protected $scope;
  5. use \Lib\Curl {
  6. get as protected mget;
  7. }
  8. public function __construct($api_url, $api_token) {
  9. $this->url = $api_url;
  10. $this->token = $api_token;
  11. $this->set_scope();
  12. }
  13. public function load($force = false) {
  14. if ($this->loaded && !$force)
  15. return $this;
  16. $jenc = $this->mget();
  17. $this->json_set_property($this->json_decode($jenc));
  18. $this->loaded = true;
  19. return $this;
  20. }
  21. public function get($scope) {
  22. throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant lookup " . $scope);
  23. }
  24. public function create() {
  25. throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant create");
  26. }
  27. public function patch() {
  28. throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant create");
  29. }
  30. public function delete() {
  31. throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant create");
  32. }
  33. private function json_decode($jenc) {
  34. $obj = json_decode($jenc);
  35. if (($err = json_last_error()) != JSON_ERROR_NONE)
  36. throw new RequestErrorException(json_last_error_msg(), $err);
  37. return $obj;
  38. }
  39. abstract protected function json_set_property($obj);
  40. abstract protected function set_scope();
  41. public function __destruct() {}
  42. }
  43. ?>