1
- <?php
1
+ <?php declare (strict_types= 1 );
2
2
3
3
/**
4
4
* PHP REST Client
5
5
* https://github.com/tcdent/php-restclient
6
- * (c) 2013-2017 Travis Dent <tcdent@gmail.com>
6
+ * (c) 2013-2022 Travis Dent <tcdent@gmail.com>
7
7
*/
8
8
9
9
class RestClientException extends Exception {}
@@ -23,13 +23,13 @@ class RestClient implements Iterator, ArrayAccess {
23
23
// Populated as-needed.
24
24
public $ decoded_response ; // Decoded response body.
25
25
26
- public function __construct ($ options =[]){
26
+ public function __construct (array $ options =[]){
27
27
$ default_options = [
28
28
'headers ' => [],
29
29
'parameters ' => [],
30
30
'curl_options ' => [],
31
31
'build_indexed_queries ' => FALSE ,
32
- 'user_agent ' => "PHP RestClient/0.1.7 " ,
32
+ 'user_agent ' => "PHP RestClient/0.1.8 " ,
33
33
'base_url ' => NULL ,
34
34
'format ' => NULL ,
35
35
'format_regex ' => "/(\w+)\/(\w+)(;[.+])?/ " ,
@@ -47,47 +47,47 @@ public function __construct($options=[]){
47
47
$ default_options ['decoders ' ], $ options ['decoders ' ]);
48
48
}
49
49
50
- public function set_option ($ key , $ value ){
50
+ public function set_option ($ key , $ value ) : void {
51
51
$ this ->options [$ key ] = $ value ;
52
52
}
53
53
54
- public function register_decoder ($ format , $ method ){
54
+ public function register_decoder (string $ format , callable $ method ) : void {
55
55
// Decoder callbacks must adhere to the following pattern:
56
56
// array my_decoder(string $data)
57
57
$ this ->options ['decoders ' ][$ format ] = $ method ;
58
58
}
59
59
60
60
// Iterable methods:
61
- public function rewind (){
61
+ public function rewind () : void {
62
62
$ this ->decode_response ();
63
- return reset ($ this ->decoded_response );
63
+ reset ($ this ->decoded_response );
64
64
}
65
65
66
- public function current (){
66
+ public function current () : mixed {
67
67
return current ($ this ->decoded_response );
68
68
}
69
69
70
- public function key (){
70
+ public function key () : mixed {
71
71
return key ($ this ->decoded_response );
72
72
}
73
73
74
- public function next (){
75
- return next ($ this ->decoded_response );
74
+ public function next () : void {
75
+ next ($ this ->decoded_response );
76
76
}
77
77
78
- public function valid (){
78
+ public function valid () : bool {
79
79
return is_array ($ this ->decoded_response )
80
80
&& (key ($ this ->decoded_response ) !== NULL );
81
81
}
82
82
83
83
// ArrayAccess methods:
84
- public function offsetExists ($ key ){
84
+ public function offsetExists ($ key ) : bool {
85
85
$ this ->decode_response ();
86
86
return is_array ($ this ->decoded_response )?
87
87
isset ($ this ->decoded_response [$ key ]) : isset ($ this ->decoded_response ->{$ key });
88
88
}
89
89
90
- public function offsetGet ($ key ){
90
+ public function offsetGet ($ key ) : mixed {
91
91
$ this ->decode_response ();
92
92
if (!$ this ->offsetExists ($ key ))
93
93
return NULL ;
@@ -96,40 +96,40 @@ public function offsetGet($key){
96
96
$ this ->decoded_response [$ key ] : $ this ->decoded_response ->{$ key };
97
97
}
98
98
99
- public function offsetSet ($ key , $ value ){
99
+ public function offsetSet ($ key , $ value ) : void {
100
100
throw new RestClientException ("Decoded response data is immutable. " );
101
101
}
102
102
103
- public function offsetUnset ($ key ){
103
+ public function offsetUnset ($ key ) : void {
104
104
throw new RestClientException ("Decoded response data is immutable. " );
105
105
}
106
106
107
107
// Request methods:
108
- public function get ($ url , $ parameters =[], $ headers =[]){
108
+ public function get (string $ url , array | string $ parameters =[], array $ headers =[]) : RestClient {
109
109
return $ this ->execute ($ url , 'GET ' , $ parameters , $ headers );
110
110
}
111
111
112
- public function post ($ url , $ parameters =[], $ headers =[]){
112
+ public function post (string $ url , array | string $ parameters =[], array $ headers =[]) : RestClient {
113
113
return $ this ->execute ($ url , 'POST ' , $ parameters , $ headers );
114
114
}
115
115
116
- public function put ($ url , $ parameters =[], $ headers =[]){
116
+ public function put (string $ url , array | string $ parameters =[], array $ headers =[]) : RestClient {
117
117
return $ this ->execute ($ url , 'PUT ' , $ parameters , $ headers );
118
118
}
119
119
120
- public function patch ($ url , $ parameters =[], $ headers =[]){
120
+ public function patch (string $ url , array | string $ parameters =[], array $ headers =[]) : RestClient {
121
121
return $ this ->execute ($ url , 'PATCH ' , $ parameters , $ headers );
122
122
}
123
123
124
- public function delete ($ url , $ parameters =[], $ headers =[]){
124
+ public function delete (string $ url , array | string $ parameters =[], array $ headers =[]) : RestClient {
125
125
return $ this ->execute ($ url , 'DELETE ' , $ parameters , $ headers );
126
126
}
127
127
128
- public function head ($ url , $ parameters =[], $ headers =[]){
128
+ public function head (string $ url , array | string $ parameters =[], array $ headers =[]) : RestClient {
129
129
return $ this ->execute ($ url , 'HEAD ' , $ parameters , $ headers );
130
130
}
131
131
132
- public function execute ($ url , $ method ='GET ' , $ parameters =[], $ headers =[]){
132
+ public function execute (string $ url , string $ method ='GET ' , array | string $ parameters =[], array $ headers =[]) : RestClient {
133
133
$ client = clone $ this ;
134
134
$ client ->url = $ url ;
135
135
$ client ->handle = curl_init ();
@@ -173,11 +173,11 @@ public function execute($url, $method='GET', $parameters=[], $headers=[]){
173
173
else
174
174
$ parameters_string = (string ) $ parameters ;
175
175
176
- if (strtoupper ($ method ) == 'POST ' ){
176
+ if (strtoupper ($ method ) === 'POST ' ){
177
177
$ curlopt [CURLOPT_POST ] = TRUE ;
178
178
$ curlopt [CURLOPT_POSTFIELDS ] = $ parameters_string ;
179
179
}
180
- elseif (strtoupper ($ method ) != 'GET ' ){
180
+ elseif (strtoupper ($ method ) !== 'GET ' ){
181
181
$ curlopt [CURLOPT_CUSTOMREQUEST ] = strtoupper ($ method );
182
182
$ curlopt [CURLOPT_POSTFIELDS ] = $ parameters_string ;
183
183
}
@@ -187,7 +187,7 @@ public function execute($url, $method='GET', $parameters=[], $headers=[]){
187
187
}
188
188
189
189
if ($ client ->options ['base_url ' ]){
190
- if ($ client ->url [0 ] != '/ ' && substr ($ client ->options ['base_url ' ], - 1 ) != '/ ' )
190
+ if ($ client ->url [0 ] !== '/ ' && ! str_ends_with ($ client ->options ['base_url ' ], '/ ' ) )
191
191
$ client ->url = '/ ' . $ client ->url ;
192
192
$ client ->url = $ client ->options ['base_url ' ] . $ client ->url ;
193
193
}
@@ -209,7 +209,7 @@ public function execute($url, $method='GET', $parameters=[], $headers=[]){
209
209
return $ client ;
210
210
}
211
211
212
- public function parse_response ($ response ){
212
+ public function parse_response ($ response ) : void {
213
213
$ headers = [];
214
214
$ this ->response_status_lines = [];
215
215
$ line = strtok ($ response , "\n" );
@@ -218,14 +218,14 @@ public function parse_response($response){
218
218
// Since we tokenize on \n, use the remaining \r to detect empty lines.
219
219
if (count ($ headers ) > 0 ) break ; // Must be the newline after headers, move on to response body
220
220
}
221
- elseif (strpos ($ line , 'HTTP ' ) === 0 ){
221
+ elseif (str_starts_with ($ line , 'HTTP ' )){
222
222
// One or more HTTP status lines
223
223
$ this ->response_status_lines [] = trim ($ line );
224
224
}
225
225
else {
226
226
// Has to be a header
227
- list ( $ key , $ value) = explode (': ' , $ line , 2 );
228
- $ key = trim ( strtolower (str_replace ('- ' , '_ ' , $ key )));
227
+ [ $ key , $ value] = explode (': ' , $ line , 2 );
228
+ $ key = strtolower ( trim (str_replace ('- ' , '_ ' , $ key )));
229
229
$ value = trim ($ value );
230
230
231
231
if (empty ($ headers [$ key ]))
@@ -241,7 +241,7 @@ public function parse_response($response){
241
241
$ this ->response = strtok ("" );
242
242
}
243
243
244
- public function get_response_format (){
244
+ public function get_response_format () : string {
245
245
if (!$ this ->response )
246
246
throw new RestClientException (
247
247
"A response must exist before it can be decoded. " );
@@ -259,7 +259,7 @@ public function get_response_format(){
259
259
"Response format could not be determined. " );
260
260
}
261
261
262
- public function decode_response (){
262
+ public function decode_response () : mixed {
263
263
if (empty ($ this ->decoded_response )){
264
264
$ format = $ this ->get_response_format ();
265
265
if (!array_key_exists ($ format , $ this ->options ['decoders ' ]))
0 commit comments