Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Response
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
13
100.00% covered (success)
100.00%
1 / 1
 getParsedResponse
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 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.0.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
42        if (($this->hasBody()) && ($this->hasHeader('Content-Type')) && (count($this->getHeader('Content-Type')->getValues()) == 1)) {
43            $rawResponse     = $this->getBody()->getContent();
44            $contentType     = $this->getHeader('Content-Type')->getValue(0);
45            $contentEncoding = ($this->hasHeader('Content-Encoding') && (count($this->getHeader('Content-Encoding')->getValues()) == 1)) ?
46                $this->getHeader('Content-Encoding')->getValue(0) : null;
47            $parsedResponse  = Parser::parseDataByContentType($rawResponse, $contentType, $contentEncoding);
48        }
49
50        return $parsedResponse;
51    }
52
53    /**
54     * Attempt to create a collection object from the response.
55     * Attempts a JSON decode on any content string that returns unparsed.
56     *
57     * @param  bool $forceJson
58     * @return Collection
59     */
60    public function collect(bool $forceJson = true): Collection
61    {
62        $data = ($forceJson) ? $this->json() : $this->getParsedResponse();
63
64        // Fall back to empty array on fail
65        if (!is_array($data)) {
66            $data = [];
67        }
68
69        return new Collection($data);
70    }
71
72    /**
73     * Attempts to JSON-decode any content string that returns unparsed.
74     *
75     * @return array
76     */
77    public function json(): array
78    {
79        $content = $this->getParsedResponse();
80
81        if (is_string($content)) {
82            $json = @json_decode($content, true);
83            if (json_last_error() === JSON_ERROR_NONE) {
84                $content = $json;
85            }
86        }
87
88        return (is_array($content)) ? $content : [];
89    }
90
91}