From b96acf6d5a22c833c05098256b9da5e5bf1dbf24 Mon Sep 17 00:00:00 2001 From: ignace namagana butera Date: Sat, 29 Feb 2020 20:42:23 +0100 Subject: [PATCH] Improve PR #161 --- CHANGELOG.md | 18 ++++++++++++++++++ src/UriTemplate.php | 11 +++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8ffcb19..323d2af3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ All Notable changes to `League\Uri` will be documented in this file +## Next - TBD + +### Added + +- None + +### Fixed + +- Bugfix `UriTemplate::expand` to comply with expansion rules for undefined variables [#161](https://github.com/thephpleague/uri/pull/161) thanks [Gabe Sullice](https://github.com/gabesullice) + +### Deprecated + +- Nothing + +### Remove + +- Hard dependencies on the `ext-mbstring` and the `ext-fileinfo` PHP extensions [#154](https://github.com/thephpleague/uri/pull/154) thanks [Nicolas Grekas](https://github.com/nicolas-grekas) + ## 6.2.0 - 2020-02-08 ### Added diff --git a/src/UriTemplate.php b/src/UriTemplate.php index 248d2272..639e18bb 100644 --- a/src/UriTemplate.php +++ b/src/UriTemplate.php @@ -18,6 +18,7 @@ use League\Uri\Exceptions\SyntaxError; use League\Uri\Exceptions\TemplateCanNotBeExpanded; use function array_filter; +use function array_key_exists; use function array_keys; use function explode; use function gettype; @@ -402,9 +403,7 @@ private function expandExpression(array $foundExpression): string $parts = []; /** @var array{name:string, modifier:string, position:string} $variable */ foreach ($expression['variables'] as $variable) { - if (isset($this->variables[$variable['name']])) { - $parts[] = $this->expandVariable($variable, $expression['operator'], $joiner, $useQuery); - } + $parts[] = $this->expandVariable($variable, $expression['operator'], $joiner, $useQuery); } $nullFilter = static function ($value): bool { @@ -430,7 +429,11 @@ private function expandExpression(array $foundExpression): string */ private function expandVariable(array $variable, string $operator, string $joiner, bool $useQuery): string { - $value = $this->variables[$variable['name']] ?? ''; + if (!array_key_exists($variable['name'], $this->variables)) { + return ''; + } + + $value = $this->variables[$variable['name']]; $arguments = [$value, $variable, $operator]; $method = 'expandString'; if (is_array($value)) {