|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ClickUpClient; |
| 4 | + |
| 5 | +// define('DOT', '.'); |
| 6 | +// define('DS', '/'); |
| 7 | + |
| 8 | +class Client |
| 9 | +{ |
| 10 | + private $guzzleClient; |
| 11 | + private const MODELS_NAMESPACE = __NAMESPACE__ . "\\Models\\"; |
| 12 | + private $models_match = [ |
| 13 | + 'TaskList' => 'list' |
| 14 | + ]; |
| 15 | + |
| 16 | + public function __construct($apiToken) |
| 17 | + { |
| 18 | + $this->guzzleClient = new \GuzzleHttp\Client([ |
| 19 | + 'base_uri' => 'https://api.clickup.com/api/v2/', |
| 20 | + 'headers' => [ |
| 21 | + 'Authorization' => $apiToken, |
| 22 | + ], |
| 23 | + ]); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * client |
| 28 | + * |
| 29 | + * @return Client |
| 30 | + */ |
| 31 | + public function client() |
| 32 | + { |
| 33 | + return $this; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * __call |
| 38 | + * |
| 39 | + * @param mixed $name |
| 40 | + * @param mixed $arguments |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function __call(string $name, array $arguments) |
| 44 | + { |
| 45 | + if (class_exists(self::MODELS_NAMESPACE . ucfirst($name)) === false) { |
| 46 | + throw new \Exception("`$name` is not a method or object", 1); |
| 47 | + } |
| 48 | + $model = ucfirst($name); |
| 49 | + $model_namespaced = self::MODELS_NAMESPACE . $model; |
| 50 | + |
| 51 | + if (array_key_exists($model, $this->models_match)) { |
| 52 | + $model = $this->models_match[$model]; |
| 53 | + } |
| 54 | + |
| 55 | + $object = new $model_namespaced($this, $arguments[0] ?? null); |
| 56 | + return $object; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param string $method |
| 61 | + * @param array $params |
| 62 | + * @return mixed |
| 63 | + */ |
| 64 | + public function get($method, $params = []) |
| 65 | + { |
| 66 | + $response = $this->guzzleClient->request('GET', $method, ['query' => $params]); |
| 67 | + return json_decode($response->getBody(), true); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param string $method |
| 72 | + * @param array $body |
| 73 | + * @return mixed |
| 74 | + */ |
| 75 | + public function post($method, $body = []) |
| 76 | + { |
| 77 | + $response = $this->guzzleClient->request('POST', $method, ['json' => $body]); |
| 78 | + return json_decode($response->getBody(), true); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * delete |
| 83 | + * |
| 84 | + * @param mixed $method |
| 85 | + * @return int |
| 86 | + */ |
| 87 | + public function delete($method) |
| 88 | + { |
| 89 | + $response = $this->guzzleClient->request('DELETE', $method); |
| 90 | + return $response->getStatusCode(); |
| 91 | + } |
| 92 | + |
| 93 | + public function put($method, $body = []) |
| 94 | + { |
| 95 | + $response = $this->guzzleClient->request('PUT', $method, ['json' => $body]); |
| 96 | + return json_decode($response->getBody(), true); |
| 97 | + } |
| 98 | +} |
0 commit comments