Response   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 22
c 1
b 0
f 0
dl 0
loc 86
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 15 4
A buildFromPsrResponse() 0 8 1
A __toString() 0 3 1
A toCollection() 0 3 1
A toJson() 0 3 1
A toObject() 0 3 1
A getBodyContents() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/http.
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 Overtrue\Http\Responses;
13
14
use GuzzleHttp\Psr7\Response as GuzzleResponse;
15
use Overtrue\Http\Support\Collection;
16
use Overtrue\Http\Support\XML;
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
    public function getBodyContents(): string
30
    {
31
        $this->getBody()->rewind();
32
        $contents = $this->getBody()->getContents();
33
        $this->getBody()->rewind();
34
35
        return $contents;
36
    }
37
38
    /**
39
     * @param \Psr\Http\Message\ResponseInterface $response
40
     *
41
     * @return \Overtrue\Http\Responses\Response
42
     */
43
    public static function buildFromPsrResponse(ResponseInterface $response): self
44
    {
45
        return new static(
46
            $response->getStatusCode(),
47
            $response->getHeaders(),
48
            $response->getBody(),
49
            $response->getProtocolVersion(),
50
            $response->getReasonPhrase()
51
        );
52
    }
53
54
    /**
55
     * Build to json.
56
     *
57
     * @return string
58
     */
59
    public function toJson(): string
60
    {
61
        return json_encode($this->toArray());
62
    }
63
64
    /**
65
     * Build to array.
66
     *
67
     * @return array
68
     */
69
    public function toArray(): array
70
    {
71
        $content = $this->getBodyContents();
72
73
        if (false !== stripos($this->getHeaderLine('Content-Type'), 'xml') || 0 === stripos($content, '<xml')) {
74
            return XML::parse($content);
75
        }
76
77
        $array = json_decode($this->getBodyContents(), true);
78
79
        if (JSON_ERROR_NONE === json_last_error()) {
80
            return (array) $array;
81
        }
82
83
        return [];
84
    }
85
86
    /**
87
     * Get collection data.
88
     *
89
     * @return \Overtrue\Http\Support\Collection
90
     */
91
    public function toCollection(): \Overtrue\Http\Support\Collection
92
    {
93
        return new Collection($this->toArray());
94
    }
95
96
    /**
97
     * @return object
98
     */
99
    public function toObject(): object
100
    {
101
        return json_decode($this->getBodyContents());
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function __toString(): string
108
    {
109
        return $this->getBodyContents();
110
    }
111
}
112