Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Servers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Servers/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
];
Expand Down
23 changes: 23 additions & 0 deletions tests/Servers/Unit/HookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Comment thread
Meldiron marked this conversation as resolved.
Expand Down
Loading