Standard::getAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Base
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\Base\View\Helper\Request;
12
13
use Psr\Http\Message\MessageInterface;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Message\StreamInterface;
17
use Psr\Http\Message\UriInterface;
18
19
20
/**
21
 * View helper class for accessing request data.
22
 *
23
 * @package Base
24
 * @subpackage View
25
 */
26
class Standard
27
	extends \Aimeos\Base\View\Helper\Base
28
	implements \Aimeos\Base\View\Helper\Request\Iface
29
{
30
	private ServerRequestInterface $request;
31
	private ?string $clientaddr;
32
	private ?string $target;
33
34
35
	/**
36
	 * Initializes the request view helper.
37
	 *
38
	 * @param \Aimeos\Base\View\Iface $view View instance with registered view helpers
39
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
40
	 * @param string|null $clientaddr Client IP address
41
	 * @param string|null $target Page ID or route name
42
	 */
43
	public function __construct( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request,
44
	?string $clientaddr = null, ?string $target = null )
45
	{
46
		parent::__construct( $view );
47
48
		$this->request = $request;
49
		$this->clientaddr = $clientaddr;
50
		$this->target = $target;
51
	}
52
53
54
	/**
55
	 * Returns the request view helper.
56
	 *
57
	 * @return \Aimeos\Base\View\Helper\Request\Iface Request view helper
58
	 */
59
	public function transform() : Iface
60
	{
61
		return $this;
62
	}
63
64
65
	/**
66
	 * Returns the client IP address.
67
	 *
68
	 * @return string|null Client IP address
69
	 */
70
	public function getClientAddress() : ?string
71
	{
72
		return $this->clientaddr;
73
	}
74
75
76
	/**
77
	 * Returns the current page or route name
78
	 *
79
	 * @return string|null Current page or route name
80
	 */
81
	public function getTarget() : ?string
82
	{
83
		return $this->target;
84
	}
85
86
87
	/**
88
	 * Retrieves the HTTP protocol version as a string.
89
	 *
90
	 * @return string HTTP protocol version.
91
	 */
92
	public function getProtocolVersion() : string
93
	{
94
		return $this->request->getProtocolVersion();
95
	}
96
97
98
	/**
99
	 * Return an instance with the specified HTTP protocol version.
100
	 *
101
	 * @param string $version HTTP protocol version
102
	 * @return self
103
	 */
104
	public function withProtocolVersion( $version ) : MessageInterface
105
	{
106
		$this->request = $this->request->withProtocolVersion( $version );
107
		return $this;
108
	}
109
110
111
	/**
112
	 * Retrieves all message header values.
113
	 *
114
	 * @return string[][] Returns an associative array of the message's headers.
115
	 *	 Each key MUST be a header name, and each value MUST be an array of
116
	 *	 strings for that header.
117
	 */
118
	public function getHeaders() : array
119
	{
120
		return $this->request->getHeaders();
121
	}
122
123
124
	/**
125
	 * Checks if a header exists by the given case-insensitive name.
126
	 *
127
	 * @param string $name Case-insensitive header field name.
128
	 * @return bool Returns true if any header names match the given header
129
	 *	 name using a case-insensitive string comparison. Returns false if
130
	 *	 no matching header name is found in the message.
131
	 */
132
	public function hasHeader( $name ) : bool
133
	{
134
		return $this->request->hasHeader( $name );
135
	}
136
137
138
	/**
139
	 * Retrieves a message header value by the given case-insensitive name.
140
	 *
141
	 * @param string $name Case-insensitive header field name.
142
	 * @return string[] An array of string values as provided for the given
143
	 *	header. If the header does not appear in the message, this method MUST
144
	 *	return an empty array.
145
	 */
146
	public function getHeader( $name ) : array
147
	{
148
		return $this->request->getHeader( $name );
149
	}
150
151
152
	/**
153
	 * Retrieves a comma-separated string of the values for a single header.
154
	 *
155
	 * @param string $name Case-insensitive header field name.
156
	 * @return string A string of values as provided for the given header
157
	 *	concatenated together using a comma. If the header does not appear in
158
	 *	the message, this method MUST return an empty string.
159
	 */
160
	public function getHeaderLine( $name ) : string
161
	{
162
		return $this->request->getHeaderLine( $name );
163
	}
164
165
166
	/**
167
	 * Return an instance with the provided value replacing the specified header.
168
	 *
169
	 * @param string $name Case-insensitive header field name.
170
	 * @param string|string[] $value Header value(s).
171
	 * @return self
172
	 * @throws \InvalidArgumentException for invalid header names or values.
173
	 */
174
	public function withHeader( $name, $value ) : MessageInterface
175
	{
176
		$this->request = $this->request->withHeader( $name, $value );
177
		return $this;
178
	}
179
180
181
	/**
182
	 * Return an instance with the specified header appended with the given value.
183
	 *
184
	 * @param string $name Case-insensitive header field name to add.
185
	 * @param string|string[] $value Header value(s).
186
	 * @return self
187
	 */
188
	public function withAddedHeader( $name, $value ) : MessageInterface
189
	{
190
		$this->request = $this->request->withAddedHeader( $name, $value );
191
		return $this;
192
	}
193
194
195
	/**
196
	 * Return an instance without the specified header.
197
	 *
198
	 * @param string $name Case-insensitive header field name to remove.
199
	 * @return self
200
	 */
201
	public function withoutHeader( $name ) : MessageInterface
202
	{
203
		$this->request = $this->request->withoutHeader( $name );
204
		return $this;
205
	}
206
207
208
	/**
209
	 * Gets the body of the message.
210
	 *
211
	 * @return StreamInterface Returns the body as a stream.
212
	 */
213
	public function getBody() : StreamInterface
214
	{
215
		return $this->request->getBody();
216
	}
217
218
219
	/**
220
	 * Return an instance with the specified message body.
221
	 *
222
	 * @param StreamInterface $body Body.
223
	 * @return self
224
	 * @throws \InvalidArgumentException When the body is not valid.
225
	 */
226
	public function withBody( StreamInterface $body ) : MessageInterface
227
	{
228
		$this->request = $this->request->withBody( $body );
229
		return $this;
230
	}
231
232
233
	/**
234
	 * Retrieves the message's request target.
235
	 *
236
	 * @return string
237
	 */
238
	public function getRequestTarget() : string
239
	{
240
		return $this->request->getRequestTarget();
241
	}
242
243
244
	/**
245
	 * Return an instance with the specific request-target.
246
	 *
247
	 * @see https://tools.ietf.org/html/rfc7230#section-2.7 (for the various
248
	 *	 request-target forms allowed in request messages)
249
	 * @param mixed $requestTarget
250
	 * @return self
251
	 */
252
	public function withRequestTarget( $requestTarget ) : RequestInterface
253
	{
254
		$this->request = $this->request->withRequestTarget( $requestTarget );
255
		return $this;
256
	}
257
258
259
	/**
260
	 * Retrieves the HTTP method of the request.
261
	 *
262
	 * @return string Returns the request method.
263
	 */
264
	public function getMethod() : string
265
	{
266
		return $this->request->getMethod();
267
	}
268
269
270
	/**
271
	 * Return an instance with the provided HTTP method.
272
	 *
273
	 * @param string $method Case-sensitive method.
274
	 * @return self
275
	 * @throws \InvalidArgumentException for invalid HTTP methods.
276
	 */
277
	public function withMethod( $method ) : RequestInterface
278
	{
279
		$this->request = $this->request->withMethod( $method );
280
		return $this;
281
	}
282
283
284
	/**
285
	 * Retrieves the URI instance.
286
	 *
287
	 * @see https://tools.ietf.org/html/rfc3986#section-4.3
288
	 * @return UriInterface Returns a UriInterface instance
289
	 *	 representing the URI of the request.
290
	 */
291
	public function getUri() : UriInterface
292
	{
293
		return $this->request->getUri();
294
	}
295
296
297
	/**
298
	 * Returns an instance with the provided URI.
299
	 *
300
	 * @param UriInterface $uri New request URI to use.
301
	 * @param bool $preserveHost Preserve the original state of the Host header.
302
	 * @return self
303
	 */
304
	public function withUri( UriInterface $uri, $preserveHost = false ) : RequestInterface
305
	{
306
		$this->request = $this->request->withUri( $uri, $preserveHost );
307
		return $this;
308
	}
309
310
311
	/**
312
	 * Retrieve server parameters.
313
	 *
314
	 * @return array List of key/value pairs from $_SERVER
315
	 */
316
	public function getServerParams() : array
317
	{
318
		return $this->request->getServerParams();
319
	}
320
321
322
	/**
323
	 * Retrieve cookies.
324
	 *
325
	 * @return array List of key/value pairs from $_SERVER
326
	 */
327
	public function getCookieParams() : array
328
	{
329
		return $this->request->getCookieParams();
330
	}
331
332
333
	/**
334
	 * Return an instance with the specified cookies.
335
	 *
336
	 * @param array $cookies Array of key/value pairs representing cookies.
337
	 * @return self
338
	 */
339
	public function withCookieParams( array $cookies ) : ServerRequestInterface
340
	{
341
		$this->request = $this->request->withCookieParams( $cookies );
342
		return $this;
343
	}
344
345
346
	/**
347
	 * Retrieve query string arguments.
348
	 *
349
	 * @return array
350
	 */
351
	public function getQueryParams() : array
352
	{
353
		return $this->request->getQueryParams();
354
	}
355
356
357
	/**
358
	 * Return an instance with the specified query string arguments.
359
	 *
360
	 * @param array $query Array of query string arguments, typically from $_GET.
361
	 * @return self
362
	 */
363
	public function withQueryParams( array $query ) : ServerRequestInterface
364
	{
365
		$this->request = $this->request->withQueryParams( $query );
366
		return $this;
367
	}
368
369
370
	/**
371
	 * Retrieve normalized file upload data.
372
	 *
373
	 * @return array An array tree of \Psr\Http\Message\UploadedFileInterface instances; an empty
374
	 *	 array MUST be returned if no data is present.
375
	 */
376
	public function getUploadedFiles() : array
377
	{
378
		return $this->request->getUploadedFiles();
379
	}
380
381
382
	/**
383
	 * Create a new instance with the specified uploaded files.
384
	 *
385
	 * @param array $uploadedFiles An array tree of \Psr\Http\Message\UploadedFileInterface instances
386
	 * @return self
387
	 */
388
	public function withUploadedFiles( array $uploadedFiles ) : ServerRequestInterface
389
	{
390
		$this->request = $this->request->withUploadedFiles( $uploadedFiles );
391
		return $this;
392
	}
393
394
395
	/**
396
	 * Retrieve any parameters provided in the request body.
397
	 *
398
	 * @return null|array|object The deserialized body parameters, if any.
399
	 *	 These will typically be an array or object.
400
	 */
401
	public function getParsedBody()
402
	{
403
		return $this->request->getParsedBody();
404
	}
405
406
407
	/**
408
	 * Return an instance with the specified body parameters.
409
	 *
410
	 * @param null|array|object $data The deserialized body data. This will
411
	 *	 typically be in an array or object.
412
	 * @return self
413
	 * @throws \InvalidArgumentException if an unsupported argument type is
414
	 *	 provided.
415
	 */
416
	public function withParsedBody( $data ) : ServerRequestInterface
417
	{
418
		$this->request = $this->request->withParsedBody( $data );
419
		return $this;
420
	}
421
422
423
	/**
424
	 * Retrieve attributes derived from the request.
425
	 *
426
	 * @return mixed[] Attributes derived from the request.
427
	 */
428
	public function getAttributes() : array
429
	{
430
		return $this->request->getAttributes();
431
	}
432
433
434
	/**
435
	 * Retrieve a single derived request attribute.
436
	 *
437
	 * @param string $name The attribute name.
438
	 * @param mixed $default Default value to return if the attribute does not exist.
439
	 * @return mixed
440
	 */
441
	public function getAttribute( $name, $default = null )
442
	{
443
		return $this->request->getAttribute( $name, $default );
444
	}
445
446
447
	/**
448
	 * Return an instance with the specified derived request attribute.
449
	 *
450
	 * @param string $name The attribute name.
451
	 * @param mixed $value The value of the attribute.
452
	 * @return self
453
	 */
454
	public function withAttribute( $name, $value ) : ServerRequestInterface
455
	{
456
		$this->request = $this->request->withAttribute( $name, $value );
457
		return $this;
458
	}
459
460
461
	/**
462
	 * Return an instance that removes the specified derived request attribute.
463
	 *
464
	 * @param string $name The attribute name.
465
	 * @return self
466
	 */
467
	public function withoutAttribute( $name ) : ServerRequestInterface
468
	{
469
		$this->request = $this->request->withoutAttribute( $name );
470
		return $this;
471
	}
472
}
473