protected function marshallUnit($unit)
    {
        if (is_null($unit) === true) {
            $json = array('base' => null);
        } elseif ($unit instanceof QtiScalar) {
            $json = $this->marshallScalar($unit);
        } elseif ($unit instanceof MultipleContainer) {
            $json = array();
            $strBaseType = BaseType::getNameByConstant($unit->getBaseType());
            $json['list'] = array($strBaseType => array());

            foreach ($unit as $u) {
                $data = $this->marshallUnit($u);
                $json['list'][$strBaseType][] = $data['base'][$strBaseType];
            }
        } elseif ($unit instanceof RecordContainer) {
            $json = array();
            $json['record'] = array();

            foreach ($unit as $k => $u) {
                $data = $this->marshallUnit($u);
                $jsonEntry = array();
                $jsonEntry['name'] = $k;

                if (array_key_exists('base', $data) === true) {
                    // Primitive base type.
                    $jsonEntry['base'] = $data['base'];
                } else {
                    // A nested list.
                    $jsonEntry['list'] = $data['list'];
                }

                $json['record'][] = $jsonEntry;
            }
        } else {
            $json = $this->marshallComplex($unit);
        }

        return $json;
    }
protected function marshallUnit($unit)
    {
        if (is_null($unit) === true) {
            $json = array('base' => null);
        } elseif ($unit instanceof QtiScalar) {
            $json = $this->marshallScalar($unit);
        } elseif ($unit instanceof MultipleContainer) {
            $json = array();
            $strBaseType = BaseType::getNameByConstant($unit->getBaseType());
            $json['list'] = array($strBaseType => array());

            foreach ($unit as $u) {
                $data = $this->marshallUnit($u);
                $json['list'][$strBaseType][] = $data['base'][$strBaseType];
            }
        } elseif ($unit instanceof RecordContainer) {
            $json = array();
            $json['record'] = array();

            foreach ($unit as $k => $u) {
                $data = $this->marshallUnit($u);
                $jsonEntry = array();
                $jsonEntry['name'] = $k;

                if (array_key_exists('base', $data) === true) {
                    // Primitive base type.
                    $jsonEntry['base'] = $data['base'];
                } else {
                    // A nested list.
                    $jsonEntry['list'] = $data['list'];
                }

                $json['record'][] = $jsonEntry;
            }
        } else {
            $json = $this->marshallComplex($unit);
        }

        return $json;
    }
protected function marshallUnit ( $ unit ) { if ( is_null ( $ unit ) === true ) { $ json = array ( 'base' => null ) ; } elseif ( $ unit instanceof QtiScalar ) { $ json = $ this -> marshallScalar ( $ unit ) ; } elseif ( $ unit instanceof MultipleContainer ) { $ json = array ( ) ; $ strBaseType = BaseType :: getNameByConstant ( $ unit -> getBaseType ( ) ) ; $ json [ 'list' ] = array ( $ strBaseType => array ( ) ) ; foreach ( $ unit as $ u ) { $ data = $ this -> marshallUnit ( $ u ) ; $ json [ 'list' ] [ $ strBaseType ] [ ] = $ data [ 'base' ] [ $ strBaseType ] ; } } elseif ( $ unit instanceof RecordContainer ) { $ json = array ( ) ; $ json [ 'record' ] = array ( ) ; foreach ( $ unit as $ k => $ u ) { $ data = $ this -> marshallUnit ( $ u ) ; $ jsonEntry = array ( ) ; $ jsonEntry [ 'name' ] = $ k ; if ( array_key_exists ( 'base' , $ data ) === true ) { // Primitive base type. $ jsonEntry [ 'base' ] = $ data [ 'base' ] ; } else { // A nested list. $ jsonEntry [ 'list' ] = $ data [ 'list' ] ; } $ json [ 'record' ] [ ] = $ jsonEntry ; } } else { $ json = $ this -> marshallComplex ( $ unit ) ; } return $ json ; }
Marshall a single unit of QTI data.

@param \qtism\runtime\common\State|\qtism\common\datatypes\QtiDatatype|null $unit
@return array An array representing the JSON data to be encoded later on.
Marshall a single unit of QTI data .