Skip to content

Commit dc39617

Browse files
authored
Merge pull request #64 from collecthor/fix-matrixdropdown-missing-celltype
fix: parsing matrixdropdown with choices at root level and missing celltype
2 parents 443f4e7 + eab5592 commit dc39617

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/Parsers/MultipleChoiceMatrixParser.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function parse(ElementParserInterface $root, array $questionConfig, Surve
2323
/** @var list<array<string, mixed>> $columns */
2424
$columns = $this->extractArray($questionConfig, 'columns');
2525

26+
$defaultChoices = $this->extractOptionalArray($questionConfig, 'choices');
27+
2628

2729
foreach ($rows as $row) {
2830
$rowName = !is_string($row) ? $row['value'] : $row;
@@ -38,7 +40,8 @@ public function parse(ElementParserInterface $root, array $questionConfig, Surve
3840
$columnQuestion['hasNone'] = true;
3941
}
4042
$columnQuestion['title'] = $title;
41-
$columnQuestion['type'] = $column['cellType'];
43+
$columnQuestion['type'] = $column['cellType'] ?? 'dropdown';
44+
$columnQuestion['choices'] = $columnQuestion['choices'] ?? $defaultChoices;
4245
yield from $root->parse($root, $columnQuestion, $surveyConfiguration, $prefix);
4346
}
4447
}

tests/Parsers/MultipleChoiceMatrixParserTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @uses \Collecthor\SurveyjsParser\Values\ValueSet
3232
* @uses \Collecthor\SurveyjsParser\Variables\MultipleChoiceVariable
3333
* @uses \Collecthor\SurveyjsParser\Variables\SingleChoiceVariable
34+
* @uses \Collecthor\SurveyjsParser\Values\IntegerValueOption
3435
*/
3536
final class MultipleChoiceMatrixParserTest extends TestCase
3637
{
@@ -398,4 +399,48 @@ public function testValuePath(): void
398399
self::assertEquals("Pakketten", $answers[0]->getDisplayValue());
399400
self::assertEquals("Pallets", $answers[1]->getDisplayValue());
400401
}
402+
403+
404+
public function testNoCellType(): void
405+
{
406+
$config = json_decode(<<<JSON
407+
{
408+
"type": "matrixdropdown",
409+
"name": "question2",
410+
"columns": [
411+
{
412+
"name": "Column 1"
413+
},
414+
{
415+
"name": "Column 2"
416+
},
417+
{
418+
"name": "Column 3"
419+
}
420+
],
421+
"choices": [
422+
1,
423+
2,
424+
3,
425+
4,
426+
5
427+
],
428+
"rows": [
429+
"Row 1",
430+
"Row 2"
431+
]
432+
}
433+
JSON, true);
434+
$parser = new MultipleChoiceMatrixParser();
435+
$surveyConfig = new SurveyConfiguration();
436+
/**
437+
* @var list<MultipleChoiceVariable> $result
438+
*/
439+
$result = toArray($parser->parse($this->getRootParser(), $config, $surveyConfig, []));
440+
441+
self::assertCount(6, $result);
442+
foreach ($result as $variable) {
443+
self::assertCount(5, $variable->getValueOptions());
444+
}
445+
}
401446
}

0 commit comments

Comments
 (0)