Request.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Unirest\Request; |
| 4 | |
| 5 | use CoreInterfaces\Core\Request\RequestInterface; |
| 6 | use CoreInterfaces\Core\Request\RequestMethod; |
| 7 | use CoreInterfaces\Http\RetryOption; |
| 8 | use Exception; |
| 9 | use InvalidArgumentException; |
| 10 | |
| 11 | class Request implements RequestInterface |
| 12 | { |
| 13 | /** |
| 14 | * This function is useful for serializing multidimensional arrays, and avoid getting |
| 15 | * the 'Array to string conversion' notice |
| 16 | * @param array|object $data array to flatten. |
| 17 | * @param bool|string $parent parent key or false if no parent |
| 18 | */ |
| 19 | public static function buildHTTPCurlQuery($data, $parent = false): array |
| 20 | { |
| 21 | $result = []; |
| 22 | |
| 23 | if (is_object($data)) { |
| 24 | $data = get_object_vars($data); |
| 25 | } |
| 26 | |
| 27 | foreach ($data as $key => $value) { |
| 28 | if (!empty($parent)) { |
| 29 | $new_key = sprintf('%s[%s]', $parent, $key); |
| 30 | } else { |
| 31 | $new_key = $key; |
| 32 | } |
| 33 | |
| 34 | if (!$value instanceof \CURLFile and (is_array($value) or is_object($value))) { |
| 35 | $result = array_merge($result, self::buildHTTPCurlQuery($value, $new_key)); |
| 36 | } else { |
| 37 | $result[$new_key] = $value; |
| 38 | } |
| 39 | } |
| 40 | return $result; |
| 41 | } |
| 42 | |
| 43 | private $httpMethod; |
| 44 | private $queryUrl; |
| 45 | private $headers; |
| 46 | private $body; |
| 47 | private $retryOption; |
| 48 | |
| 49 | /** |
| 50 | * @param string $url Query url |
| 51 | * @param string $method Http method |
| 52 | * @param array $headers Http request headers |
| 53 | * @param mixed $body Http request body |
| 54 | * @param string $retryOption To enable/disable httpMethods whitelist while retrying Api call |
| 55 | */ |
| 56 | public function __construct( |
| 57 | string $url, |
| 58 | string $method = RequestMethod::GET, |
| 59 | array $headers = [], |
| 60 | $body = null, |
| 61 | string $retryOption = RetryOption::USE_GLOBAL_SETTINGS |
| 62 | ) { |
| 63 | $this->queryUrl = $this->validateUrl($url); |
| 64 | $this->httpMethod = $method; |
| 65 | $this->headers = $headers; |
| 66 | $this->body = $body; |
| 67 | $this->retryOption = $retryOption; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Validates and processes the given Url to ensure safe usage with cURL. |
| 72 | * @param string $url The given Url to process |
| 73 | * @return string Pre-processed Url as string |
| 74 | * @throws InvalidArgumentException |
| 75 | */ |
| 76 | private function validateUrl(string $url): string |
| 77 | { |
| 78 | //ensure that the urls are absolute |
| 79 | $matchCount = preg_match("#^(https?://[^/]+)#", $url, $matches); |
| 80 | if ($matchCount == 0) { |
| 81 | throw new InvalidArgumentException('Invalid Url format.'); |
| 82 | } |
| 83 | //get the http protocol match |
| 84 | $protocol = $matches[1]; |
| 85 | |
| 86 | //remove redundant forward slashes |
| 87 | $query = substr($url, strlen($protocol)); |
| 88 | $query = preg_replace("#//+#", "/", $query); |
| 89 | |
| 90 | //return process url |
| 91 | return $protocol . $query; |
| 92 | } |
| 93 | |
| 94 | public function getHttpMethod(): string |
| 95 | { |
| 96 | return $this->httpMethod; |
| 97 | } |
| 98 | |
| 99 | public function getQueryUrl(): string |
| 100 | { |
| 101 | return $this->queryUrl; |
| 102 | } |
| 103 | |
| 104 | public function getHeaders(): array |
| 105 | { |
| 106 | return $this->headers; |
| 107 | } |
| 108 | |
| 109 | public function getParameters(): array |
| 110 | { |
| 111 | return []; |
| 112 | } |
| 113 | |
| 114 | public function getEncodedParameters(): array |
| 115 | { |
| 116 | return []; |
| 117 | } |
| 118 | |
| 119 | public function getMultipartParameters(): array |
| 120 | { |
| 121 | return []; |
| 122 | } |
| 123 | |
| 124 | public function getBody() |
| 125 | { |
| 126 | return $this->body; |
| 127 | } |
| 128 | |
| 129 | public function getRetryOption(): string |
| 130 | { |
| 131 | return $this->retryOption; |
| 132 | } |
| 133 | |
| 134 | public function convert(): Request |
| 135 | { |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | public function toApiException(string $message): Exception |
| 140 | { |
| 141 | return new Exception($message); |
| 142 | } |
| 143 | } |
| 144 |