HierarchicalFormat.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  */
12 
16 
17 /**
18  * HierarchicalFormat maybe used as base class for formats that
19  * are able to represent hierarchical data like JSON or XML. This format
20  * automatically iterates over data when de-/serializing and uses template
21  * methods to implement the specific format.
22  *
23  * @author ingo herwig <ingo@wemove.com>
24  */
25 abstract class HierarchicalFormat extends AbstractFormat {
26 
27  /**
28  * @see AbstractFormat::deserializeValues()
29  */
30  protected function deserializeValues($values) {
31  if ($this->isSerializedNode($values)) {
32  // the values represent a node
33  $result = $this->deserializeNode($values);
34  $node = $result['node'];
35  $values = $result['data'];
36  $values[$node->getOID()->__toString()] = $node;
37  }
38  else {
39  foreach ($values as $key => $value) {
40  if (is_array($value) || is_object($value)) {
41  // array/object value
42  $result = $this->deserializeValues($value);
43  // flatten the array, if the deserialization result is only an array
44  // with size 1 and the key is an oid (e.g. if a node was deserialized)
45  if (is_array($result) && sizeof($result) == 1 && ObjectId::isValid(key($result))) {
46  unset($values[$key]);
47  $values[key($result)] = current($result);
48  }
49  else {
50  $values[$key] = $result;
51  }
52  }
53  else {
54  // string value
55  $values[$key] = $value;
56  }
57  }
58  }
59  return $values;
60  }
61 
62  /**
63  * @see AbstractFormat::serializeValues()
64  */
65  protected function serializeValues($values) {
66  if ($this->isDeserializedNode($values)) {
67  // the values represent a node
68  $values = $this->serializeNode($values);
69  }
70  else {
71  foreach ($values as $key => $value) {
72  if (is_array($value) || is_object($value)) {
73  // array/object value
74  $result = $this->serializeValues($value);
75  if (ObjectId::isValid($key)) {
76  $values = $result;
77  }
78  else {
79  $values[$key] = $result;
80  }
81  }
82  else {
83  // string value
84  $values[$key] = $value;
85  }
86  }
87  }
88  return $values;
89  }
90 
91  /**
92  * Determine if the value is a serialized Node. The default
93  * implementation returns false.
94  * @param $value The data value
95  * @return Boolean
96  * @note Subclasses override this if necessary
97  */
98  protected function isSerializedNode($value) {
99  return false;
100  }
101 
102  /**
103  * Determine if the value is a deserialized Node. The default
104  * implementation checks if the value is an object of type Node.
105  * @param $value The data value
106  * @return Boolean
107  * @note Subclasses override this if necessary
108  */
109  protected function isDeserializedNode($value) {
110  return ($value instanceof Node);
111  }
112 
113  /**
114  * Serialize a Node
115  * @param $value The data value
116  * @return The serialized Node
117  */
118  protected abstract function serializeNode($value);
119 
120  /**
121  * Deserialize a Node
122  * @param $value The data value
123  * @return An array with keys 'node' and 'data' where the node
124  * value is the Node instance and the data value is the
125  * remaining part of data, that is not used for deserializing the Node
126  */
127  protected abstract function deserializeNode($value);
128 }
129 ?>
isDeserializedNode($value)
Determine if the value is a deserialized Node.
AbstractFormat maybe used as base class for specialized formats.
HierarchicalFormat maybe used as base class for formats that are able to represent hierarchical data ...
static isValid($oid)
Check if a serialized ObjectId has a valid syntax, the type is known and if the number of primary key...
Definition: ObjectId.php:132
isSerializedNode($value)
Determine if the value is a serialized Node.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34