Skip to content

Commit

Permalink
Add possibility to pass bigint keys for json that we need replace quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
donhardman committed May 17, 2021
1 parent 60d8a12 commit 6bdc781
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait RequestTrait {
protected int $request_timeout = 30;
protected int $request_ssl_verify = 0;
protected int $request_keepalive = 20;
protected string $request_useragent = 'KISS/Request v0.8.0';
protected string $request_useragent = 'KISS/Request v0.8.1';

// The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent.
protected ?string $request_encoding = '';
Expand All @@ -24,6 +24,8 @@ trait RequestTrait {
// { host, port, user, password, type }
protected array $request_proxy = [];

protected array $request_json_bigint_keys = [];

protected array $request_handlers = [];
protected ?CurlMultiHandle $request_mh = null;

Expand Down Expand Up @@ -192,7 +194,7 @@ private function process(CurlHandle $ch): array {
protected function requestEncode(array $payload): string {
return match ($this->request_type) {
'msgpack' => msgpack_pack($payload),
'json' => json_encode($payload),
'json' => $this->encodeJson($payload),
'binary' => BinaryCodec::create()->pack($payload),
default => http_build_query($payload, false, '&'),
};
Expand All @@ -213,4 +215,13 @@ protected function requestDecode(string $response): mixed {
default => $response,
};
}

protected function encodeJson(mixed $data): string {
$json = json_encode($data);
if ($this->request_json_bigint_keys) {
$json = preg_replace('/"(' . implode('|', $this->request_json_bigint_keys) . ')":"([0-9]+)"/ius', '"$1":$2', $json);
}

return $json;
}
}

0 comments on commit 6bdc781

Please sign in to comment.