1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EasyWeChat\Kernel\Traits; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Contracts\Arrayable; |
15
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
16
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidConfigException; |
17
|
|
|
use EasyWeChat\Kernel\Http\Response; |
18
|
|
|
use EasyWeChat\Kernel\Support\Collection; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait ResponseCastable. |
23
|
|
|
* |
24
|
|
|
* @author overtrue <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
trait ResponseCastable |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
30
|
|
|
* @param string|null $type |
31
|
|
|
* |
32
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
33
|
|
|
* |
34
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
35
|
|
|
*/ |
36
|
17 |
|
protected function castResponseToType(ResponseInterface $response, $type = null) |
37
|
|
|
{ |
38
|
17 |
|
$response = Response::buildFromPsrResponse($response); |
39
|
17 |
|
$response->getBody()->rewind(); |
40
|
|
|
|
41
|
17 |
|
switch ($type ?? 'array') { |
42
|
17 |
|
case 'collection': |
|
|
|
|
43
|
3 |
|
return $response->toCollection(); |
44
|
16 |
|
case 'array': |
|
|
|
|
45
|
16 |
|
return $response->toArray(); |
46
|
2 |
|
case 'object': |
47
|
1 |
|
return $response->toObject(); |
48
|
2 |
|
case 'raw': |
49
|
2 |
|
return $response; |
50
|
|
|
default: |
51
|
1 |
|
if (!is_subclass_of($type, Arrayable::class)) { |
52
|
1 |
|
throw new InvalidConfigException(sprintf( |
53
|
1 |
|
'Config key "response_type" classname must be an instanceof %s', |
54
|
1 |
|
Arrayable::class |
55
|
|
|
)); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return new $type($response); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $response |
64
|
|
|
* @param string|null $type |
65
|
|
|
* |
66
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
67
|
|
|
* |
68
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
69
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
70
|
|
|
*/ |
71
|
6 |
|
protected function detectAndCastResponseToType($response, $type = null) |
72
|
|
|
{ |
73
|
|
|
switch (true) { |
74
|
6 |
|
case $response instanceof ResponseInterface: |
75
|
1 |
|
$response = Response::buildFromPsrResponse($response); |
76
|
|
|
|
77
|
1 |
|
break; |
78
|
|
|
case $response instanceof Arrayable: |
79
|
1 |
|
$response = new Response(200, [], json_encode($response->toArray())); |
80
|
|
|
|
81
|
1 |
|
break; |
82
|
6 |
|
case ($response instanceof Collection) || is_array($response) || is_object($response): |
83
|
6 |
|
$response = new Response(200, [], json_encode($response)); |
84
|
|
|
|
85
|
6 |
|
break; |
86
|
1 |
|
case is_scalar($response): |
87
|
1 |
|
$response = new Response(200, [], $response); |
88
|
|
|
|
89
|
1 |
|
break; |
90
|
|
|
default: |
91
|
1 |
|
throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response))); |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
return $this->castResponseToType($response, $type); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.