Skip to content

Commit

Permalink
Fix broken code that assumes array keys are strings (#120)
Browse files Browse the repository at this point in the history
Fix broken code that assumes array keys are strings see #119
  • Loading branch information
GrahamCampbell authored Sep 8, 2023
1 parent d912b61 commit 656e759
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
10 changes: 6 additions & 4 deletions uri/UriTemplate/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ final class Expression
private function __construct(public readonly Operator $operator, VarSpecifier ...$varSpecifiers)
{
$this->varSpecifiers = $varSpecifiers;
$this->variableNames = array_keys(array_fill_keys(
array_map(static fn (VarSpecifier $varSpecifier): string => $varSpecifier->name, $varSpecifiers),
1
));
$this->variableNames = array_unique(
array_map(
static fn (VarSpecifier $varSpecifier): string => $varSpecifier->name,
$varSpecifiers
)
);
$this->value = '{'.$operator->value.implode(',', array_map(
static fn (VarSpecifier $varSpecifier): string => $varSpecifier->toString(),
$varSpecifiers
Expand Down
16 changes: 8 additions & 8 deletions uri/UriTemplate/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ final class Template implements Stringable
private function __construct(public readonly string $value, Expression ...$expressions)
{
$this->expressions = $expressions;
$this->variableNames = array_keys(array_reduce(
$expressions,
fn (array $curry, Expression $expression): array => [
...$curry,
...array_fill_keys($expression->variableNames, 1),
],
[]
));
$this->variableNames = array_unique(
array_merge(
...array_map(
static fn (Expression $expression): array => $expression->variableNames,
$expressions
)
)
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions uri/UriTemplate/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public static function expectedVariableNames(): iterable
'expected' => [],
],
[
'template' => '{foo}{bar}',
'expected' => ['foo', 'bar'],
'template' => '{foo}{bar}{420}',
'expected' => ['foo', 'bar', '420'],
],
[
'template' => '{foo}{foo:2}{+foo}',
Expand Down

0 comments on commit 656e759

Please sign in to comment.