appName = $appName; $key = "kintone.host"; $host = config($key); if (!$host) { throw new ConfigException("kintone.host", $host); } $key = "kintone.applications." . $appName . ".apiToken"; $apiToken = config($key); if (!$apiToken) { throw new ConfigException($key, $apiToken); } $key = "kintone.applications." . $appName . ".appId"; $appId = config($key); if (!$appId) { throw new ConfigException($key, $appId); } $this->host = $host; $this->apiToken = $apiToken; $this->appId = $appId; } public function setFields(array $fields): static { $this->fields = [ ...$fields, ...self::DEFAULT_FIELDS, ]; return $this; } public function __destruct() { $this->deleteCursor(); } /** * @param integer $id * @param TValue $result * @return Response */ public function find(int $id, KintoneModel &$result): Response { $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, ])->get($this->getRecordUrl(), [ "app" => $this->appId, "id" => $id, 'fields' => $this->fields, ]); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage()); throw $e; } } $result->setDataFromRecordResponse($response['record']); return $response; } /** * @param TValue $model * @return void */ public function update(KintoneModel &$model) { $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, ])->put($this->getRecordUrl(), [ "app" => $this->appId, "id" => $model->getRecordId(), "record" => $model->getApiLayout(), "revision" => $model->getRevision(), ]); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage()); throw $e; } } $model->clean($response["revision"]); return $response; } /** * @param TValue $model * @return void */ public function create(KintoneModel &$model) { $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, ])->post($this->getRecordUrl(), [ "app" => $this->appId, "record" => $model->getApiLayout(), ]); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage()); throw $e; } } $model->clean($response["revision"]); $model->setRecordId($response["id"]); return $response; } public function createCursor(KintoneRecordQuery|null $query = null) { $data = [ "app" => $this->appId, 'fields' => $this->fields, ]; if ($query !== null) { $data["query"] = $query->toQuery(); } $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, ])->post($this->getCursorUrl(), $data); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage(), ['data' => $data]); throw $e; } } $cursor = $response["id"]; $totalCount = $response["totalCount"]; if (0 < $totalCount) { $this->hasNext = true; $this->cursor = $cursor; $this->cursorDataCount = $totalCount; } else { $this->cursor = null; $this->hasNext = false; $this->cursorDataCount = 0; } return $response; } /** * @return Collection */ public function next() { if (!$this->hasNext) { return collect(); } $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, ])->get($this->getCursorUrl(), [ "id" => $this->cursor, ]); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage()); throw $e; } } $ret = collect(); $hasNext = $response["next"]; if (!$hasNext) { $this->cursor = null; $this->hasNext = false; $this->cursorDataCount = 0; } foreach ($response["records"] as $data) { /** * @var KintoneModel $model */ $model = new $this->appName(); $model->setDataFromRecordResponse($data); $ret->push($model); } return $ret; } /** * @return Collection */ public function all(KintoneRecordQuery|null $query = null, array|null $fields = null) { if ($this->cursor !== null) { $this->deleteCursor(); } $this->createCursor($query, $fields); $list = collect(); while (true) { $ret = $this->next(); foreach ($ret as $ele) { $list->push($ele); } if ($this->cursor === null) { break; } } return $list; } public function deleteCursor() { if ($this->cursor === null) { return; } $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, ])->delete($this->getCursorUrl(), [ "id" => $this->cursor, ]); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage()); throw $e; } } $this->cursor = null; $this->hasNext = false; $this->cursorDataCount = 0; } public function fileGet(string $fileKey) { $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, "Content-Type" => "application/json", ])->get($this->getCursorUrl(), [ "fileKey" => $fileKey, ]); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage()); throw $e; } } return $response; } public function filePut($file): string { $response = Http::withHeaders([ "X-Cybozu-API-Token" => $this->apiToken, "Content-Type" => "multipart/form-data", ])->post($this->getCursorUrl(), $file); if ($response->failed()) { $e = $response->toException(); if ($e instanceof Exception) { Log::error($e->getMessage()); throw $e; } } return $response['']; } private function getRecordUrl() { return $this->getUrl(self::URL_RECORD); } private function getCursorUrl() { return $this->getUrl(self::URL_CURSOL); } private function getFileUrl() { return $this->getUrl(self::URL_FILE); } private function getUrl(string $path) { return $this->host . $path; } }