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

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