Skip to content

Commit

Permalink
[BUGFIX] Fix Iterator/Extract with single=true and array subject
Browse files Browse the repository at this point in the history
Close: #1833
  • Loading branch information
NamelessCoder committed Jul 12, 2023
1 parent 6f8c743 commit a9ae125
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Classes/ViewHelpers/Iterator/ExtractViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static function renderStatic(
$result = [];
}

if ($single && $result instanceof \Traversable) {
if ($single && ($result instanceof \Traversable || is_array($result))) {
return reset($result);
}

Expand Down
20 changes: 20 additions & 0 deletions Tests/Unit/ViewHelpers/Iterator/ExtractViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ public function extractByKeyExtractsKeyByPath($structure, $key, $expected)
);
}

/**
* @test
* @dataProvider simpleStructures
*/
public function extractByKeyExtractsKeyByPathWithSingle($structure, $key, $expected)
{
if (is_array($expected)) {
$expected = reset($expected);
}
$this->assertEquals(
$expected,
$this->executeViewHelper(['content' => $structure, 'key' => $key, 'recursive' => false, 'single' => true])
);
}

/**
* @return array
*/
Expand All @@ -185,6 +200,11 @@ public function simpleStructures()
'myKey',
'myValue'
],
'flat associative array with array as value' => [
['myKey' => ['foo' => 'myValue']],
'myKey',
['foo' => 'myValue'],
],
'deeper associative array' => [
[
'myFirstKey' => [
Expand Down

0 comments on commit a9ae125

Please sign in to comment.