Add HTTP method to App\Arguments

This commit is contained in:
Philipp 2022-01-02 22:21:41 +01:00
parent c7f2ba213b
commit ee2a15d822
No known key found for this signature in database
GPG Key ID: 24A7501396EB5432
2 changed files with 40 additions and 2 deletions

View File

@ -52,14 +52,19 @@ class Arguments
* @var int The count of arguments * @var int The count of arguments
*/ */
private $argc; private $argc;
/**
* @var string The used HTTP method
*/
private $method;
public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0) public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0, string $method = Router::GET)
{ {
$this->queryString = $queryString; $this->queryString = $queryString;
$this->command = $command; $this->command = $command;
$this->moduleName = $moduleName; $this->moduleName = $moduleName;
$this->argv = $argv; $this->argv = $argv;
$this->argc = $argc; $this->argc = $argc;
$this->method = $method;
} }
/** /**
@ -94,6 +99,11 @@ class Arguments
return $this->argv; return $this->argv;
} }
public function getMethod()
{
return $this->method;
}
/** /**
* @return int The count of arguments of this call * @return int The count of arguments of this call
*/ */
@ -199,6 +209,8 @@ class Arguments
$module = "login"; $module = "login";
} }
return new Arguments($queryString, $command, $module, $argv, $argc); $httpMethod = in_array($server['REQUEST_METHOD'] ?? '', Router::ALLOWED_METHODS) ? $server['REQUEST_METHOD'] : Router::GET;
return new Arguments($queryString, $command, $module, $argv, $argc, $httpMethod);
} }
} }

View File

@ -32,6 +32,7 @@ class ArgumentsTest extends TestCase
self::assertEquals($assert['command'], $arguments->getCommand()); self::assertEquals($assert['command'], $arguments->getCommand());
self::assertEquals($assert['argv'], $arguments->getArgv()); self::assertEquals($assert['argv'], $arguments->getArgv());
self::assertEquals($assert['argc'], $arguments->getArgc()); self::assertEquals($assert['argc'], $arguments->getArgc());
self::assertEquals($assert['method'], $arguments->getMethod());
self::assertCount($assert['argc'], $arguments->getArgv()); self::assertCount($assert['argc'], $arguments->getArgv());
} }
@ -47,6 +48,7 @@ class ArgumentsTest extends TestCase
'command' => '', 'command' => '',
'argv' => [], 'argv' => [],
'argc' => 0, 'argc' => 0,
'method' => App\Router::GET
], ],
$arguments); $arguments);
} }
@ -60,6 +62,7 @@ class ArgumentsTest extends TestCase
'command' => 'profile/test/it', 'command' => 'profile/test/it',
'argv' => ['profile', 'test', 'it'], 'argv' => ['profile', 'test', 'it'],
'argc' => 3, 'argc' => 3,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2', 'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2',
@ -74,6 +77,7 @@ class ArgumentsTest extends TestCase
'command' => '~test/it', 'command' => '~test/it',
'argv' => ['~test', 'it'], 'argv' => ['~test', 'it'],
'argc' => 2, 'argc' => 2,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'pagename=~test/it&arg1=value1&arg2=value2', 'QUERY_STRING' => 'pagename=~test/it&arg1=value1&arg2=value2',
@ -88,6 +92,7 @@ class ArgumentsTest extends TestCase
'command' => 'u/test/it', 'command' => 'u/test/it',
'argv' => ['u', 'test', 'it'], 'argv' => ['u', 'test', 'it'],
'argc' => 3, 'argc' => 3,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'pagename=u/test/it&arg1=value1&arg2=value2', 'QUERY_STRING' => 'pagename=u/test/it&arg1=value1&arg2=value2',
@ -102,6 +107,7 @@ class ArgumentsTest extends TestCase
'command' => 'profile/test/it', 'command' => 'profile/test/it',
'argv' => ['profile', 'test', 'it'], 'argv' => ['profile', 'test', 'it'],
'argc' => 3, 'argc' => 3,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2/', 'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2/',
@ -116,6 +122,7 @@ class ArgumentsTest extends TestCase
'command' => 'profile/test/it', 'command' => 'profile/test/it',
'argv' => ['profile', 'test', 'it'], 'argv' => ['profile', 'test', 'it'],
'argc' => 3, 'argc' => 3,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'wrong=profile/test/it&arg1=value1&arg2=value2/', 'QUERY_STRING' => 'wrong=profile/test/it&arg1=value1&arg2=value2/',
@ -130,6 +137,7 @@ class ArgumentsTest extends TestCase
'command' => 'notvalid/it', 'command' => 'notvalid/it',
'argv' => ['notvalid', 'it'], 'argv' => ['notvalid', 'it'],
'argc' => 2, 'argc' => 2,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'pagename=notvalid/it&arg1=value1&arg2=value2/', 'QUERY_STRING' => 'pagename=notvalid/it&arg1=value1&arg2=value2/',
@ -143,6 +151,7 @@ class ArgumentsTest extends TestCase
'command' => '', 'command' => '',
'argv' => [], 'argv' => [],
'argc' => 0, 'argc' => 0,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'arg1=value1&arg2=value2/', 'QUERY_STRING' => 'arg1=value1&arg2=value2/',
@ -156,6 +165,7 @@ class ArgumentsTest extends TestCase
'command' => 'api/call.json', 'command' => 'api/call.json',
'argv' => ['api', 'call.json'], 'argv' => ['api', 'call.json'],
'argc' => 2, 'argc' => 2,
'method' => App\Router::GET,
], ],
'server' => [ 'server' => [
'QUERY_STRING' => 'pagename=api/call.json', 'QUERY_STRING' => 'pagename=api/call.json',
@ -164,6 +174,22 @@ class ArgumentsTest extends TestCase
'pagename' => 'api/call.json' 'pagename' => 'api/call.json'
], ],
], ],
'withHTTPMethod' => [
'assert' => [
'queryString' => 'api/call.json',
'command' => 'api/call.json',
'argv' => ['api', 'call.json'],
'argc' => 2,
'method' => App\Router::POST,
],
'server' => [
'QUERY_STRING' => 'pagename=api/call.json',
'REQUEST_METHOD' => App\Router::POST,
],
'get' => [
'pagename' => 'api/call.json'
],
],
]; ];
} }