• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Http-Client / 8912297926

28 Jan 2024 09:02AM UTC coverage: 94.855% (-0.6%) from 95.469%
8912297926

push

github

web-flow
Update GitHub workflow and dependencies for PHP 8.3 support (#25)

295 of 311 relevant lines covered (94.86%)

141.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.69
/src/Response.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Http-Client.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Http-Client
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\HttpClient;
18

19
use JBZoo\Data\JSON;
20
use JBZoo\Utils\Xml;
21

22
/**
23
 * @property int        $code
24
 * @property array      $headers
25
 * @property string     $body
26
 * @property null|float $time
27
 */
28
class Response
29
{
30
    protected int      $internalCode    = 0;
31
    protected array    $internalHeaders = [];
32
    protected ?string  $internalBody    = null;
33
    protected ?JSON    $parsedJsonData  = null;
34
    protected ?float   $time            = null;
35
    protected ?Request $originalRequest = null;
36

37
    /**
38
     * @return null|array|float|int|string|string[]
39
     */
40
    public function __get(string $name)
41
    {
42
        if ($name === 'code') {
258✔
43
            return $this->getCode();
186✔
44
        }
45

46
        if ($name === 'headers') {
258✔
47
            return $this->getHeaders();
6✔
48
        }
49

50
        if ($name === 'body') {
258✔
51
            return $this->getBody();
42✔
52
        }
53

54
        if ($name === 'time') {
258✔
55
            return $this->getTime();
258✔
56
        }
57

58
        throw new Exception("Property '{$name}' not defined");
×
59
    }
60

61
    public function setCode(int $code): self
62
    {
63
        $this->internalCode = $code;
270✔
64

65
        return $this;
270✔
66
    }
67

68
    public function getCode(): int
69
    {
70
        return $this->internalCode;
216✔
71
    }
72

73
    public function setHeaders(array $headers): self
74
    {
75
        $result = [];
270✔
76

77
        foreach ($headers as $key => $value) {
270✔
78
            if (\is_array($value)) {
258✔
79
                $value = \implode(';', $value);
258✔
80
            }
81

82
            $result[$key] = $value;
258✔
83
        }
84

85
        $this->internalHeaders = $result;
270✔
86

87
        return $this;
270✔
88
    }
89

90
    public function getHeaders(): array
91
    {
92
        return $this->internalHeaders;
120✔
93
    }
94

95
    public function setBody(string $body): self
96
    {
97
        $this->internalBody   = $body;
276✔
98
        $this->parsedJsonData = null;
276✔
99

100
        return $this;
276✔
101
    }
102

103
    public function getBody(): ?string
104
    {
105
        return $this->internalBody;
78✔
106
    }
107

108
    public function getJSON(): ?JSON
109
    {
110
        if (
111
            $this->parsedJsonData === null
168✔
112
            && $this->internalBody !== null
168✔
113
            && $this->internalBody !== ''
168✔
114
        ) {
115
            $this->parsedJsonData = new JSON($this->internalBody);
168✔
116
        }
117

118
        return $this->parsedJsonData;
168✔
119
    }
120

121
    public function getXml(): JSON
122
    {
123
        try {
124
            $xmlAsArray = Xml::dom2Array(Xml::createFromString($this->internalBody));
12✔
125
        } catch (\Exception $exception) {
×
126
            throw new Exception(
×
127
                "Can't parse xml document from HTTP response. " .
×
128
                "Details: {$exception->getMessage()}",
×
129
            );
×
130
        }
131

132
        return new JSON($xmlAsArray);
12✔
133
    }
134

135
    public function getHeader(string $headerKey, bool $ignoreCase = true): ?string
136
    {
137
        if ($ignoreCase) {
102✔
138
            $headers   = [];
102✔
139
            $headerKey = \strtolower($headerKey);
102✔
140

141
            foreach ($this->getHeaders() as $key => $value) {
102✔
142
                $key           = \strtolower((string)$key);
102✔
143
                $headers[$key] = $value;
102✔
144
            }
145
        } else {
146
            $headers = $this->getHeaders();
×
147
        }
148

149
        return $headers[$headerKey] ?? null;
102✔
150
    }
151

152
    public function setRequest(Request $request): self
153
    {
154
        $this->originalRequest = $request;
270✔
155

156
        return $this;
270✔
157
    }
158

159
    public function getRequest(): ?Request
160
    {
161
        return $this->originalRequest;
42✔
162
    }
163

164
    public function getTime(): ?float
165
    {
166
        return $this->time;
258✔
167
    }
168

169
    public function setTime(float $time): self
170
    {
171
        $this->time = $time;
258✔
172

173
        return $this;
258✔
174
    }
175

176
    public function toArray(bool $parseJson = false): array
177
    {
178
        $request      = $this->getRequest();
6✔
179
        $requestArray = $request?->toArray();
6✔
180

181
        $bodyRaw  = $this->getBody();
6✔
182
        $bodyJson = $this->getJSON();
6✔
183
        if ($parseJson && $bodyJson !== null) {
6✔
184
            $bodyRaw = $bodyJson;
×
185
        }
186

187
        return [
6✔
188
            'request'  => $requestArray,
6✔
189
            'response' => [
6✔
190
                'code'    => $this->getCode(),
6✔
191
                'body'    => $bodyRaw,
6✔
192
                'headers' => $this->getHeaders(),
6✔
193
                'time'    => $this->getTime(),
6✔
194
            ],
6✔
195
        ];
6✔
196
    }
197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc