1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CloudyCity\TencentMarketingSDK\Kernel\Traits; |
4
|
|
|
|
5
|
|
|
use CloudyCity\TencentMarketingSDK\Kernel\Contracts\Arrayable; |
6
|
|
|
use CloudyCity\TencentMarketingSDK\Kernel\Exceptions\InvalidArgumentException; |
7
|
|
|
use CloudyCity\TencentMarketingSDK\Kernel\Http\Response; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait ResponseCastable from easywechat. |
13
|
|
|
*/ |
14
|
|
|
trait ResponseCastable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $responseType; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
protected function getResponseType() |
25
|
|
|
{ |
26
|
|
|
return $this->responseType; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $responseType |
31
|
|
|
*/ |
32
|
|
|
protected function setResponseType($responseType) |
33
|
|
|
{ |
34
|
|
|
$this->responseType = $responseType; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ResponseInterface $response |
39
|
|
|
* @param string $type |
40
|
|
|
* |
41
|
|
|
* @throws InvalidArgumentException |
42
|
|
|
* |
43
|
|
|
* @return array|Response|ArrayCollection|object|ResponseInterface |
44
|
|
|
*/ |
45
|
|
|
protected function castResponseToType(ResponseInterface $response, $type = 'array') |
46
|
|
|
{ |
47
|
|
|
$response = Response::buildFromPsrResponse($response); |
48
|
|
|
$response->getBody()->rewind(); |
49
|
|
|
|
50
|
|
|
switch ($type) { |
51
|
|
|
case 'collection': |
52
|
|
|
return $response->toCollection(); |
53
|
|
|
case 'array': |
54
|
|
|
return $response->toArray(); |
55
|
|
|
case 'object': |
56
|
|
|
return $response->toObject(); |
57
|
|
|
case 'raw': |
58
|
|
|
return $response; |
59
|
|
|
default: |
60
|
|
|
if (!is_subclass_of($type, Arrayable::class)) { |
61
|
|
|
throw new InvalidArgumentException(sprintf( |
62
|
|
|
'"$responseType" classname must be an instanceof %s', |
63
|
|
|
Arrayable::class |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new $type($response); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $response |
73
|
|
|
* @param string $type |
74
|
|
|
* |
75
|
|
|
* @throws InvalidArgumentException |
76
|
|
|
* |
77
|
|
|
* @return array|Response|ArrayCollection|object|ResponseInterface |
78
|
|
|
*/ |
79
|
|
|
protected function detectAndCastResponseToType($response, $type = 'array') |
80
|
|
|
{ |
81
|
|
|
switch (true) { |
82
|
|
|
case $response instanceof ResponseInterface: |
83
|
|
|
$response = Response::buildFromPsrResponse($response); |
84
|
|
|
|
85
|
|
|
break; |
86
|
|
|
case $response instanceof Arrayable: |
87
|
|
|
$response = new Response(200, [], json_encode($response->toArray())); |
88
|
|
|
|
89
|
|
|
break; |
90
|
|
|
case ($response instanceof ArrayCollection) || is_array($response) || is_object($response): |
91
|
|
|
$response = new Response(200, [], json_encode($response)); |
92
|
|
|
|
93
|
|
|
break; |
94
|
|
|
case is_scalar($response): |
95
|
|
|
$response = new Response(200, [], (string) $response); |
96
|
|
|
|
97
|
|
|
break; |
98
|
|
|
default: |
99
|
|
|
throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response))); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->castResponseToType($response, $type); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|