Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Response
95.65% covered (success)
95.65%
22 / 23
66.67% covered (warning)
66.67%
2 / 3
15
0.00% covered (danger)
0.00%
0 / 1
 getParsedResponse
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
8.03
 collect
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 json
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Http\Client;
15
16use Pop\Http\AbstractResponse;
17use Pop\Http\Parser;
18use Pop\Utils\Collection;
19
20/**
21 * HTTP client response class
22 *
23 * @category   Pop
24 * @package    Pop\Http
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    5.2.0
29 */
30class Response extends AbstractResponse
31{
32
33    /**
34     * Get the parsed response
35     *
36     * @return mixed
37     */
38    public function getParsedResponse(): mixed
39    {
40        $parsedResponse     = null;
41        $contentTypeHeaders = ['Content-Type', 'Content-type', 'content-type'];
42
43        if ($this->hasBody()) {
44            $rawResponse     = $this->getBody()->getContent();
45            $contentEncoding = ($this->hasHeader('Content-Encoding') && (count($this->getHeader('Content-Encoding')->getValues()) == 1)) ?
46                $this->getHeader('Content-Encoding')->getValue(0) : null;
47
48            foreach ($contentTypeHeaders as $contentTypeHeader) {
49                if ($this->hasHeader($contentTypeHeader) && (count($this->getHeader($contentTypeHeader)->getValues()) == 1)) {
50                    $contentType    = $this->getHeader($contentTypeHeader)->getValue(0);
51                    $parsedResponse = Parser::parseDataByContentType($rawResponse, $contentType, $contentEncoding);
52                    if ($parsedResponse != $rawResponse) {
53                        break;
54                    }
55                }
56            }
57        }
58
59        return $parsedResponse;
60    }
61
62    /**
63     * Attempt to create a collection object from the response.
64     * Attempts a JSON decode on any content string that returns unparsed.
65     *
66     * @param  bool $forceJson
67     * @return Collection
68     */
69    public function collect(bool $forceJson = true): Collection
70    {
71        $data = ($forceJson) ? $this->json() : $this->getParsedResponse();
72
73        // Fall back to empty array on fail
74        if (!is_array($data)) {
75            $data = [];
76        }
77
78        return new Collection($data);
79    }
80
81    /**
82     * Attempts to JSON-decode any content string that returns unparsed.
83     *
84     * @return array
85     */
86    public function json(): array
87    {
88        $content = $this->getParsedResponse();
89
90        if (is_string($content)) {
91            $json = @json_decode($content, true);
92            if (json_last_error() === JSON_ERROR_NONE) {
93                $content = $json;
94            }
95        }
96
97        return (is_array($content)) ? $content : [];
98    }
99
100}