From 2cec9c78859b3efc696ca58dfe530144e9d20269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 13:39:00 +0200 Subject: [PATCH 1/2] Support aliases --- src/Servers/Base.php | 14 ++++++++++++-- src/Servers/Hook.php | 4 +++- tests/Servers/Unit/HookTest.php | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Servers/Base.php b/src/Servers/Base.php index 3adf7f8..afa936e 100644 --- a/src/Servers/Base.php +++ b/src/Servers/Base.php @@ -239,10 +239,20 @@ protected function prepare(Container $context, Hook $hook, array $values = [], a $scope = new Container($context); foreach ($hook->getParams() as $key => $param) { // Get value from route or request object - $existsInRequest = \array_key_exists($key, $requestParams); + $actualKey = $key; + if (!\array_key_exists($key, $requestParams) && !empty($param['aliases'])) { + foreach ($param['aliases'] as $alias) { + if (\array_key_exists($alias, $requestParams)) { + $actualKey = $alias; + break; + } + } + } + + $existsInRequest = \array_key_exists($actualKey, $requestParams); $existsInValues = \array_key_exists($key, $values); $paramExists = $existsInRequest || $existsInValues; - $arg = $existsInRequest ? $requestParams[$key] : $param['default']; + $arg = $existsInRequest ? $requestParams[$actualKey] : $param['default']; // Adding is string to avoid PHP built-in functions if (!is_string($arg) && \is_callable($arg)) { diff --git a/src/Servers/Hook.php b/src/Servers/Hook.php index 4a645ce..f8e0202 100644 --- a/src/Servers/Hook.php +++ b/src/Servers/Hook.php @@ -222,9 +222,10 @@ public function inject(string $injection): static * @param bool $deprecated * @param string $example * @param string|null $model + * @param array $aliases * @return static */ - public function param(string $key, mixed $default, Validator|callable $validator, string $description = '', bool $optional = false, array $injections = [], bool $skipValidation = false, bool $deprecated = false, string $example = '', ?string $model = null): static + public function param(string $key, mixed $default, Validator|callable $validator, string $description = '', bool $optional = false, array $injections = [], bool $skipValidation = false, bool $deprecated = false, string $example = '', ?string $model = null, array $aliases = []): static { $this->params[$key] = [ 'default' => $default, @@ -236,6 +237,7 @@ public function param(string $key, mixed $default, Validator|callable $validator 'deprecated' => $deprecated, 'example' => $example, 'model' => $model, + 'aliases' => $aliases, 'value' => null, 'order' => count($this->params) + count($this->injections), ]; diff --git a/tests/Servers/Unit/HookTest.php b/tests/Servers/Unit/HookTest.php index dcff538..22314e7 100644 --- a/tests/Servers/Unit/HookTest.php +++ b/tests/Servers/Unit/HookTest.php @@ -58,6 +58,29 @@ public function testParamCanBeSet() $this->assertCount(2, $this->hook->getParams()); } + public function testParamAliasesDefaultEmpty() + { + $this->hook->param('x', '', new Text(10)); + + $params = $this->hook->getParams(); + $this->assertArrayHasKey('aliases', $params['x']); + $this->assertSame([], $params['x']['aliases']); + } + + public function testParamAliasesCanBeSet() + { + $this->hook->param( + 'projectId', + '', + new Text(64), + description: '', + aliases: ['project', 'project_id'] + ); + + $params = $this->hook->getParams(); + $this->assertSame(['project', 'project_id'], $params['projectId']['aliases']); + } + public function testResourcesCanBeInjected() { $this->assertEquals([], $this->hook->getInjections()); From 24bbd8fbb6550bc72f2816d9b0c3b690e38e17fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 13:57:44 +0200 Subject: [PATCH 2/2] fix url params --- src/Servers/Base.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Servers/Base.php b/src/Servers/Base.php index afa936e..1f28d26 100644 --- a/src/Servers/Base.php +++ b/src/Servers/Base.php @@ -239,27 +239,37 @@ protected function prepare(Container $context, Hook $hook, array $values = [], a $scope = new Container($context); foreach ($hook->getParams() as $key => $param) { // Get value from route or request object - $actualKey = $key; + $requestKey = $key; if (!\array_key_exists($key, $requestParams) && !empty($param['aliases'])) { foreach ($param['aliases'] as $alias) { if (\array_key_exists($alias, $requestParams)) { - $actualKey = $alias; + $requestKey = $alias; break; } } } - $existsInRequest = \array_key_exists($actualKey, $requestParams); - $existsInValues = \array_key_exists($key, $values); + $valuesKey = $key; + if (!\array_key_exists($key, $values) && !empty($param['aliases'])) { + foreach ($param['aliases'] as $alias) { + if (\array_key_exists($alias, $values)) { + $valuesKey = $alias; + break; + } + } + } + + $existsInRequest = \array_key_exists($requestKey, $requestParams); + $existsInValues = \array_key_exists($valuesKey, $values); $paramExists = $existsInRequest || $existsInValues; - $arg = $existsInRequest ? $requestParams[$actualKey] : $param['default']; + $arg = $existsInRequest ? $requestParams[$requestKey] : $param['default']; // Adding is string to avoid PHP built-in functions if (!is_string($arg) && \is_callable($arg)) { $injections = array_map(fn ($injection) => $scope->get($injection), $param['injections']); $arg = \call_user_func_array($arg, $injections); } - $value = $existsInValues ? $values[$key] : $arg; + $value = $existsInValues ? $values[$valuesKey] : $arg; /** * Validation