Also added COUNTRY_CODE (i forgor in the previous commit).

This commit is contained in:
flash 2024-10-05 14:10:08 +00:00
parent efe7d181d4
commit c43c6eb334
4 changed files with 41 additions and 4 deletions

View file

@ -1 +1 @@
0.2410.51358 0.2410.51409

View file

@ -17,6 +17,7 @@ class HttpRequest extends HttpMessage {
/** /**
* @param string $remoteAddr Origin remote address. * @param string $remoteAddr Origin remote address.
* @param bool $secure true if HTTPS. * @param bool $secure true if HTTPS.
* @param string $countryCode Origin ISO 3166 country code.
* @param string $version HTTP message version. * @param string $version HTTP message version.
* @param string $method HTTP request method. * @param string $method HTTP request method.
* @param string $path HTTP request path. * @param string $path HTTP request path.
@ -28,6 +29,7 @@ class HttpRequest extends HttpMessage {
public function __construct( public function __construct(
private string $remoteAddr, private string $remoteAddr,
private bool $secure, private bool $secure,
private string $countryCode,
string $version, string $version,
private string $method, private string $method,
private string $path, private string $path,
@ -57,6 +59,15 @@ class HttpRequest extends HttpMessage {
return $this->secure; 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. * Retrieves the HTTP request method.
* *
@ -163,6 +174,9 @@ class HttpRequest extends HttpMessage {
$build->setHttpVersion((string)filter_input(INPUT_SERVER, 'SERVER_PROTOCOL')); $build->setHttpVersion((string)filter_input(INPUT_SERVER, 'SERVER_PROTOCOL'));
$build->setMethod((string)filter_input(INPUT_SERVER, 'REQUEST_METHOD')); $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 // 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'); $path = (string)filter_input(INPUT_SERVER, 'REQUEST_URI');
$pathQueryOffset = strpos($path, '?'); $pathQueryOffset = strpos($path, '?');

View file

@ -13,6 +13,7 @@ use InvalidArgumentException;
class HttpRequestBuilder extends HttpMessageBuilder { class HttpRequestBuilder extends HttpMessageBuilder {
private string $remoteAddr = '::'; private string $remoteAddr = '::';
private bool $secure = false; private bool $secure = false;
private string $countryCode = 'XX';
private string $method = 'GET'; private string $method = 'GET';
private string $path = '/'; private string $path = '/';
@ -61,6 +62,27 @@ class HttpRequestBuilder extends HttpMessageBuilder {
$this->secure = $secure; $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. * Returns HTTP request method.
* *
@ -180,6 +202,7 @@ class HttpRequestBuilder extends HttpMessageBuilder {
return new HttpRequest( return new HttpRequest(
$this->getRemoteAddress(), $this->getRemoteAddress(),
$this->isSecure(), $this->isSecure(),
$this->getCountryCode(),
$this->getHttpVersion(), $this->getHttpVersion(),
$this->getMethod(), $this->getMethod(),
$this->getPath(), $this->getPath(),

View file

@ -174,15 +174,15 @@ final class RouterTest extends TestCase {
$router->get('/test', fn() => 'also unexpected'); $router->get('/test', fn() => 'also unexpected');
ob_start(); 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()); $this->assertEquals('expected', ob_get_clean());
ob_start(); 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()); $this->assertEquals('expected', ob_get_clean());
ob_start(); 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()); $this->assertEquals('expected', ob_get_clean());
} }
} }