Exemple #1
0
 /**
  * Create a new RecordContainer object.
  *
  * @param array $array An associative array.
  * @throws \InvalidArgumentException If the given $array is not associative.
  */
 public function __construct(array $array = array())
 {
     if (Arrays::isAssoc($array)) {
         $dataPlaceHolder =& $this->getDataPlaceHolder();
         foreach ($array as $k => $v) {
             $this->checkType($v);
             $dataPlaceHolder[$k] = $v;
         }
         reset($dataPlaceHolder);
     } else {
         $msg = "The array argument must be an associative array.";
         throw new InvalidArgumentException($msg);
     }
 }
 /**
  * Transform a PCI JSON representation of QTI data into the QTISM runtime model.
  * 
  * @param string|array $json The json data to be transformed.
  * @throws UnmarshallingException If an error occurs while processing $json.
  * @return null|qtism\common\datatypes\QtiDataType|array
  */
 public function unmarshall($json)
 {
     if (is_string($json) === true) {
         $tmpJson = @json_decode($json, true);
         if ($tmpJson === null) {
             // An error occured while decoding.
             $msg = "An error occured while decoding the following JSON data '" . mb_substr($json, 0, 30, 'UTF-8') . "...'.";
             $code = UnmarshallingException::JSON_DECODE;
             throw new UnmarshallingException($msg, $code);
         }
         $json = $tmpJson;
     }
     if (is_array($json) === false || count($json) === 0) {
         $msg = "The '" . get_class($this) . "::unmarshall' method only accepts a JSON string or a non-empty array as argument, '";
         if (is_object($json) === true) {
             $msg .= get_class($json);
         } else {
             $msg .= gettype($json);
         }
         $msg .= "' given.";
         $code = UnmarshallingException::NOT_SUPPORTED;
         throw new UnmarshallingException($msg, $code);
     }
     if (Arrays::isAssoc($json) === false) {
         $msg = "The '" . get_class($this) . "::unmarshall' does not accepts non-associative arrays.";
         $code = UnmarshallingException::NOT_SUPPORTED;
         throw new UnmarshallingException($msg, $code);
     }
     // Check whether or not $json is a state (no 'base' nor 'list' keys found),
     // a base, a list or a record.
     $keys = array_keys($json);
     if (in_array('base', $keys) === true) {
         // This is a base.
         return $this->unmarshallUnit($json);
     } else {
         if (in_array('list', $keys) === true) {
             $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);
             // This is a list.
             foreach ($json['list'][$keys[0]] as $v) {
                 try {
                     if ($v === null) {
                         $returnValue[] = $this->unmarshallUnit(array('base' => $v));
                     } else {
                         $returnValue[] = $this->unmarshallUnit(array('base' => array($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;
         } else {
             if (in_array('record', $keys) === true) {
                 // This is a record.
                 $returnValue = new RecordContainer();
                 if (count($json['record']) === 0) {
                     return $returnValue;
                 }
                 foreach ($json['record'] as $v) {
                     if (isset($v['name']) === false) {
                         $msg = "No 'name' key found in record field.";
                         $code = UnmarshallingException::NOT_PCI;
                         throw new UnmarshallingException($msg, $code);
                     }
                     if (isset($v['base']) === true || array_key_exists('base', $v) && $v['base'] === null) {
                         $unit = array('base' => $v['base']);
                     } else {
                         // No value found, let's go for a null value.
                         $unit = array('base' => null);
                     }
                     $returnValue[$v['name']] = $this->unmarshallUnit($unit);
                 }
                 return $returnValue;
             } else {
                 // This is a state.
                 $state = array();
                 foreach ($json as $k => $j) {
                     $state[$k] = $this->unmarshall($j);
                 }
                 return $state;
             }
         }
     }
 }
 /**
  * @dataProvider isAssocInvalidProvider
  */
 public function testIsAssocInvalid(array $array)
 {
     $this->assertFalse(Arrays::isAssoc($array));
 }