|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the zibios/wrike-php-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Zbigniew Ślązak |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zibios\WrikePhpLibrary\Transformer\Response\Psr; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Psr\Http\Message\StreamInterface; |
|
16
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\ResponseFormatEnum; |
|
17
|
|
|
use Zibios\WrikePhpLibrary\Transformer\ResponseTransformerInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Abstract Response Transformer for PSR Response from HTTP Client. |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractPsrResponseTransformer implements ResponseTransformerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $responseFormat |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function isSupportedResponseFormat($responseFormat) |
|
30
|
|
|
{ |
|
31
|
4 |
|
return $responseFormat === ResponseFormatEnum::PSR_RESPONSE; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ResponseInterface $response |
|
36
|
|
|
* |
|
37
|
|
|
* @return ResponseInterface |
|
38
|
|
|
*/ |
|
39
|
90 |
|
protected function transformToPsrResponse(ResponseInterface $response) |
|
40
|
|
|
{ |
|
41
|
90 |
|
return $response; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ResponseInterface $response |
|
46
|
|
|
* |
|
47
|
|
|
* @return StreamInterface |
|
48
|
|
|
*/ |
|
49
|
88 |
|
protected function transformToPsrBody(ResponseInterface $response) |
|
50
|
|
|
{ |
|
51
|
88 |
|
return $this->transformToPsrResponse($response)->getBody(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param ResponseInterface $response |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \RuntimeException |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
86 |
|
protected function transformToStringBody(ResponseInterface $response) |
|
62
|
|
|
{ |
|
63
|
86 |
|
return $this->transformToPsrBody($response)->getContents(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ResponseInterface $response |
|
68
|
|
|
* |
|
69
|
|
|
* @throws \RuntimeException |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
84 |
|
protected function transformToArrayBody(ResponseInterface $response) |
|
74
|
|
|
{ |
|
75
|
84 |
|
return json_decode($this->transformToStringBody($response), true); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|