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