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

JBZoo / Http-Client / 7365262943

14 Nov 2023 06:38PM UTC coverage: 95.469%. Remained the same
7365262943

push

github

web-flow
Fixed failed test - New mock (#24)

295 of 309 relevant lines covered (95.47%)

62.78 hits per line

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

90.48
/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') {
172✔
43
            return $this->getCode();
124✔
44
        }
45

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

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

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

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

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

65
        return $this;
180✔
66
    }
67

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

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

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

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

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

87
        return $this;
180✔
88
    }
89

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

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

100
        return $this;
184✔
101
    }
102

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

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

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

121
    public function getXml(): JSON
122
    {
123
        try {
124
            $xmlAsArray = Xml::dom2Array(Xml::createFromString($this->internalBody));
8✔
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);
8✔
133
    }
134

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

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

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

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

156
        return $this;
180✔
157
    }
158

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

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

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

173
        return $this;
172✔
174
    }
175

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

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

187
        return [
2✔
188
            'request'  => $requestArray,
2✔
189
            'response' => [
2✔
190
                'code'    => $this->getCode(),
4✔
191
                'body'    => $bodyRaw,
2✔
192
                'headers' => $this->getHeaders(),
4✔
193
                'time'    => $this->getTime(),
4✔
194
            ],
2✔
195
        ];
2✔
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

© 2026 Coveralls, Inc