Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Response
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
8 / 8
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExitCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResponseCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setResponseCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setExitCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Miniframe\Core;
4
5class Response extends \Exception
6{
7    /**
8     * Text to return
9     *
10     * @var string
11     */
12    private $text;
13
14    /**
15     * Exit code to return
16     *
17     * @var int
18     */
19    private $exitCode = 0;
20
21    /**
22     * HTTP Response code
23     *
24     * @var int
25     */
26    private $responseCode = 200;
27
28    /**
29     * List of HTTP headers
30     *
31     * @var array{header: string, replace: bool}[]
32     */
33    private $headers = array();
34
35    /**
36     * Initializes a generic response
37     *
38     * @param string  $text     Response text.
39     * @param integer $exitCode Exit code (used for shell error codes).
40     */
41    public function __construct(string $text = '', int $exitCode = 0)
42    {
43        $this->text = $text;
44        $this->exitCode = $exitCode;
45    }
46
47    /**
48     * Returns the response text
49     *
50     * @return string
51     */
52    public function render(): string
53    {
54        return $this->text;
55    }
56
57    /**
58     * Returns the current exit code (used for shell error codes)
59     *
60     * @return integer
61     */
62    public function getExitCode(): int
63    {
64        return $this->exitCode;
65    }
66
67    /**
68     * Returns the current response code (used for HTTP responses)
69     *
70     * @return integer
71     */
72    public function getResponseCode(): int
73    {
74        return $this->responseCode;
75    }
76
77    /**
78     * Sets a new response code (used for HTTP responses)
79     *
80     * @param integer $responseCode The new response code.
81     *
82     * @return void
83     */
84    public function setResponseCode(int $responseCode): void
85    {
86        $this->responseCode = $responseCode;
87    }
88
89    /**
90     * Sets a new exit code (used for shell error codes)
91     *
92     * @param integer $exitCode Error code.
93     *
94     * @return void
95     */
96    public function setExitCode(int $exitCode): void
97    {
98        $this->exitCode = $exitCode;
99    }
100
101    /**
102     * Adds a response header
103     *
104     * @param string  $header  The header string.
105     * @param boolean $replace When set to false, a header can be sent back multiple times.
106     *
107     * @return void
108     */
109    public function addHeader(string $header, bool $replace = true): void
110    {
111        $this->headers[] = ['header' => $header, 'replace' => $replace];
112    }
113
114    /**
115     * Returns a list of all headers to set (each record has two keys: 'header' and 'replace')
116     *
117     * @return array{header: string, replace: bool}[]
118     */
119    public function getHeaders(): array
120    {
121        return $this->headers;
122    }
123}