source: Dev/branches/rest-dojo-ui/server/classes/Marshaller.php @ 303

Last change on this file since 303 was 303, checked in by hendrikvanantwerpen, 13 years ago

[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 size: 3.1 KB
RevLine 
[274]1<?php
2
3class Marshaller {
4
5    const OBJ_TYPE = '__objectType';
[303]6    const OBJ_UID = '__objectUid';
7    const REF_TYPE = '__referenceType';
8    const REF_UID = '__referenceUid';
[274]9
10    private static function isWrappedObject($obj) {
11        return isset($obj[self::OBJ_TYPE]);
12    }
13
[303]14    private static function isWrappedReference($obj) {
15        return isset($obj[self::REF_TYPE]);
16    }
17
[274]18    public static function marshall($obj) {
19        switch (gettype($obj)) {
20            case 'array':
[303]21                return array_map(array('Marshaller', 'marshall'), $obj);
[274]22            case 'object':
23                return static::wrapObject($obj);
24            default:
25                return $obj;
26        }
27    }
28
29    private static function wrapObject($obj) {
30        $objectType = get_class($obj);
31        if ($objectType == 'DateTime') {
32            return array(self::OBJ_TYPE => 'DateTime', 'timestamp' => $obj->getTimestamp());
[303]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            );
[274]47        } else {
[303]48            return static::marshall((array) $obj);
[274]49        }
50    }
51
52    public static function unmarshall($obj) {
53        switch (gettype($obj)) {
54            case 'object':
55                $obj = (array) $obj;
56            case 'array':
57                if (static::isWrappedObject($obj)) {
58                    return static::unwrapObject($obj);
[303]59                } else if (static::isWrappedReference($obj)) {
60                    return static::unwrapReference($obj);
[274]61                } else {
[303]62                    return array_map(array('Marshaller', 'unmarshall'), $obj);
[274]63                }
64            default:
65                return $obj;
66        }
67    }
68
69    private static function unwrapObject($obj) {
[303]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        }
[274]78        if ($objectType == 'DateTime') {
79            $date = new DateTime();
80            $date->setTimestamp($obj['timestamp']);
81            return $date;
[303]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;
[274]88        } else {
89            return $obj;
90        }
91    }
92
[303]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   
[274]99}
100
101?>
Note: See TracBrowser for help on using the repository browser.