Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
19 / 19
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractHttp
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
19 / 19
38
100.00% covered (success)
100.00%
1 / 1
 addHeader
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 addHeaders
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getHeaderValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHeadersAsArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getHeadersAsString
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 hasHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeHeader
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 removeHeaders
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setBody
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBodyContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 hasBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasBodyContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 decodeBodyContent
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 removeBody
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __get
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-2023 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;
15
16use Pop\Mime\Part\Header;
17use Pop\Mime\Part\Body;
18
19/**
20 * Abstract HTTP class
21 *
22 * @category   Pop
23 * @package    Pop\Http
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    4.1.0
28 */
29abstract class AbstractHttp implements HttpInterface
30{
31
32    /**
33     * Headers
34     * @var array
35     */
36    protected $headers = [];
37
38    /**
39     * Body
40     * @var Body
41     */
42    protected $body = null;
43
44    /**
45     * Add a header
46     *
47     * @param  Header|string $header
48     * @param  string $value
49     * @return AbstractHttp
50     */
51    public function addHeader($header, $value = null)
52    {
53        if ($header instanceof Header) {
54            $this->headers[$header->getName()] = $header;
55        } else {
56            $this->headers[$header] = new Header($header, $value);
57        }
58
59        return $this;
60    }
61
62    /**
63     * Add all headers
64     *
65     * @param  array $headers
66     * @return AbstractHttp
67     */
68    public function addHeaders(array $headers)
69    {
70        foreach ($headers as $header => $value) {
71            if ($value instanceof Header) {
72                $this->addHeader($value);
73            } else {
74                $this->addHeader($header, $value);
75            }
76        }
77        return $this;
78    }
79
80    /**
81     * Get a header
82     *
83     * @param  string $name
84     * @return mixed
85     */
86    public function getHeader($name)
87    {
88        return (isset($this->headers[$name])) ? $this->headers[$name] : null;
89    }
90
91    /**
92     * Get header value
93     *
94     * @param  string $name
95     * @return mixed
96     */
97    public function getHeaderValue($name)
98    {
99        return (isset($this->headers[$name])) ? $this->headers[$name]->getValue() : null;
100    }
101
102    /**
103     * Get all headers
104     *
105     * @return array
106     */
107    public function getHeaders()
108    {
109        return $this->headers;
110    }
111
112    /**
113     * Get all header values as associative array
114     *
115     * @return array
116     */
117    public function getHeadersAsArray()
118    {
119        $headers = [];
120
121        foreach ($this->headers as $name => $header) {
122            $headers[$name] = $header->getValue();
123        }
124        return $headers;
125    }
126
127    /**
128     * Get all header values formatted string
129     *
130     * @param  string $status
131     * @param  string $eol
132     * @return string
133     */
134    public function getHeadersAsString($status = null, $eol = "\r\n")
135    {
136        $headers = '';
137
138        if (null !== $status) {
139            $headers = $status . $eol;
140        }
141
142        foreach ($this->headers as $header) {
143            $headers .= $header . $eol;
144        }
145
146        return $headers;
147    }
148
149    /**
150     * Determine if there are headers
151     *
152     * @return boolean
153     */
154    public function hasHeaders()
155    {
156        return (count($this->headers) > 0);
157    }
158
159    /**
160     * Has a header
161     *
162     * @param  string $name
163     * @return boolean
164     */
165    public function hasHeader($name)
166    {
167        return (isset($this->headers[$name]));
168    }
169
170    /**
171     * Remove a header
172     *
173     * @param  string $name
174     * @return AbstractHttp
175     */
176    public function removeHeader($name)
177    {
178        if (isset($this->headers[$name])) {
179            unset($this->headers[$name]);
180        }
181
182        return $this;
183    }
184
185    /**
186     * Remove all headers
187     *
188     * @return AbstractHttp
189     */
190    public function removeHeaders()
191    {
192        $this->headers = [];
193        return $this;
194    }
195
196    /**
197     * Set the body
198     *
199     * @param  string|Body $body
200     * @return AbstractHttp
201     */
202    public function setBody($body)
203    {
204        $this->body = ($body instanceof Body) ? $body : new Body($body);
205        return $this;
206    }
207
208    /**
209     * Get the body
210     *
211     * @return Body
212     */
213    public function getBody()
214    {
215        return $this->body;
216    }
217
218    /**
219     * Get body content
220     *
221     * @return mixed
222     */
223    public function getBodyContent()
224    {
225        return (null !== $this->body) ? $this->body->getContent() : null;
226    }
227
228    /**
229     * Has a body
230     *
231     * @return boolean
232     */
233    public function hasBody()
234    {
235        return (null !== $this->body);
236    }
237
238    /**
239     * Has body content
240     *
241     * @return boolean
242     */
243    public function hasBodyContent()
244    {
245        return ((null !== $this->body) && $this->body->hasContent());
246    }
247
248    /**
249     * Decode the body
250     *
251     * @param  string $body
252     * @return Body
253     */
254    public function decodeBodyContent($body = null)
255    {
256        if (null !== $body) {
257            $this->setBody($body);
258        }
259        if (($this->hasHeader('Transfer-Encoding')) &&
260            (strtolower($this->getHeader('Transfer-Encoding')->getValue()) == 'chunked')) {
261            $this->body->setContent(Parser::decodeChunkedData($this->body->getContent()));
262        }
263        $contentEncoding = ($this->hasHeader('Content-Encoding')) ? $this->getHeader('Content-Encoding')->getValue() : null;
264        $this->body->setContent(Parser::decodeData($this->body->getContent(), $contentEncoding));
265
266        return $this->body;
267    }
268
269    /**
270     * Remove the body
271     *
272     * @return AbstractHttp
273     */
274    public function removeBody()
275    {
276        $this->body = null;
277        return $this;
278    }
279
280    /**
281     * Magic method to get either the headers or body
282     *
283     * @param  string $name
284     * @return mixed
285     */
286    public function __get($name)
287    {
288        switch ($name) {
289            case 'headers':
290                return $this->headers;
291                break;
292            case 'body':
293                return $this->body;
294                break;
295            default:
296                return null;
297        }
298    }
299
300}