Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http\Client;
4
 
5
use ArrayAccess;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Traits\Macroable;
8
use LogicException;
9
 
10
class Request implements ArrayAccess
11
{
12
    use Macroable;
13
 
14
    /**
15
     * The underlying PSR request.
16
     *
17
     * @var \Psr\Http\Message\RequestInterface
18
     */
19
    protected $request;
20
 
21
    /**
22
     * The decoded payload for the request.
23
     *
24
     * @var array
25
     */
26
    protected $data;
27
 
28
    /**
29
     * Create a new request instance.
30
     *
31
     * @param  \Psr\Http\Message\RequestInterface  $request
32
     * @return void
33
     */
34
    public function __construct($request)
35
    {
36
        $this->request = $request;
37
    }
38
 
39
    /**
40
     * Get the request method.
41
     *
42
     * @return string
43
     */
44
    public function method()
45
    {
46
        return $this->request->getMethod();
47
    }
48
 
49
    /**
50
     * Get the URL of the request.
51
     *
52
     * @return string
53
     */
54
    public function url()
55
    {
56
        return (string) $this->request->getUri();
57
    }
58
 
59
    /**
60
     * Determine if the request has a given header.
61
     *
62
     * @param  string  $key
63
     * @param  mixed  $value
64
     * @return bool
65
     */
66
    public function hasHeader($key, $value = null)
67
    {
68
        if (is_null($value)) {
69
            return ! empty($this->request->getHeaders()[$key]);
70
        }
71
 
72
        $headers = $this->headers();
73
 
74
        if (! Arr::has($headers, $key)) {
75
            return false;
76
        }
77
 
78
        $value = is_array($value) ? $value : [$value];
79
 
80
        return empty(array_diff($value, $headers[$key]));
81
    }
82
 
83
    /**
84
     * Determine if the request has the given headers.
85
     *
86
     * @param  array|string  $headers
87
     * @return bool
88
     */
89
    public function hasHeaders($headers)
90
    {
91
        if (is_string($headers)) {
92
            $headers = [$headers => null];
93
        }
94
 
95
        foreach ($headers as $key => $value) {
96
            if (! $this->hasHeader($key, $value)) {
97
                return false;
98
            }
99
        }
100
 
101
        return true;
102
    }
103
 
104
    /**
105
     * Get the values for the header with the given name.
106
     *
107
     * @param  string  $key
108
     * @return array
109
     */
110
    public function header($key)
111
    {
112
        return Arr::get($this->headers(), $key, []);
113
    }
114
 
115
    /**
116
     * Get the request headers.
117
     *
118
     * @return array
119
     */
120
    public function headers()
121
    {
122
        return $this->request->getHeaders();
123
    }
124
 
125
    /**
126
     * Get the body of the request.
127
     *
128
     * @return string
129
     */
130
    public function body()
131
    {
132
        return (string) $this->request->getBody();
133
    }
134
 
135
    /**
136
     * Determine if the request contains the given file.
137
     *
138
     * @param  string  $name
139
     * @param  string|null  $value
140
     * @param  string|null  $filename
141
     * @return bool
142
     */
143
    public function hasFile($name, $value = null, $filename = null)
144
    {
145
        if (! $this->isMultipart()) {
146
            return false;
147
        }
148
 
149
        return collect($this->data)->reject(function ($file) use ($name, $value, $filename) {
150
            return $file['name'] != $name ||
151
                ($value && $file['contents'] != $value) ||
152
                ($filename && $file['filename'] != $filename);
153
        })->count() > 0;
154
    }
155
 
156
    /**
157
     * Get the request's data (form parameters or JSON).
158
     *
159
     * @return array
160
     */
161
    public function data()
162
    {
163
        if ($this->isForm()) {
164
            return $this->parameters();
165
        } elseif ($this->isJson()) {
166
            return $this->json();
167
        }
168
 
169
        return $this->data ?? [];
170
    }
171
 
172
    /**
173
     * Get the request's form parameters.
174
     *
175
     * @return array
176
     */
177
    protected function parameters()
178
    {
179
        if (! $this->data) {
180
            parse_str($this->body(), $parameters);
181
 
182
            $this->data = $parameters;
183
        }
184
 
185
        return $this->data;
186
    }
187
 
188
    /**
189
     * Get the JSON decoded body of the request.
190
     *
191
     * @return array
192
     */
193
    protected function json()
194
    {
195
        if (! $this->data) {
196
            $this->data = json_decode($this->body(), true);
197
        }
198
 
199
        return $this->data;
200
    }
201
 
202
    /**
203
     * Determine if the request is simple form data.
204
     *
205
     * @return bool
206
     */
207
    public function isForm()
208
    {
209
        return $this->hasHeader('Content-Type', 'application/x-www-form-urlencoded');
210
    }
211
 
212
    /**
213
     * Determine if the request is JSON.
214
     *
215
     * @return bool
216
     */
217
    public function isJson()
218
    {
219
        return $this->hasHeader('Content-Type') &&
220
               str_contains($this->header('Content-Type')[0], 'json');
221
    }
222
 
223
    /**
224
     * Determine if the request is multipart.
225
     *
226
     * @return bool
227
     */
228
    public function isMultipart()
229
    {
230
        return $this->hasHeader('Content-Type') &&
231
               str_contains($this->header('Content-Type')[0], 'multipart');
232
    }
233
 
234
    /**
235
     * Set the decoded data on the request.
236
     *
237
     * @param  array  $data
238
     * @return $this
239
     */
240
    public function withData(array $data)
241
    {
242
        $this->data = $data;
243
 
244
        return $this;
245
    }
246
 
247
    /**
248
     * Get the underlying PSR compliant request instance.
249
     *
250
     * @return \Psr\Http\Message\RequestInterface
251
     */
252
    public function toPsrRequest()
253
    {
254
        return $this->request;
255
    }
256
 
257
    /**
258
     * Determine if the given offset exists.
259
     *
260
     * @param  string  $offset
261
     * @return bool
262
     */
263
    public function offsetExists($offset): bool
264
    {
265
        return isset($this->data()[$offset]);
266
    }
267
 
268
    /**
269
     * Get the value for a given offset.
270
     *
271
     * @param  string  $offset
272
     * @return mixed
273
     */
274
    public function offsetGet($offset): mixed
275
    {
276
        return $this->data()[$offset];
277
    }
278
 
279
    /**
280
     * Set the value at the given offset.
281
     *
282
     * @param  string  $offset
283
     * @param  mixed  $value
284
     * @return void
285
     *
286
     * @throws \LogicException
287
     */
288
    public function offsetSet($offset, $value): void
289
    {
290
        throw new LogicException('Request data may not be mutated using array access.');
291
    }
292
 
293
    /**
294
     * Unset the value at the given offset.
295
     *
296
     * @param  string  $offset
297
     * @return void
298
     *
299
     * @throws \LogicException
300
     */
301
    public function offsetUnset($offset): void
302
    {
303
        throw new LogicException('Request data may not be mutated using array access.');
304
    }
305
}