From c43c6eb334f85edf8fa7b5e4058b3ba35a27d960 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 5 Oct 2024 14:10:08 +0000 Subject: [PATCH] Also added COUNTRY_CODE (i forgor in the previous commit). --- VERSION | 2 +- src/Http/HttpRequest.php | 14 ++++++++++++++ src/Http/HttpRequestBuilder.php | 23 +++++++++++++++++++++++ tests/RouterTest.php | 6 +++--- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index fb1318b..5206707 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2410.51358 +0.2410.51409 diff --git a/src/Http/HttpRequest.php b/src/Http/HttpRequest.php index deaf681..5734cdc 100644 --- a/src/Http/HttpRequest.php +++ b/src/Http/HttpRequest.php @@ -17,6 +17,7 @@ class HttpRequest extends HttpMessage { /** * @param string $remoteAddr Origin remote address. * @param bool $secure true if HTTPS. + * @param string $countryCode Origin ISO 3166 country code. * @param string $version HTTP message version. * @param string $method HTTP request method. * @param string $path HTTP request path. @@ -28,6 +29,7 @@ class HttpRequest extends HttpMessage { public function __construct( private string $remoteAddr, private bool $secure, + private string $countryCode, string $version, private string $method, private string $path, @@ -57,6 +59,15 @@ class HttpRequest extends HttpMessage { return $this->secure; } + /** + * Returns the origin ISO 3166 country code. + * + * @return string Two letter country code. + */ + public function getCountryCode(): string { + return $this->countryCode; + } + /** * Retrieves the HTTP request method. * @@ -163,6 +174,9 @@ class HttpRequest extends HttpMessage { $build->setHttpVersion((string)filter_input(INPUT_SERVER, 'SERVER_PROTOCOL')); $build->setMethod((string)filter_input(INPUT_SERVER, 'REQUEST_METHOD')); + if(filter_has_var(INPUT_SERVER, 'COUNTRY_CODE')) + $build->setCountryCode((string)filter_input(INPUT_SERVER, 'COUNTRY_CODE')); + // this currently doesn't "properly" support the scenario where a full url is specified in the http request $path = (string)filter_input(INPUT_SERVER, 'REQUEST_URI'); $pathQueryOffset = strpos($path, '?'); diff --git a/src/Http/HttpRequestBuilder.php b/src/Http/HttpRequestBuilder.php index 5aec39e..cf34545 100644 --- a/src/Http/HttpRequestBuilder.php +++ b/src/Http/HttpRequestBuilder.php @@ -13,6 +13,7 @@ use InvalidArgumentException; class HttpRequestBuilder extends HttpMessageBuilder { private string $remoteAddr = '::'; private bool $secure = false; + private string $countryCode = 'XX'; private string $method = 'GET'; private string $path = '/'; @@ -61,6 +62,27 @@ class HttpRequestBuilder extends HttpMessageBuilder { $this->secure = $secure; } + /** + * Returns the origin ISO 3166 country code. + * + * @return string Two letter country code. + */ + protected function getCountryCode(): string { + return $this->countryCode; + } + + /** + * Sets the the ISO 3166 country code for this request. + * + * @param string $countryCode Two letter country code. + */ + public function setCountryCode(string $countryCode): void { + if(strlen($countryCode) !== 2) + throw new InvalidArgumentException('$countryCode must be two characters'); + + $this->countryCode = strtoupper($countryCode); + } + /** * Returns HTTP request method. * @@ -180,6 +202,7 @@ class HttpRequestBuilder extends HttpMessageBuilder { return new HttpRequest( $this->getRemoteAddress(), $this->isSecure(), + $this->getCountryCode(), $this->getHttpVersion(), $this->getMethod(), $this->getPath(), diff --git a/tests/RouterTest.php b/tests/RouterTest.php index e746862..64e4f9f 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -174,15 +174,15 @@ final class RouterTest extends TestCase { $router->get('/test', fn() => 'also unexpected'); ob_start(); - $router->dispatch(new HttpRequest('::1', true, '1.1', 'GET', '/', [], [], new HttpHeaders([]), null)); + $router->dispatch(new HttpRequest('::1', true, 'NL', '1.1', 'GET', '/', [], [], new HttpHeaders([]), null)); $this->assertEquals('expected', ob_get_clean()); ob_start(); - $router->dispatch(new HttpRequest('::1', true, '1.1', 'GET', '/test', [], [], new HttpHeaders([]), null)); + $router->dispatch(new HttpRequest('::1', true, 'US', '1.1', 'GET', '/test', [], [], new HttpHeaders([]), null)); $this->assertEquals('expected', ob_get_clean()); ob_start(); - $router->dispatch(new HttpRequest('::1', true, '1.1', 'GET', '/error', [], [], new HttpHeaders([]), null)); + $router->dispatch(new HttpRequest('::1', true, 'GB', '1.1', 'GET', '/error', [], [], new HttpHeaders([]), null)); $this->assertEquals('expected', ob_get_clean()); } }