Completed
Push — master ( 32869d...8a9333 )
by Carlos
04:33 queued 30s
created

Response::toObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Http;
13
14
use EasyWeChat\Kernel\Support\Collection;
15
use EasyWeChat\Kernel\Support\XML;
16
use GuzzleHttp\Psr7\Response as GuzzleResponse;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * Class Response.
21
 *
22
 * @author overtrue <[email protected]>
23
 */
24
class Response extends GuzzleResponse
25
{
26
    /**
27
     * @return string
28
     */
29 21
    public function getBodyContents()
30
    {
31 21
        $this->getBody()->rewind();
32 21
        $contents = $this->getBody()->getContents();
33 21
        $this->getBody()->rewind();
34
35 21
        return $contents;
36
    }
37
38
    /**
39
     * @param \Psr\Http\Message\ResponseInterface $response
40
     *
41
     * @return \EasyWeChat\Kernel\Http\Response
42
     */
43 21
    public static function buildFromPsrResponse(ResponseInterface $response)
44
    {
45 21
        return new static(
46 21
            $response->getStatusCode(),
47 21
            $response->getHeaders(),
48 21
            $response->getBody(),
49 21
            $response->getProtocolVersion(),
50 21
            $response->getReasonPhrase()
51
        );
52
    }
53
54
    /**
55
     * Build to json.
56
     *
57
     * @return string
58
     */
59 3
    public function toJson()
60
    {
61 3
        return json_encode($this->toArray());
62
    }
63
64
    /**
65
     * Build to array.
66
     *
67
     * @return array
68
     */
69 20
    public function toArray()
70
    {
71 20
        $content = $this->removeControlCharacters($this->getBodyContents());
72
73 20
        if (false !== stripos($this->getHeaderLine('Content-Type'), 'xml') || 0 === stripos($content, '<xml')) {
74 2
            return XML::parse($content);
75
        }
76
77 18
        $array = json_decode($content, true, 512, JSON_BIGINT_AS_STRING);
78
79 18
        if (JSON_ERROR_NONE === json_last_error()) {
80 18
            return (array) $array;
81
        }
82
83 2
        return [];
84
    }
85
86
    /**
87
     * Get collection data.
88
     *
89
     * @return \EasyWeChat\Kernel\Support\Collection
90
     */
91 4
    public function toCollection()
92
    {
93 4
        return new Collection($this->toArray());
94
    }
95
96
    /**
97
     * @return object
98
     */
99 3
    public function toObject()
100
    {
101 3
        return json_decode($this->toJson());
102
    }
103
104
    /**
105
     * @return bool|string
106
     */
107 1
    public function __toString()
108
    {
109 1
        return $this->getBodyContents();
110
    }
111
112
    /**
113
     * @param string $content
114
     *
115
     * @return string
116
     */
117 20
    protected function removeControlCharacters(string $content)
118
    {
119 20
        return \preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $content);
120
    }
121
}
122