1616 * @method string scheme()
1717 * @method string remoteAddress()
1818 */
19+ require_once __DIR__ . '/RequestServerAccessor.php ' ;
20+
1921class Request
2022{
2123 /**
22- * @var array<string, mixed>
24+ * Server-derived request value accessor.
2325 */
24- private array $ server ;
26+ private RequestServerAccessor $ serverAccessor ;
27+
28+ private const SERVER_ACCESSOR_METHODS = [
29+ 'hasServer ' => true ,
30+ 'serverString ' => true ,
31+ 'uri ' => true ,
32+ 'hostHeader ' => true ,
33+ 'scheme ' => true ,
34+ 'remoteAddress ' => true ,
35+ ];
2536
2637 /**
2738 * @var array<string, mixed>
@@ -45,7 +56,7 @@ class Request
4556 public function __construct (?array $ server = null , ?array $ query = null , ?array $ post = null , ?string $ body = null )
4657 {
4758 // codacy:ignore-start - Request superglobal access is intentionally centralized in this wrapper
48- $ this ->server = $ server ?? $ _SERVER ;
59+ $ this ->serverAccessor = new RequestServerAccessor ( $ server ?? $ _SERVER ) ;
4960 $ this ->query = $ query ?? $ _GET ;
5061 $ this ->post = $ post ?? $ _POST ;
5162 // codacy:ignore-end
@@ -54,48 +65,16 @@ public function __construct(?array $server = null, ?array $query = null, ?array
5465
5566 public function __call (string $ name , array $ arguments ): mixed
5667 {
57- switch ($ name ) {
58- case 'hasServer ' :
59- return is_string ($ arguments [0 ] ?? null ) && array_key_exists ($ arguments [0 ], $ this ->server );
60-
61- case 'serverString ' :
62- return $ this ->serverString (
63- is_string ($ arguments [0 ] ?? null ) ? $ arguments [0 ] : '' ,
64- is_string ($ arguments [1 ] ?? null ) ? $ arguments [1 ] : ''
65- );
66-
67- case 'uri ' :
68- return $ this ->serverString ('REQUEST_URI ' );
69-
70- case 'hostHeader ' :
71- return strtolower (trim ($ this ->serverString ('HTTP_HOST ' )));
72-
73- case 'scheme ' :
74- $ https = strtolower ($ this ->serverString ('HTTPS ' ));
75- return $ https !== '' && $ https !== 'off ' && $ https !== '0 ' ? 'https ' : 'http ' ;
76-
77- case 'remoteAddress ' :
78- $ ipAddress = $ this ->serverString ('REMOTE_ADDR ' , 'unknown ' );
79- if ($ ipAddress === 'unknown ' ) {
80- return 'unknown ' ;
81- }
82- return filter_var ($ ipAddress , FILTER_VALIDATE_IP ) !== false ? $ ipAddress : 'invalid ' ;
83-
84- default :
85- throw new BadMethodCallException ('Unknown request method: ' . $ name );
68+ if (!isset (self ::SERVER_ACCESSOR_METHODS [$ name ])) {
69+ throw new BadMethodCallException ('Unknown request method: ' . $ name );
8670 }
87- }
88-
89- private function serverString (string $ key , string $ default = '' ): string
90- {
91- $ value = $ this ->server [$ key ] ?? $ default ;
9271
93- return is_scalar ( $ value ) ? ( string ) $ value : $ default ;
72+ return $ this -> serverAccessor ->{ $ name }(... $ arguments ) ;
9473 }
9574
9675 public function method (): string
9776 {
98- $ method = strtoupper ($ this ->serverString ('REQUEST_METHOD ' , 'GET ' ));
77+ $ method = strtoupper ($ this ->serverAccessor -> serverString ('REQUEST_METHOD ' , 'GET ' ));
9978
10079 return preg_match ('/^[A-Z]+$/ ' , $ method ) === 1 ? $ method : 'GET ' ;
10180 }
@@ -107,11 +86,11 @@ public function header(string $name): ?string
10786 ? $ normalized
10887 : 'HTTP_ ' . $ normalized ;
10988
110- if (!array_key_exists ( $ serverKey , $ this ->server )) {
89+ if (!$ this ->serverAccessor -> hasServer ( $ serverKey )) {
11190 return null ;
11291 }
11392
114- $ value = $ this ->serverString ($ serverKey );
93+ $ value = $ this ->serverAccessor -> serverString ($ serverKey );
11594
11695 return $ value === '' ? null : $ value ;
11796 }
0 commit comments