Completed
Push — master ( c52910...66a65c )
by lyu
02:53 queued 47s
created

Response   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 119
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setForceArrayKeys() 0 6 1
A getForceArrayKeys() 0 4 1
A getBodyContents() 0 8 1
A buildFromPsrResponse() 0 10 1
A toJson() 0 4 1
A toArray() 0 6 1
A toCollection() 0 4 1
A toObject() 0 4 1
A __toString() 0 4 1
A removeControlCharacters() 0 4 1
1
<?php
2
3
namespace Kaylyu\Alipay\Kernel\Http;
4
5
use Kaylyu\Alipay\Kernel\Exceptions\Exception;
6
use Kaylyu\Alipay\Kernel\Support\Collection;
7
use Kaylyu\Alipay\Kernel\Support\XML;
8
use GuzzleHttp\Psr7\Response as GuzzleResponse;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class Response.
13
 */
14
class Response extends GuzzleResponse
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $forceArrayKeys = [];
20
21
    /**
22
     * @param $forceArrayKeys
23
     *
24
     * @return $this
25
     */
26
    public function setForceArrayKeys($forceArrayKeys)
27
    {
28
        $this->forceArrayKeys = $forceArrayKeys;
29
30
        return $this;
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function getForceArrayKeys()
37
    {
38
        return $this->forceArrayKeys;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getBodyContents()
45
    {
46
        $this->getBody()->rewind();
47
        $contents = $this->getBody()->getContents();
48
        $this->getBody()->rewind();
49
50
        return $contents;
51
    }
52
53
    /**
54
     * @param ResponseInterface $response
55
     * @author kaylv <[email protected]>
56
     * @return static
57
     */
58
    public static function buildFromPsrResponse(ResponseInterface $response)
59
    {
60
        return new static(
61
            $response->getStatusCode(),
62
            $response->getHeaders(),
63
            $response->getBody(),
64
            $response->getProtocolVersion(),
65
            $response->getReasonPhrase()
66
        );
67
    }
68
69
    /**
70
     * Build to json.
71
     *
72
     * @return string
73
     *
74
     * @throws Exception
75
     */
76
    public function toJson()
77
    {
78
        return json_encode($this->toArray());
79
    }
80
81
    /**
82
     * Build to array.
83
     *
84
     * @return array
85
     *
86
     * @throws Exception
87
     */
88
    public function toArray()
89
    {
90
        $content = $this->removeControlCharacters($this->getBodyContents());
91
92
        return XML::parse($content, $this->forceArrayKeys);
93
    }
94
95
    /**
96
     * Get collection data.
97
     * @author kaylv <[email protected]>
98
     * @return Collection
99
     */
100
    public function toCollection()
101
    {
102
        return new Collection($this->toArray());
103
    }
104
105
    /**
106
     * @return object
107
     *
108
     * @throws Exception
109
     */
110
    public function toObject()
111
    {
112
        return json_decode($this->toJson());
113
    }
114
115
    /**
116
     * @return bool|string
117
     */
118
    public function __toString()
119
    {
120
        return $this->getBodyContents();
121
    }
122
123
    /**
124
     * @param string $content
125
     *
126
     * @return string
127
     */
128
    protected function removeControlCharacters(string $content)
129
    {
130
        return \preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $content);
131
    }
132
}
133