wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.NodeSerializer.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.NodeSerializer.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
20 require_once(BASE."wcmf/lib/model/class.Node.php");
21 require_once(BASE."wcmf/lib/model/class.NodeIterator.php");
22 require_once(BASE."wcmf/lib/model/class.NodeProcessor.php");
23 
24 /**
25  * @class NodeSerializer
26  * @ingroup Util
27  * @brief NodeSerializer provides helper functions to de-/serialize Nodes.
28  *
29  * @author ingo herwig <ingo@wemove.com>
30  */
32 {
33  /**
34  * Deserialize a Node from serialized data. Only values given in data are be set.
35  * @param type The type the data belong to
36  * @param data The serialized node data (either as object or as array)
37  * @param hasFlattendedValues True if all node data is serialized into one array, false if
38  * there is an extra array 'values', that holds the data types and inside these the values
39  * @param parent The parent node [default: null]
40  * @return A reference to the node deserialized from the data or null if the type does not exist
41  */
42  function &deserializeNode($type, $data, $hasFlattendedValues, $parent=null)
43  {
45  {
46  $persistenceFacade = &PersistenceFacade::getInstance();
47  $node = $persistenceFacade->create($type, BUILDEPTH_SINGLE);
48  // remove default values
49  $node->clearValues();
50 
51  if (!$hasFlattendedValues)
52  {
53  $valueData = array();
54  $properties = array();
55  $relatives = array();
56  foreach($data as $key => $value)
57  {
58  if ($key == 'values') {
59  $valueData = $value;
60  }
61  elseif ($key == 'properties') {
62  $properties = $value;
63  }
64  elseif ($key != 'oid' && $key != 'type') {
65  $relatives[$key] = $value;
66  }
67  }
68  foreach ($valueData as $dataType => $values)
69  {
70  foreach ($values as $key => $value) {
71  NodeSerializer::deserializeValue($node, $key, $value, $dataType, $hasFlattendedValues);
72  }
73  }
74  foreach ($properties as $key => $value) {
75  $node->setProperty($key, $value);
76  }
77  foreach ($relatives as $type => $objects)
78  {
79  foreach ($objects as $object) {
80  NodeSerializer::deserializeNode($type, $object, $hasFlattendedValues, $node);
81  }
82  }
83  }
84  else
85  {
86  // in case of not flattened values, the array only contains
87  // value names and values (no data types)
88  foreach($data as $key => $value)
89  NodeSerializer::deserializeValue($node, $key, $value, null, $hasFlattendedValues);
90  }
91 
92  if ($parent != null) {
93  $parent->addChild($node);
94  }
95  return $node;
96  }
97  else
98  return null;
99  }
100  /**
101  * Deserialize an node value
102  * @param node A reference to the node
103  * @param key The value name or type if value is an array
104  * @param value The value or child data, if value is an array
105  * @param dataType The dataType of the value
106  * @param hasFlattendedValues
107  */
108  function deserializeValue(&$node, $key, $value, $dataType, $hasFlattendedValues)
109  {
110  if (!is_array($value)) {
111  $node->setValue($key, $value, $dataType);
112  }
113  else
114  {
115  // deserialize children
116  foreach($value as $childData) {
117  NodeSerializer::deserializeNode($key, $childData, $hasFlattendedValues, $node);
118  }
119  }
120  }
121  /**
122  * Serialize a Node into an array
123  * @param obj A reference to the node to serialize
124  * @param flattenValues True if all node data should be serialized into one array, false if
125  * there should be an extra array 'values', that holds the data types and inside these the values
126  * @return The node serialized into an associated array
127  */
128  function serializeNode(&$obj, $flattenValues)
129  {
130  $result = array();
131  $rightsManager = &RightsManager::getInstance();
132 
133  $iter = new NodeIterator($obj);
134  while (!$iter->isEnd())
135  {
136  $curNode = &$iter->getCurrentObject();
137  $curResult = &NodeSerializer::getArray();;
138 
139  // use NodeProcessor to iterate over all Node values
140  // and call the global convert function on each
141  $values = &NodeSerializer::getArray();
142  $processor = new NodeProcessor('serializeAttribute', array(&$values, $flattenValues), new NodeSerializer());
143  $processor->run($curNode, false);
144 
145  if ($flattenValues) {
146  $curResult = $values;
147  }
148  else {
149  $curResult['values'] = $values;
150  }
151 
152  // add oid, type, parentoids, childoids
153  $curResult['oid'] = $curNode->getOID();
154  $curResult['type'] = $curNode->getType();
155  $curResult['properties'] = array();
156  foreach($curNode->getPropertyNames() as $name) {
157  $propertyValue = $curNode->getProperty($name);
158  if ($propertyValue instanceof Node) {
159  $propertyValue = NodeSerializer::serializeNode($propertyValue, $flattenValues);
160  }
161  $curResult['properties'][$name] = $propertyValue;
162  }
163  // add current result to result
164  $path = split('/', $curNode->getPath());
165  if (sizeof($path) == 1) {
166  $result = &$curResult;
167  }
168  else
169  {
170  array_shift($path);
171  $array = &NodeSerializer::getPathArray($result, $path, 0);
172  $array[sizeof($array)] = $curResult;
173  }
174  $iter->proceed();
175  }
176  return $result;
177  }
178  /**
179  * Callback function for NodeProcessor (see NodeProcessor).
180  */
181  function serializeAttribute(&$node, $valueName, $dataType, &$result, $flattenDataTypes)
182  {
183  if (!$flattenDataTypes)
184  {
185  if (!isset($result[$dataType]))
186  $result[$dataType] = array();
187  $result[$dataType][$valueName] = $node->getValue($valueName, $dataType);
188  }
189  else
190  $result[$valueName] = $node->getValue($valueName, $dataType);
191  }
192  /**
193  */
194  function &getArray()
195  {
196  return array();
197  }
198  /**
199  */
200  function &getPathArray(&$array, $path, $curDepth)
201  {
202  if (!isset($array[$path[$curDepth]]))
203  $array[$path[$curDepth]] = array();
204 
205  if ($curDepth < sizeof($path)-1)
206  return NodeSerializer::getPathArray($array[$path[$curDepth]][0], $path, ++$curDepth);
207  else
208  return $array[$path[$curDepth]];
209  }
210 }
211 ?>
& getPathArray(&$array, $path, $curDepth)
& deserializeNode($type, $data, $hasFlattendedValues, $parent=null)
Node is the basic component for building trees (although a Node can have one than more parents)...
Definition: class.Node.php:118
serializeAttribute(&$node, $valueName, $dataType, &$result, $flattenDataTypes)
NodeIterator is used to iterate over a tree/list build of objects using a Depth-First-Algorithm. Classes used with the NodeIterator must implement the getChildren() and getOID() methods. NodeIterator implements the 'Iterator Pattern'. The base class NodeIterator defines the interface for all specialized Iterator classes.
NodeProcessor is used to iterate over all values of a Node and apply a given callback function...
deserializeValue(&$node, $key, $value, $dataType, $hasFlattendedValues)
serializeNode(&$obj, $flattenValues)
NodeSerializer provides helper functions to de-/serialize Nodes.