DojoNodeSerializer.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\model\impl;
12 
22 
23 /**
24  * DojoNodeSerializer is used to serialize Nodes into the Dojo rest format and
25  * vice versa. The format of serialized Nodes is defined in the Dojo documentation (See:
26  * http://dojotoolkit.org/reference-guide/1.8/quickstart/rest.html)
27  *
28  * @author ingo herwig <ingo@wemove.com>
29  */
31 
32  private $_persistenceFacade = null;
33 
34  /**
35  * Constructor
36  * @param $persistenceFacade
37  */
38  public function __construct(PersistenceFacade $persistenceFacade) {
39  $this->_persistenceFacade = $persistenceFacade;
40  }
41 
42  /**
43  * @see NodeSerializer::isSerializedNode
44  */
45  public function isSerializedNode($data) {
46  if (is_object($data)) {
47  $data = (array)$data;
48  }
49  $syntaxOk = (is_array($data) && (isset($data['oid'])));
50  // check for oid variables
51  if ($syntaxOk && isset($data['oid']) && preg_match('/^\{.+\}$/', $data['oid'])) {
52  $syntaxOk = false;
53  }
54  return $syntaxOk;
55  }
56 
57  /**
58  * @see NodeSerializer::deserializeNode
59  */
60  public function deserializeNode($data, Node $parent=null, $role=null) {
61  if (!isset($data['oid'])) {
62  throw new IllegalArgumentException("Serialized Node data must contain an 'oid' parameter");
63  }
64  $oid = ObjectId::parse($data['oid']);
65  if ($oid == null) {
66  throw new IllegalArgumentException("The object id '".$oid."' is invalid");
67  }
68 
69  // don't create all values by default (-> don't use PersistenceFacade::create() directly,
70  // just for determining the class)
71  $class = get_class($this->_persistenceFacade->create($oid->getType(), BuildDepth::SINGLE));
72  $node = new $class;
73 
74  $remainingData = array();
75 
76  $mapper = $node->getMapper();
77  foreach($data as $key => $value) {
78  if ($mapper->hasAttribute($key)) {
79  $this->deserializeValue($node, $key, $value);
80  }
81  else {
82  $remainingData[$key] = $value;
83  }
84  }
85 
86  // set oid after attributes in order to
87  // avoid it being changed from missing pk values
88  $node->setOID($oid);
89 
90  // create hierarchy
91  if ($parent != null) {
92  $parent->addNode($node, $role);
93  }
94 
95  return array('node' => $node, 'data' => $remainingData);
96  }
97 
98  /**
99  * @see NodeSerializer::serializeNode
100  */
101  public function serializeNode($node, $rolesToRefOnly=array()) {
102  if (!($node instanceof Node)) {
103  return null;
104  }
105  $curResult = array();
106  $curResult['oid'] = $node->getOID()->__toString();
107 
108  // serialize attributes
109  // use NodeValueIterator to iterate over all Node values
110  $valueIter = new NodeValueIterator($node, false);
111  foreach($valueIter as $valueName => $value) {
112  $curResult[$valueName] = $value;
113  }
114 
115  // add related objects by creating an attribute that is named as the role of the object
116  // multivalued relations will be serialized into an array
117  $mapper = $node->getMapper();
118  foreach ($mapper->getRelations() as $relation) {
119  $role = $relation->getOtherRole();
120  $relatedNodes = $node->getValue($role);
121  if ($relatedNodes) {
122  // serialize the nodes
123  $isMultiValued = $relation->isMultiValued();
124  if ($isMultiValued) {
125  $curResult[$role] = array();
126  foreach ($relatedNodes as $relatedNode) {
127  if ($relatedNode instanceof PersistentObjectProxy || in_array($role, $rolesToRefOnly)) {
128  // add the reference to the relation attribute
129  $curResult[$role][] = array('$ref' => $relatedNode->getOID()->__toString());
130  }
131  else {
132  $curResult[$role][] = $this->serializeNode($relatedNode, array($relation->getThisRole()));
133  }
134  }
135  }
136  else {
137  $relatedNode = $relatedNodes;
138  if ($relatedNode instanceof PersistentObjectProxy || in_array($role, $rolesToRefOnly)) {
139  // add the reference to the relation attribute
140  $curResult[$role] = array('$ref' => $relatedNode->getOID()->__toString());
141  }
142  else {
143  $curResult[$role] = $this->serializeNode($relatedNode, array($relation->getThisRole()));
144  }
145  }
146  }
147  }
148  return $curResult;
149  }
150 }
151 ?>
IllegalArgumentException signals an exception in method arguments.
NodeValueIterator is used to iterate over all persistent values of a Node (not including relations)...
deserializeValue(Node $node, $key, $value)
Deserialize a node value.
__construct(PersistenceFacade $persistenceFacade)
Constructor.
deserializeNode($data, Node $parent=null, $role=null)
serializeNode($node, $rolesToRefOnly=array())
PersistentObjectProxy is proxy for an PersistentObject instance.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:144
DojoNodeSerializer is used to serialize Nodes into the Dojo rest format and vice versa.
NodeSerializerBase is a base class for NodeSerialize implementations.
PersistenceFacade defines the interface for PersistenceFacade implementations.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34