shahradelahi /
easy-http
| 1 | <?php |
||
| 2 | |||
| 3 | namespace EasyHttp\Model; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * HttpOptions class |
||
| 7 | * |
||
| 8 | * @link https://github.com/shahradelahi/easy-http |
||
| 9 | * @author Shahrad Elahi (https://github.com/shahradelahi) |
||
| 10 | * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
||
| 11 | * |
||
| 12 | * @method array getHeaders() Get the header |
||
| 13 | * @method array getCookie() Get the cookie |
||
| 14 | * @method string|null getBody() Get the body |
||
| 15 | * @method int getTimeout() Get the timeout |
||
| 16 | * @method array getMultipart() Get the multipart |
||
| 17 | * @method ProxyServer getProxy() Get the proxy |
||
| 18 | * @method array getQuery() Get the query |
||
| 19 | * @method array getCurlOptions() Get the curl options |
||
| 20 | * |
||
| 21 | * @method $this setHeaders(array $headers) Set the header |
||
| 22 | * @method $this setCookie(array $cookie) Set the cookie |
||
| 23 | * @method $this setQuery(array $query) Set the query |
||
| 24 | * @method $this setTimeout(int $timeout) Set the timeout |
||
| 25 | * @method $this setMultipart(array $multipart) Set the multipart |
||
| 26 | */ |
||
| 27 | class HttpOptions |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * An array of HTTP header fields to set, in the format array('<em>Content-type: text/plain</em>', '<em>Content-length: 100</em>') |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private array $headers = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * An array of cookies to set, in the format array('name' => 'value', 'name2' => 'value2') |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private array $cookie = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * An array of query data (e.g., array('id' => '123', 'name' => 'John')) for use in the query string part of the URI (e.g., http://example.com/index.php?id=123&name=John) |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private array $query = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The body of the HTTP request |
||
| 53 | * |
||
| 54 | * @var ?string |
||
| 55 | */ |
||
| 56 | private ?string $body = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The maximum number of seconds to allow cURL functions to execute |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | private int $timeout = 30; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * An array of multipart data (e.g., array('name', 'contents', 'size')), for use in the multipart/form-data part of the request body |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private array $multipart = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * An array of cURL options |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private array $curlOptions = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The proxy server to use |
||
| 81 | * |
||
| 82 | * @var ?ProxyServer |
||
| 83 | */ |
||
| 84 | private ?ProxyServer $proxy = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Http Options constructor. |
||
| 88 | * |
||
| 89 | * @param array $options |
||
| 90 | */ |
||
| 91 | public function __construct(array $options = []) |
||
| 92 | { |
||
| 93 | $this->setOptions($options); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns the class as an array |
||
| 98 | * |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | public function toArray(): array |
||
| 102 | { |
||
| 103 | $result = []; |
||
| 104 | foreach (get_object_vars($this) as $key => $value) { |
||
| 105 | $result[$key] = $value; |
||
| 106 | } |
||
| 107 | return $result; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set Options |
||
| 112 | * |
||
| 113 | * @param array $options |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | public function setOptions(array $options): void |
||
| 117 | { |
||
| 118 | foreach ($options as $key => $value) { |
||
| 119 | if (method_exists($this, 'set' . ucfirst($key))) { |
||
| 120 | if ($value !== null) { |
||
| 121 | $this->{'set' . ucfirst($key)}($value); |
||
| 122 | } |
||
| 123 | } else { |
||
| 124 | if (property_exists($this, $key)) { |
||
| 125 | $this->{$key} = $value; |
||
| 126 | } else { |
||
| 127 | throw new \InvalidArgumentException("Invalid option: $key"); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set Body of Http request |
||
| 135 | * |
||
| 136 | * @param ?string|array $body The body of the request - On array it will be converted to json |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function setBody(string|array|null $body): void |
||
| 140 | { |
||
| 141 | if (is_array($body)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 142 | $this->body = json_encode($body); |
||
| 143 | $this->headers['Content-Type'] = 'application/json'; |
||
| 144 | } else { |
||
| 145 | $this->body = $body; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Set proxy server |
||
| 151 | * |
||
| 152 | * @param array $proxy ["host", "port", "user", "pass"] |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public function setProxy(array $proxy): void |
||
| 156 | { |
||
| 157 | $this->proxy = (new ProxyServer())->setProxy($proxy); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Generate URL-encoded query string. |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function getQueryString(): string |
||
| 166 | { |
||
| 167 | return http_build_query($this->query); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set Curl Options |
||
| 172 | * |
||
| 173 | * @param array $options [{"CURLOPT_*": "value"}, ...] |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function setCurlOptions(array $options): void |
||
| 177 | { |
||
| 178 | if (count($options) > 0) { |
||
| 179 | foreach ($options as $option => $value) { |
||
| 180 | $this->curlOptions[$option] = $value; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add Multipart Data |
||
| 187 | * |
||
| 188 | * @param array $multipart [{"name", "path"}, ...] |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function addMultiPart(array $multipart): void |
||
| 192 | { |
||
| 193 | $this->multipart[] = $multipart; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $name |
||
| 198 | * @param array $arguments |
||
| 199 | * @return mixed |
||
| 200 | */ |
||
| 201 | public function __call(string $name, array $arguments): mixed |
||
| 202 | { |
||
| 203 | if (method_exists($this, $name)) { |
||
| 204 | return $this->{$name}(...$arguments); |
||
| 205 | } |
||
| 206 | |||
| 207 | if (property_exists($this, $name)) { |
||
| 208 | return $this->{$name}; |
||
| 209 | } |
||
| 210 | |||
| 211 | if (str_starts_with($name, 'set')) { |
||
| 212 | $property = lcfirst(substr($name, 3)); |
||
| 213 | if (property_exists($this, $property)) { |
||
| 214 | $this->{$property} = $arguments[0]; |
||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | if (str_starts_with($name, 'get')) { |
||
| 220 | $property = lcfirst(substr($name, 3)); |
||
| 221 | if (property_exists($this, $property)) { |
||
| 222 | return $this->{$property}; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | throw new \BadMethodCallException("Method $name does not exist"); |
||
| 227 | } |
||
| 228 | |||
| 229 | } |