diff --git a/src/Servers/Base.php b/src/Servers/Base.php index 3adf7f8..1f28d26 100644 --- a/src/Servers/Base.php +++ b/src/Servers/Base.php @@ -239,17 +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 - $existsInRequest = \array_key_exists($key, $requestParams); - $existsInValues = \array_key_exists($key, $values); + $requestKey = $key; + if (!\array_key_exists($key, $requestParams) && !empty($param['aliases'])) { + foreach ($param['aliases'] as $alias) { + if (\array_key_exists($alias, $requestParams)) { + $requestKey = $alias; + break; + } + } + } + + $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[$key] : $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 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());