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

Develop #302

Merged
merged 5 commits into from
Oct 28, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feature: add support of 'list' type in record
  • Loading branch information
Nikita committed Oct 27, 2021
commit 947ccb578b48f9b0f66e36b9c818febb7bf95232
101 changes: 58 additions & 43 deletions src/qtism/runtime/pci/json/Unmarshaller.php
Original file line number Diff line number Diff line change
@@ -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();
@@ -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'])) {
$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);
}

return $returnValue;
@@ -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
* @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
@@ -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,
@@ -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());
}
}