Completed
Push — master ( 28f1e1...eed622 )
by Carlos
07:52
created

ResponseCastable::castResponseToType()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 6
nop 2
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
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
    protected function castResponseToType(ResponseInterface $response, $type = null)
37
    {
38
        $response = Response::buildFromPsrResponse($response);
39
        $response->getBody()->rewind();
40
41
        switch ($type ?? 'array') {
42
            case 'collection':
43
                return $response->toCollection();
44
            case 'array':
45
                return $response->toArray();
46
            case 'object':
47
                return $response->toObject();
48
            case 'raw':
49
                return $response;
50
            default:
51
                if (!is_subclass_of($type, Arrayable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \EasyWeChat\Kernel\Contracts\Arrayable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
52
                    throw new InvalidConfigException(sprintf('Config key "response_type" classname must be an instanceof %s', Arrayable::class));
53
                }
54
55
                return new $type($response);
56
        }
57
    }
58
59
    /**
60
     * @param mixed       $response
61
     * @param string|null $type
62
     *
63
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|string
64
     *
65
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
66
     */
67
    protected function detectAndCastResponseToType($response, $type = null)
68
    {
69
        switch (true) {
70
            case $response instanceof ResponseInterface:
71
                $response = Response::buildFromPsrResponse($response);
72
                break;
73
            case ($response instanceof Collection) || is_array($response) || is_object($response):
74
                $response = new Response(200, [], json_encode($response));
75
                break;
76
            case is_scalar($response):
77
                $response = new Response(200, [], $response);
78
                break;
79
            default:
80
                throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
81
        }
82
83
        return $this->castResponseToType($response, $type);
84
    }
85
}
86