diff --git a/src/Client.php b/src/Client.php index 80daa27..dbd4840 100644 --- a/src/Client.php +++ b/src/Client.php @@ -35,6 +35,7 @@ class Client /** @var array $retryStatusCodes */ private array $retryStatusCodes = [500, 503]; + private mixed $jsonEncodeFlags; /** * @param string $key @@ -121,7 +122,41 @@ public function setMaxRetries(int $maxRetries): self $this->maxRetries = $maxRetries; return $this; } + /** + * set json_encode flags. + * + * @param array $flags + * @return self + */ + public function setJsonEncodeFlags(array $flags): self + { + $this->jsonEncodeFlags = implode('|', $flags); + return $this; + } + + /** + * Encode to json. + * + * @param array $data + * @return string + * @throws \Exception If JSON encoding fails + */ + private function jsonEncode(array $data): string + { + $result = null; + if (!empty($this->jsonEncodeFlags)) { + $result = json_encode($data, $this->jsonEncodeFlags); + } else { + $result = json_encode($data); + } + + if ($result === false) { + throw new Exception('Failed to encode data to JSON: ' . json_last_error_msg()); + } + + return $result; + } /** * Set the retry delay in milliseconds. * @@ -215,7 +250,7 @@ public function fetch( if (isset($this->headers['content-type']) && $body !== null) { $body = match ($this->headers['content-type']) { - self::CONTENT_TYPE_APPLICATION_JSON => json_encode($body), + self::CONTENT_TYPE_APPLICATION_JSON => $this->jsonEncode($body), self::CONTENT_TYPE_APPLICATION_FORM_URLENCODED, self::CONTENT_TYPE_MULTIPART_FORM_DATA => self::flatten($body), self::CONTENT_TYPE_GRAPHQL => $body[0], default => $body,