-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
172 lines (148 loc) · 3.59 KB
/
Copy pathRequest.php
File metadata and controls
172 lines (148 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Request class file
*
* @package Mantle
*/
namespace Mantle\Http_Client;
use Mantle\Support\Str;
use function Mantle\Support\Helpers\data_get;
/**
* Request Record
*
* Used to store a request that was made via the WordPress HTTP API that can be
* asserted against.
*/
readonly class Request {
/**
* Arguments of the request.
*
* @var array<string, mixed> $args Arguments of the request.
*/
public array $args;
/**
* Constructor
*
* @param array<string, mixed> $args Arguments of the request.
* @param string $url URL of the request.
*/
public function __construct( array $args, public string $url ) {
// Format the headers to be lowercase.
$args['headers'] = array_change_key_case( $args['headers'] ?? [] );
$this->args = $args;
}
/**
* Retrieve the URL of the request.
*/
public function url(): string {
return $this->url;
}
/**
* Retrieve the method of the request. The method is always uppercase.
*
* @todo Combine method() and enum_method() into one method that returns the
* enum directly for Mantle 2.0.
*/
public function method(): string {
return strtoupper( $this->args['method'] ?? '' );
}
/**
* Retrieve the enum method of the request.
*/
public function enum_method(): Http_Method {
return Http_Method::from( $this->method() );
}
/**
* Check if the request has a set of headers.
*
* @param array<string, string> $headers Headers to check for.
*/
public function has_headers( array $headers ): bool {
foreach ( $headers as $key => $value ) {
if ( ! $this->has_header( $key, $value ) ) {
return false;
}
}
return true;
}
/**
* Determine if the request has a given header.
*
* @param string $header Header to compare.
* @param mixed $value Header value to compare, optional.
*/
public function has_header( string $header, $value = null ): bool {
$header = strtolower( $header );
if ( is_null( $value ) ) {
return isset( $this->args['headers'][ $header ] );
}
return isset( $this->args['headers'][ $header ] ) && $value === $this->args['headers'][ $header ];
}
/**
* Retrieve the value of a header.
*
* @param string $header Header to retrieve.
* @return mixed
*/
public function header( string $header ) {
$header = strtolower( $header );
return $this->args['headers'][ $header ] ?? null;
}
/**
* Retrieve all headers of the request.
*
* @return array<string, string>
*/
public function headers(): array {
return $this->args['headers'];
}
/**
* Retrieve the body of the request.
*/
public function body(): mixed {
return $this->args['body'] ?? null;
}
/**
* Retrieve the JSON decoded body of the request.
*
* @return array<mixed>|null
*/
public function json(): mixed {
return json_decode( (string) $this->body(), true );
}
/**
* Determine if the request is simple form data.
*/
public function is_form(): bool {
return $this->has_header( 'Content-Type', 'application/x-www-form-urlencoded' );
}
/**
* Determine if the request is JSON.
*/
public function is_json(): bool {
return $this->has_header( 'Content-Type' )
&& Str::contains( $this->header( 'Content-Type' ), 'json' );
}
/**
* Retrieve a specific value from the request.
*
* @param string $key Key to retrieve.
*/
public function get( string $key ): mixed {
return data_get( $this->args, $key );
}
/**
* Dump the request to the screen.
*/
public function dump(): static {
dump( $this->args, $this->url );
return $this;
}
/**
* Dump the request to the screen and die.
*/
public function dd(): never {
$this->dump();
exit( 1 );
}
}