wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.HierarchicalFormat.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.HierarchicalFormat.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/presentation/format/class.AbstractFormat.php");
20 
21 /**
22  * @class HierarchicalFormat
23  * @ingroup Format
24  * @brief HierarchicalFormat maybe used as base class for formats that
25  * are able to represent hierarchical data like JSON or XML. This format
26  * automatically iterates over data when de-/serializing and uses template
27  * methods to implement the specific format.
28  *
29  * @author ingo herwig <ingo@wemove.com>
30  */
32 {
33  /**
34  * @see IFormat::deserialize()
35  */
36  function deserialize(&$request)
37  {
38  $data = &$request->getData();
39 
40  // deserialize Nodes
41  $this->beforeDeserialize($data);
42  ArrayUtil::array_walk_recursive($data, array($this, 'processValues'), 'deserializeNode');
43  $this->afterDeserialize($data);
44  }
45  /**
46  * @see IFormat::serialize()
47  */
48  function serialize(&$response)
49  {
50  $data = &$response->getData();
51 
52  // serialize Nodes
53  $this->beforeSerialize($data);
54  ArrayUtil::array_walk_recursive($data, array($this, 'processValues'), 'serializeNode');
55  $this->afterSerialize($data);
56  }
57  /**
58  * Callback function for array_walk_recursive. De-/Serializes any Node instances
59  * using the function given in method parameter.
60  * @param value The array value
61  * @param key The array key
62  * @param method The method to apply to each value
63  */
64  function processValues(&$value, $key, $method)
65  {
66  if (is_string($value) && EncodingUtil::isUtf8($value)) {
67  $value = EncodingUtil::convertCp1252Utf8ToIso($value);
68  }
69  if ( (strpos($method, 'deserialize') === 0 && $this->isSerializedNode($key, $value)) ||
70  (strpos($method, 'serialize') === 0 && $this->isDeserializedNode($key, $value)) )
71  {
72  $value = $this->$method($key, $value);
73  }
74  }
75 
76  /**
77  * Template methods
78  */
79 
80  /**
81  * Modify data before deserialization. The default implementation does nothing.
82  * @param data A reference to the data array
83  * @note Subclasses override this if necessary
84  */
85  function beforeDeserialize(&$data) {}
86  /**
87  * Modify data after deserialization. The default implementation does nothing.
88  * @param data A reference to the data array
89  * @note Subclasses override this if necessary
90  */
91  function afterDeserialize(&$data) {}
92 
93  /**
94  * Modify data before serialization. The default implementation does nothing.
95  * @param data A reference to the data array
96  * @note Subclasses override this if necessary
97  */
98  function beforeSerialize(&$data) {}
99  /**
100  * Modify data after serialization. The default implementation does nothing.
101  * @param data A reference to the data array
102  * @note Subclasses override this if necessary
103  */
104  function afterSerialize(&$data) {}
105 
106  /**
107  * Determine if the value is a serialized Node. The default
108  * implementation returns false.
109  * @param key The data key
110  * @param value A reference to the data value
111  * @return True/False
112  * @note Subclasses override this if necessary
113  */
114  function isSerializedNode($key, &$value)
115  {
116  return false;
117  }
118  /**
119  * Determine if the value is a deserialized Node. The default
120  * implementation checks if the value is an object of type Node.
121  * @param key The data key
122  * @param value A reference to the data value
123  * @return True/False
124  * @note Subclasses override this if necessary
125  */
126  function isDeserializedNode($key, &$value)
127  {
128  return (is_a($value, 'node') || is_a($value, 'Node'));
129  }
130 
131  /**
132  * Serialize a Node
133  * @param key The data key
134  * @param value A reference to the data value
135  * @return The serialized Node
136  */
137  function serializeNode($key, &$value)
138  {
139  WCMFException::throwEx("serializeNode() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
140  }
141  /**
142  * Deserialize a Node
143  * @param key The data key
144  * @param value A reference to the data value
145  * @return The deserialized Node
146  */
147  function &deserializeNode($key, &$value)
148  {
149  WCMFException::throwEx("deserializeNode() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
150  }
151 }
152 ?>
processValues(&$value, $key, $method)
static isUtf8($string)
static convertCp1252Utf8ToIso($str)
& deserializeNode($key, &$value)
AbstractFormat maybe used as base class for specialized formats.
array_walk_recursive(&$input, $funcname, $userdata="")
HierarchicalFormat maybe used as base class for formats that are able to represent hierarchical data ...
throwEx($message, $file='', $line='')