1
+ <?php
2
+ namespace encryptorcode \httpclient ;
3
+
4
+ class HttpRequest{
5
+ private $ method ;
6
+ private $ url ;
7
+ private $ requestBodyType ;
8
+ private $ params ;
9
+ private $ headers ;
10
+ private $ body ;
11
+
12
+ public static function get ($ url ) : HttpRequest {
13
+ return new HttpRequest ("GET " ,$ url );
14
+ }
15
+
16
+ public static function post ($ url ) : HttpRequest {
17
+ return new HttpRequest ("POST " ,$ url );
18
+ }
19
+
20
+ public static function put ($ url ) : HttpRequest {
21
+ return new HttpRequest ("PUT " ,$ url );
22
+ }
23
+
24
+ public static function patch ($ url ) : HttpRequest {
25
+ return new HttpRequest ("PATCH " ,$ url );
26
+ }
27
+
28
+ public static function delete ($ url ) : HttpRequest {
29
+ return new HttpRequest ("PATCH " ,$ url );
30
+ }
31
+
32
+ private function __construct ($ method , $ url ){
33
+ $ this ->method = $ method ;
34
+ $ this ->url = $ url ;
35
+ }
36
+
37
+ public function param ($ key , $ value ) : HttpRequest {
38
+ $ this ->params [$ key ] = $ value ;
39
+ return $ this ;
40
+ }
41
+
42
+ public function header ($ key , $ value ) : HttpRequest {
43
+ $ this ->headers [$ key ] = $ value ;
44
+ return $ this ;
45
+ }
46
+
47
+ public function formParam ($ key , $ value ) : HttpRequest {
48
+ if ($ this ->method == "GET " ){
49
+ throw new HttpException ("FORM_DATA not supported for method " .$ this ->method );
50
+ }
51
+
52
+ if (isset ($ this ->requestBodyType )){
53
+ if ($ this ->requestBodyType != "FORM_DATA " ){
54
+ throw new HttpException ("Request already has a body of type " .$ this ->requestBodyType );
55
+ }
56
+ } else {
57
+ $ this ->requestBodyType = "FORM_DATA " ;
58
+ $ this ->body = array ();
59
+ }
60
+
61
+ $ this ->body [$ key ] = $ value ;
62
+ return $ this ;
63
+ }
64
+
65
+ public function jsonData ($ data ) : HttpRequest {
66
+ if ($ this ->method == "GET " ){
67
+ throw new HttpException ("JSON_BODY not supported for method " .$ this ->method );
68
+ }
69
+
70
+ if (isset ($ this ->requestBodyType )){
71
+ if ($ this ->requestBodyType != "JSON_BODY " ){
72
+ throw new HttpException ("Request already has a body of type " .$ this ->requestBodyType );
73
+ }
74
+ }
75
+
76
+ if (gettype ($ data ) !== "string " ){
77
+ $ this ->body = json_encode ($ data );
78
+ } else {
79
+ $ this ->body = $ data ;
80
+ }
81
+
82
+ $ this ->requestBodyType = "JSON_BODY " ;
83
+ $ this ->headers ["Content-Type " ] = "application/json " ;
84
+ return $ this ;
85
+ }
86
+
87
+ public function getResponse () : HttpResponse {
88
+ return HttpConnector::request ($ this );
89
+ }
90
+
91
+ public function getParams () : ?array {
92
+ return $ this ->params ;
93
+ }
94
+
95
+ public function getHeaders () : ?array {
96
+ return $ this ->headers ;
97
+ }
98
+
99
+ public function getMethod () : string {
100
+ return $ this ->method ;
101
+ }
102
+
103
+ public function getUrl () : string {
104
+ return $ this ->url ;
105
+ }
106
+
107
+ public function getRequestBodyType () : ?string {
108
+ return $ this ->requestBodyType ;
109
+ }
110
+
111
+ public function getBody (){
112
+ return $ this ->body ;
113
+ }
114
+
115
+ }
0 commit comments