1010use BasicHttpClient \Request \Transport \HttpsTransport ;
1111use BasicHttpClient \Request \Transport \HttpTransport ;
1212use BasicHttpClient \Response \ResponseInterface ;
13- use BasicHttpClient \Util \UrlUtil ;
1413use CommonException \NetworkException \Base \NetworkException ;
1514use CommonException \NetworkException \ConnectionTimeoutException ;
1615use CommonException \NetworkException \CurlException ;
16+ use Url \UrlInterface ;
1717
1818/**
1919 * Class Request
@@ -28,25 +28,15 @@ abstract class AbstractRequest implements RequestInterface
2828 */
2929 private $ userAgent = 'PHP Basic HTTP Client 1.0 ' ;
3030
31- /**
32- * @var string
33- */
34- private $ endpoint ;
35-
36- /**
37- * @var int
38- */
39- private $ port ;
40-
4131 /**
4232 * @var string
4333 */
4434 private $ method = self ::REQUEST_METHOD_GET ;
4535
4636 /**
47- * @var string[]
37+ * @var UrlInterface
4838 */
49- private $ queryParameters = array () ;
39+ private $ url ;
5040
5141 /**
5242 * @var TransportInterface
@@ -114,54 +104,6 @@ public function setUserAgent($userAgent)
114104 return $ this ;
115105 }
116106
117- /**
118- * @return string
119- */
120- public function getEndpoint ()
121- {
122- return $ this ->endpoint ;
123- }
124-
125- /**
126- * @param string $endpoint
127- * @return $this
128- */
129- public function setEndpoint ($ endpoint )
130- {
131- $ urlUtil = new UrlUtil ();
132- if (!$ urlUtil ->validateUrl ($ endpoint )) {
133- throw new \InvalidArgumentException ('The given endpoint is not a valid URL ' );
134- }
135- $ this ->endpoint = $ endpoint ;
136- return $ this ;
137- }
138-
139- /**
140- * @return int
141- */
142- public function getPort ()
143- {
144- return $ this ->port ;
145- }
146-
147- /**
148- * @return bool
149- */
150- public function hasPort ()
151- {
152- return !is_null ($ this ->port );
153- }
154-
155- /**
156- * @param int $port
157- * @return $this
158- */
159- public function setPort ($ port )
160- {
161- $ this ->port = $ port ;
162- return $ this ;
163- }
164-
165107 /**
166108 * @return string
167109 */
@@ -181,60 +123,20 @@ public function setMethod($method)
181123 }
182124
183125 /**
184- * @return string[]
185- */
186- public function getQueryParameters ()
187- {
188- return $ this ->queryParameters ;
189- }
190-
191- /**
192- * @return bool
193- */
194- public function hasQueryParameters ()
195- {
196- return count ($ this ->queryParameters ) > 0 ;
197- }
198-
199- /**
200- * @param string $parameterName
201- * @param string $parameterValue
202- * @return $this
203- */
204- public function addQueryParameter ($ parameterName , $ parameterValue )
205- {
206- if (!is_string ($ parameterName ) || !is_string ($ parameterValue )) {
207- throw new \InvalidArgumentException ('Query parameter names and values have to be a string. ' );
208- }
209- $ this ->queryParameters [$ parameterName ] = $ parameterValue ;
210- return $ this ;
211- }
212-
213- /**
214- * @param string[] $queryParameters
215- * @return $this
126+ * @return UrlInterface
216127 */
217- public function setQueryParameters ( $ queryParameters )
128+ public function getUrl ( )
218129 {
219- foreach ($ queryParameters as &$ queryParameter ) {
220- if (!is_scalar ($ queryParameter )) {
221- $ argumentType = (is_object ($ queryParameter )) ? get_class ($ queryParameter ) : gettype ($ queryParameter );
222- throw new \InvalidArgumentException (
223- 'Expected the query parameters as array of scalar values. Got ' . $ argumentType
224- );
225- }
226- $ queryParameter = (string )$ queryParameter ;
227- }
228- $ this ->queryParameters = $ queryParameters ;
229- return $ this ;
130+ return $ this ->url ;
230131 }
231132
232133 /**
134+ * @param UrlInterface $url
233135 * @return $this
234136 */
235- public function removeQueryParameters ( )
137+ public function setUrl ( UrlInterface $ url )
236138 {
237- $ this ->queryParameters = array () ;
139+ $ this ->url = $ url ;
238140 return $ this ;
239141 }
240142
@@ -364,8 +266,8 @@ public function configureCurl($curl)
364266 curl_setopt ($ curl , CURLINFO_HEADER_OUT , true );
365267 curl_setopt ($ curl , CURLOPT_USERAGENT , $ this ->getUserAgent ());
366268 curl_setopt ($ curl , CURLOPT_URL , $ this ->calculateEndpoint ());
367- if ($ this ->hasPort ()) {
368- curl_setopt ($ curl , CURLOPT_PORT , $ this ->getPort ());
269+ if ($ this ->getUrl ()-> hasPort ()) {
270+ curl_setopt ($ curl , CURLOPT_PORT , $ this ->getUrl ()-> getPort ());
369271 }
370272 // Request method
371273 curl_setopt ($ curl , CURLOPT_HTTPGET , true );
@@ -469,12 +371,7 @@ public function getEffectiveHeaders()
469371 */
470372 protected function calculateEndpoint ()
471373 {
472- $ endpoint = $ this ->getEndpoint ();
473- if ($ this ->hasQueryParameters ()) {
474- $ glueCharacter = (strpos ($ endpoint , '? ' ) === false ) ? '? ' : '& ' ;
475- $ endpoint .= $ glueCharacter . http_build_query ($ this ->getQueryParameters ());
476- }
477- return $ endpoint ;
374+ return $ this ->getUrl ()->buildUrl ();
478375 }
479376
480377 /**
@@ -483,8 +380,10 @@ protected function calculateEndpoint()
483380 */
484381 protected function prePerform ()
485382 {
486- $ urlUtil = new UrlUtil ();
487- if ($ urlUtil ->getScheme ($ this ->getEndpoint ()) == 'HTTPS ' && !$ this ->getTransport () instanceof HttpsTransport) {
383+ if (
384+ mb_strtoupper ($ this ->getUrl ()->getScheme ()) == 'HTTPS '
385+ && !$ this ->getTransport () instanceof HttpsTransport
386+ ) {
488387 throw new HttpRequestException ('Transport misconfiguration. Use HttpsTransport for HTTPS requests. ' );
489388 }
490389 }
0 commit comments