Completed
Pull Request — master (#20)
by Alexander
01:34
created

Message::withType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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