Skip to content

Commit d27c9ff

Browse files
authored
#27 fixed missing sub-key in template data for nested arrays (#28)
1 parent 251bb0a commit d27c9ff

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/Component/ComponentItemFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private function createVariationParameters(array $parameters, array $variation):
127127

128128
foreach ($parameters as $name => $type) {
129129
if (\is_array($type)) {
130-
$paramValue = $this->createVariationParameters($type, $variation[$name] ?? []);
130+
$paramValue[$name] = $this->createVariationParameters($type, $variation[$name] ?? []);
131131
} else {
132132
$paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? null);
133133
}

src/Component/Data/Faker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private function createFakeData(array $params, mixed $variation): array
4646

4747
if (!\array_key_exists($name, $result)) {
4848
// set from variation
49-
$result[$name] = $variation;
49+
$result[$name] = $variation[$name] ?? $variation;
5050
}
5151
}
5252

src/Component/Data/Generator/NullGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class NullGenerator implements GeneratorInterface
1010
{
1111
public function supports(string $type, mixed $context = null): bool
1212
{
13-
return empty($context);
13+
return null === $context || '' === $context;
1414
}
1515

1616
public function generate(string $type, mixed $context = null): null

tests/Functional/Service/ComponentItemFactoryTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,43 @@ public function testCreateForParamWithOptionalVariationValue(): void
242242
self::assertNull($variations['variation1']['optionalEmpty']);
243243
}
244244

245+
public function testCreateForArrayParameter(): void
246+
{
247+
$data = [
248+
'name' => 'TestComponent',
249+
'title' => 'Test title',
250+
'description' => 'description',
251+
'category' => 'MainCategory',
252+
'path' => 'path/to/component',
253+
'renderPath' => 'path/to/component',
254+
'parameters' => [
255+
'arrayParam' => [
256+
'param1' => 'String',
257+
'param2' => 'Boolean',
258+
],
259+
],
260+
'variations' => [
261+
'variation1' => [
262+
'arrayParam' => [
263+
'param1' => 'Some cool hipster text',
264+
'param2' => false,
265+
],
266+
],
267+
],
268+
];
269+
270+
/** @var ComponentItemFactory $factory */
271+
$factory = self::getContainer()->get('twig_doc.service.component_factory');
272+
273+
$component = $factory->create($data);
274+
$variations = $component->getVariations();
275+
276+
self::assertIsArray($variations);
277+
self::assertArrayHasKey('variation1', $variations);
278+
self::assertEquals('Some cool hipster text', $variations['variation1']['arrayParam']['param1']);
279+
self::assertFalse($variations['variation1']['arrayParam']['param2']);
280+
}
281+
245282
public static function getInvalidComponentConfigurationTestCases(): iterable
246283
{
247284
yield [

0 commit comments

Comments
 (0)