Ignore:
Timestamp:
03/04/12 15:48:42 (13 years ago)
Author:
hendrikvanantwerpen
Message:

[Server] Refactored model classes with some meta-programming. Specific classes only define their fields and inherit from class RdfObject?. Changes to handle the new model objects correctly.
[Client] Added rft/store module for uniform resource access. Removed dependencies on 'uid' field name. Added support for references without loading full object nor exposing uri.
[Client] Added reset() to QuestionWidget?.
[RDFAPI] Fixed PHP warning.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/rest-dojo-ui/server/classes/Marshaller.php

    r274 r303  
    44
    55    const OBJ_TYPE = '__objectType';
     6    const OBJ_UID = '__objectUid';
     7    const REF_TYPE = '__referenceType';
     8    const REF_UID = '__referenceUid';
    69
    710    private static function isWrappedObject($obj) {
     
    912    }
    1013
     14    private static function isWrappedReference($obj) {
     15        return isset($obj[self::REF_TYPE]);
     16    }
     17
    1118    public static function marshall($obj) {
    1219        switch (gettype($obj)) {
    1320            case 'array':
    14                 return array_map(array('Marshaller','marshall'), $obj);
     21                return array_map(array('Marshaller', 'marshall'), $obj);
    1522            case 'object':
    1623                return static::wrapObject($obj);
     
    2431        if ($objectType == 'DateTime') {
    2532            return array(self::OBJ_TYPE => 'DateTime', 'timestamp' => $obj->getTimestamp());
     33        } else if ($obj instanceof RdfObject) {
     34            $array = array(
     35                self::OBJ_TYPE => $obj->getType(),
     36                self::OBJ_UID => $obj->getUid()
     37            );
     38            foreach ($obj as $prop => $val) {
     39                $array[$prop] = static::marshall($val);
     40            }
     41            return $array;
     42        } else if ($obj instanceof RdfPointer) {
     43            return array(
     44                self::REF_TYPE => $obj->getType(),
     45                self::REF_UID => $obj->getUid()
     46            );
    2647        } else {
    27             $newObj = static::marshall((array) $obj);
    28             $newObj[self::OBJ_TYPE] = get_class($obj);
    29             return $newObj;
     48            return static::marshall((array) $obj);
    3049        }
    3150    }
     
    3857                if (static::isWrappedObject($obj)) {
    3958                    return static::unwrapObject($obj);
     59                } else if (static::isWrappedReference($obj)) {
     60                    return static::unwrapReference($obj);
    4061                } else {
    41                     return array_map(array('Marshaller','unmarshall'), $obj);
     62                    return array_map(array('Marshaller', 'unmarshall'), $obj);
    4263                }
    4364            default:
     
    4768
    4869    private static function unwrapObject($obj) {
    49         $objectType = $obj[self::OBJ_TYPE];
    50         unset($obj[self::OBJ_TYPE]);
     70        if (isset($obj[self::OBJ_TYPE])) {
     71            $objectType = $obj[self::OBJ_TYPE];
     72            unset($obj[self::OBJ_TYPE]);
     73        }
     74        if (isset($obj[self::OBJ_UID])) {
     75            $objectUid = $obj[self::OBJ_UID];
     76            unset($obj[self::OBJ_UID]);
     77        }
    5178        if ($objectType == 'DateTime') {
    5279            $date = new DateTime();
    5380            $date->setTimestamp($obj['timestamp']);
    5481            return $date;
    55         } else if (is_subclass_of($objectType, 'ResearchToolObjectInterface')) {
    56             return $objectType::create((object)static::unmarshall($obj));
     82        } else if (is_subclass_of($objectType, 'RdfObject')) {
     83            $rdfObject = new $objectType($objectUid);
     84            foreach ($obj as $prop => $val) {
     85                $rdfObject->$prop = static::unmarshall($val);
     86            }
     87            return $rdfObject;
    5788        } else {
    5889            return $obj;
     
    6091    }
    6192
     93    private static function unwrapReference($ref) {
     94        $refType = $ref[self::REF_TYPE];
     95        $refUid = $ref[self::REF_UID];
     96        return new RdfPointer(RdfObject::typeAndUidToResourceUri($refType, $refUid));
     97    }
     98   
    6299}
    63100
Note: See TracChangeset for help on using the changeset viewer.