From f170ef7284e494260316af6a4299d57db0751a37 Mon Sep 17 00:00:00 2001 From: Brecht De Winne Date: Tue, 14 May 2024 15:17:32 +0200 Subject: [PATCH 1/3] feature: Provide compatibility with psr/http-message 2.0 --- lib/SparkPost/SparkPostResponse.php | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/SparkPost/SparkPostResponse.php b/lib/SparkPost/SparkPostResponse.php index 402a2b2..d953787 100644 --- a/lib/SparkPost/SparkPostResponse.php +++ b/lib/SparkPost/SparkPostResponse.php @@ -2,6 +2,7 @@ namespace SparkPost; +use Psr\Http\Message\MessageInterface; use Psr\Http\Message\ResponseInterface as ResponseInterface; use Psr\Http\Message\StreamInterface as StreamInterface; @@ -39,84 +40,83 @@ public function getRequest() } /** - * Returns the body. + * Gets the body in json format. * - * @return array $body - the json decoded body from the http response + * @return array Decoded body. */ - public function getBody() - { - $body = $this->response->getBody(); - $body_string = $body->__toString(); - - $json = json_decode($body_string, true); + public function getBodyAsJson() : array { + return json_decode($this->getBody()->getContents(), true); + } - return $json; + public function getBody() : StreamInterface + { + return $this->response->getBody(); } /** * pass these down to the response given in the constructor. */ - public function getProtocolVersion() + public function getProtocolVersion() : string { return $this->response->getProtocolVersion(); } - public function withProtocolVersion($version) + public function withProtocolVersion($version) : MessageInterface { return $this->response->withProtocolVersion($version); } - public function getHeaders() + public function getHeaders() : array { return $this->response->getHeaders(); } - public function hasHeader($name) + public function hasHeader($name) : bool { return $this->response->hasHeader($name); } - public function getHeader($name) + public function getHeader($name) : array { return $this->response->getHeader($name); } - public function getHeaderLine($name) + public function getHeaderLine($name) : string { return $this->response->getHeaderLine($name); } - public function withHeader($name, $value) + public function withHeader($name, $value) : MessageInterface { return $this->response->withHeader($name, $value); } - public function withAddedHeader($name, $value) + public function withAddedHeader($name, $value) : MessageInterface { return $this->response->withAddedHeader($name, $value); } - public function withoutHeader($name) + public function withoutHeader($name) : MessageInterface { return $this->response->withoutHeader($name); } - public function withBody(StreamInterface $body) + public function withBody(StreamInterface $body) : MessageInterface { return $this->response->withBody($body); } - public function getStatusCode() + public function getStatusCode() : int { return $this->response->getStatusCode(); } - public function withStatus($code, $reasonPhrase = '') + public function withStatus($code, $reasonPhrase = '') : ResponseInterface { return $this->response->withStatus($code, $reasonPhrase); } - public function getReasonPhrase() + public function getReasonPhrase() : string { return $this->response->getReasonPhrase(); } From 716f95ab9adbf3fda9ad638878666a393e216fc4 Mon Sep 17 00:00:00 2001 From: Brecht De Winne Date: Thu, 23 May 2024 10:11:19 +0200 Subject: [PATCH 2/3] feature: replace the HttpClient interface with ClientInterface --- composer.json | 3 ++- lib/SparkPost/SparkPost.php | 11 +++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 1736421..29a6a95 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "php-http/httplug": "^1.0 || ^2.0", "php-http/message": "^1.0", "php-http/client-implementation": "^1.0", - "php-http/discovery": "^1.0" + "php-http/discovery": "^1.0", + "psr/http-client": "^1.0" }, "require-dev": { "phpunit/phpunit": "^8.0 || ^9.0", diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 140caf7..cc979b2 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -2,10 +2,9 @@ namespace SparkPost; -use Http\Client\HttpClient; -use Http\Client\HttpAsyncClient; use Http\Discovery\MessageFactoryDiscovery; use Http\Message\RequestFactory; +use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; class SparkPost @@ -16,7 +15,7 @@ class SparkPost private $version = '2.3.0'; /** - * @var HttpClient|HttpAsyncClient used to make requests + * @var ClientInterface used to make requests */ private $httpClient; @@ -270,14 +269,14 @@ public function getUrl($path, $params = []) /** * Sets $httpClient to be used for request. * - * @param HttpClient|HttpAsyncClient $httpClient - the client to be used for request + * @param ClientInterface $httpClient - the client to be used for request * * @return SparkPost */ public function setHttpClient($httpClient) { - if (!($httpClient instanceof HttpAsyncClient || $httpClient instanceof HttpClient)) { - throw new \LogicException(sprintf('Parameter to SparkPost::setHttpClient must be instance of "%s" or "%s"', HttpClient::class, HttpAsyncClient::class)); + if (!($httpClient instanceof ClientInterface)) { + throw new \LogicException(sprintf('Parameter to SparkPost::setHttpClient must be instance of "%s"', ClientInterface::class)); } $this->httpClient = $httpClient; From 54d3210b7b8567a85407f965e54f17732f971be1 Mon Sep 17 00:00:00 2001 From: Brecht De Winne Date: Thu, 23 May 2024 10:16:09 +0200 Subject: [PATCH 3/3] bugfix: forgot replacement --- lib/SparkPost/SparkPost.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index cc979b2..d2d6229 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -51,7 +51,7 @@ class SparkPost /** * Sets up the SparkPost instance. * - * @param HttpClient $httpClient - An httplug client or adapter + * @param ClientInterface $httpClient - An httplug client or adapter * @param array $options - An array to overide default options or a string to be used as an API key */ public function __construct($httpClient, array $options)