Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support of 'list' type #300

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 58 additions & 43 deletions src/qtism/runtime/pci/json/Unmarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,44 +146,7 @@ public function unmarshall($json)
// This is a base.
return $this->unmarshallUnit($json);
} elseif (in_array('list', $keys)) {
$keys = array_keys($json['list']);
if (isset($keys[0]) === false) {
$msg = 'No baseType provided for list.';
throw new UnmarshallingException($msg, UnmarshallingException::NOT_PCI);
}

$baseType = BaseType::getConstantByName($keys[0]);

if ($baseType === false) {
$msg = "Unknown QTI baseType '" . $keys[0] . "'.";
$code = UnmarshallingException::NOT_PCI;
throw new UnmarshallingException($msg, $code);
}

$returnValue = new MultipleContainer($baseType);

if (!is_array($json['list'][$keys[0]])) {
$msg = 'list is not an array';
throw new UnmarshallingException($msg, UnmarshallingException::NOT_PCI);
}

// This is a list.
foreach ($json['list'][$keys[0]] as $v) {
try {
if ($v === null) {
$returnValue[] = $this->unmarshallUnit(['base' => $v]);
} else {
$returnValue[] = $this->unmarshallUnit(['base' => [$keys[0] => $v]]);
}
} catch (InvalidArgumentException $e) {
$strBaseType = BaseType::getNameByConstant($baseType);
$msg = "A value is not compliant with the '${strBaseType}' baseType.";
$code = UnmarshallingException::NOT_PCI;
throw new UnmarshallingException($msg, $code);
}
}

return $returnValue;
return $this->unmarshallList($json);
} elseif (in_array('record', $keys)) {
// This is a record.
$returnValue = new RecordContainer();
Expand All @@ -199,14 +162,14 @@ public function unmarshall($json)
throw new UnmarshallingException($msg, $code);
}

if (isset($v['base']) || (array_key_exists('base', $v) && $v['base'] === null)) {
$unit = ['base' => $v['base']];
if (isset($v['base'])) {
$returnValue[$v['name']] = $this->unmarshallUnit(['base' => $v['base']]);
} elseif (isset($v['list'])) {
quintanilhar marked this conversation as resolved.
Show resolved Hide resolved
$returnValue[$v['name']] = $this->unmarshallList($v);
} else {
// No value found, let's go for a null value.
$unit = ['base' => null];
$returnValue[$v['name']] = $this->unmarshallUnit(['base' => null]);
}

$returnValue[$v['name']] = $this->unmarshallUnit($unit);
}
poyuki marked this conversation as resolved.
Show resolved Hide resolved

return $returnValue;
Expand Down Expand Up @@ -474,4 +437,56 @@ protected function unmarshallIdentifier(array $unit)
{
return new QtiIdentifier($unit['base']['identifier']);
}

/**
* Parse associate array, return MultipleContainer
* which contains converted to BaseType items of 'list'
*
* @param array $parsedJson
* @return MultipleContainer
* @throws FileManagerException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this exception is not thrown here. Can you confirm, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IF type in 'list' will be file it can be

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unmarshallFile will be called, which can throw this exception

* @throws UnmarshallingException
*/
protected function unmarshallList(array $parsedJson)
{
$list = $parsedJson['list'];
$key = key($list);

if ($key === null) {
$msg = 'No baseType provided for list.';
throw new UnmarshallingException($msg, UnmarshallingException::NOT_PCI);
}

$baseType = BaseType::getConstantByName($key);

if ($baseType === false) {
$msg = "Unknown QTI baseType '" . $key . "'.";
$code = UnmarshallingException::NOT_PCI;
throw new UnmarshallingException($msg, $code);
}

$returnValue = new MultipleContainer($baseType);

if (!is_array($list[$key])) {
$msg = 'list is not an array';
throw new UnmarshallingException($msg, UnmarshallingException::NOT_PCI);
}

foreach ($list[$key] as $v) {
try {
if ($v === null) {
$returnValue[] = $this->unmarshallUnit(['base' => $v]);
} else {
$returnValue[] = $this->unmarshallUnit(['base' => [$key => $v]]);
}
} catch (InvalidArgumentException $e) {
$strBaseType = BaseType::getNameByConstant($baseType);
$msg = "A value is not compliant with the '${strBaseType}' baseType.";
$code = UnmarshallingException::NOT_PCI;
throw new UnmarshallingException($msg, $code);
}
}

return $returnValue;
}
}
29 changes: 27 additions & 2 deletions test/qtismtest/runtime/pci/json/JsonUnmarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public function testUnmarshallFileHash()

$json = sprintf(
'{ "base" : { "%s" : {
"mime" : "%s",
"data" : "%s",
"mime" : "%s",
"data" : "%s",
"name" : "%s",
"id" : "%s" } } }',
FileHash::FILE_HASH_KEY,
Expand Down Expand Up @@ -375,4 +375,29 @@ public function unmarshallInvalidProvider()
['{ "record" : [ { "namez" } ] '],
];
}

public function testUnmarshallListWithinRecord()
{
$unmarshaller = self::createUnmarshaller();
$json = '
{
"record": [
{ "name" : "Søyler", "list": {"string" : ["SØYLE 1: navn=1, verdi=3", "SØYLE 2: navn=1, verdi=4"] } }
]
}
';

$container = $unmarshaller->unmarshall($json);
$list = $container->getArrayCopy(true);

$key = "Søyler";

$this::assertTrue(array_key_exists($key, $list) );

$list = $list[$key];
$items = $list->getArrayCopy();

$this::assertEquals("SØYLE 1: navn=1, verdi=3", $items[0]->getValue());
$this::assertEquals("SØYLE 2: navn=1, verdi=4", $items[1]->getValue());
}
}