Completed
Push — master ( da510d...a5cc05 )
by Alexander
01:57
created

Message::code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l;
4
5
/**
6
 * Class Message
7
 *
8
 * Container for requests and responses made with the Remote class
9
 *
10
 * @package alkemann\h2l
11
 */
12
final class Message
13
{
14
    const CONTENT_JSON = 'application/json';
15
    const CONTENT_FORM = 'application/x-www-form-urlencoded';
16
    const CONTENT_HTML = 'text/html';
17
    const CONTENT_TEXT = 'text/plain';
18
    const CONTENT_XML = 'text/xml';
19
20
    const REQUEST = "REQUEST";
21
    const RESPONSE = "RESPONSE";
22
    /**
23
     * @var string  "REQUEST"|"RESPONSE"
24
     */
25
    private $type;
26
27
    /**
28
     * @var int
29
     */
30
    private $code;
31
    /**
32
     * @var string
33
     */
34
    private $url = '';
35
    /**
36
     * Enum with Request::GET, Request::POST etc
37
     * @var string
38
     */
39
    private $method = Request::GET;
40
    /**
41
     * @var string
42
     */
43
    private $body;
44
    /**
45
     * @var array
46
     */
47
    private $meta = [];
48
    /**
49
     * @var array
50
     */
51
    private $headers = [];
52
    /**
53
     * @var array
54
     */
55
    private $options = [];
56
    /**
57
     * @var string
58
     */
59
    private $content_type = 'text/html';
60
    /**
61
     * @var string
62
     */
63
    private $content_charset = 'utf-8';
64
65
    public function type(): ?string
66
    {
67
        return $this->type;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function url(): string
74
    {
75
        return $this->url;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function method(): string
82
    {
83
        return $this->method;
84
    }
85
86
    /**
87
     * @return null|string
88
     */
89
    public function body(): ?string
90
    {
91
        return $this->body;
92
    }
93
94
    /**
95
     * @return null|string|array|\SimpleXMLElement body converted from raw format
96
     */
97
    public function content()
98
    {
99
        switch ($this->contentType()) {
100
            case static::CONTENT_JSON:
101
                return json_decode($this->body, true);
102
            case static::CONTENT_XML:
103
                return new \SimpleXMLElement($this->body);
104
            case null:
105
            default:
106
                return $this->body;
107
        }
108
    }
109
110
    public function contentType(): string
111
    {
112
        return $this->content_type;
113
    }
114
115
    public function charset(): string
116
    {
117
        return $this->content_charset;
118
    }
119
120
    /**
121
     * @param string $name
122
     * @return null|string
123
     */
124
    public function header(string $name): ?string
125
    {
126
        foreach ($this->headers as $key => $value) {
127
            if (strcasecmp($key, $name) === 0) {
128
                return $value;
129
            }
130
        }
131
        return null;
132
    }
133
134
    /**
135
     * @param string $class name of class that must take data array as constructor
136
     * @return object body json decoded and sent to constructor of $class
137
     */
138
    public function as(string $class)
139
    {
140
        return new $class($this->content());
141
    }
142
143
    /**
144
     * @return array
145
     */
146
    public function headers(): array
147
    {
148
        return $this->headers;
149
    }
150
151
    /**
152
     * @return array
153
     */
154
    public function meta(): array
155
    {
156
        return $this->meta;
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    public function options(): array
163
    {
164
        return $this->options;
165
    }
166
167
    /**
168
     * @return int|null
169
     */
170
    public function code(): ?int
171
    {
172
        return $this->code;
173
    }
174
175
    /**
176
     * @param string $type
177
     * @return Message
178
     */
179
    public function withType(string $type): Message
180
    {
181
        $new = clone $this;
182
        $new->type = $type;
183
        return $new;
184
    }
185
186
    /**
187
     * @param int $code
188
     * @return Message
189
     */
190
    public function withCode(int $code): Message
191
    {
192
        $new = clone $this;
193
        $new->code = $code;
194
        return $new;
195
    }
196
197
    /**
198
     * @param string $url
199
     * @return Message
200
     */
201
    public function withUrl(string $url): Message
202
    {
203
        $new = clone $this;
204
        $new->url = $url;
205
        return $new;
206
    }
207
208
    /**
209
     * @param string $method
210
     * @return Message
211
     */
212
    public function withMethod(string $method): Message
213
    {
214
        $new = clone $this;
215
        $new->method = $method;
216
        return $new;
217
    }
218
219
    /**
220
     * @param string $body
221
     * @return Message
222
     */
223
    public function withBody(string $body): Message
224
    {
225
        $new = clone $this;
226
        $new->body = $body;
227
        return $new;
228
    }
229
230
    /**
231
     * @param array $headers
232
     * @return Message
233
     */
234
    public function withHeaders(array $headers): Message
235
    {
236
        $new = clone $this;
237
        $new->headers = $headers;
238
        $content_header = $new->header('Content-Type');
239
        if (is_string($content_header)) {
240
            if (strpos($content_header, ';') === false) {
241
                $new->content_type = trim(strtolower($content_header));
242
            } else {
243
                list($type, $other) = explode(';', $content_header, 2);
244
                $new->content_type = trim(strtolower($type));
245
                list($key, $charset) = explode('=', $other, 2);
246
                if ('charset' === strtolower(trim($key))) {
247
                    $new->content_charset = strtolower(trim(trim($charset, '"')));
248
                }
249
            }
250
        }
251
        return $new;
252
    }
253
254
    /**
255
     * @param array $options
256
     * @return Message
257
     */
258
    public function withOptions(array $options): Message
259
    {
260
        $new = clone $this;
261
        $new->options = $options;
262
        return $new;
263
    }
264
265
    /**
266
     * @param array $meta
267
     * @return Message
268
     */
269
    public function withMeta(array $meta): Message
270
    {
271
        $new = clone $this;
272
        $new->meta = $meta;
273
        return $new;
274
    }
275
}
276