AbstractFormat.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 
21 
22 /**
23  * AbstractFormat maybe used as base class for specialized formats.
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
27 abstract class AbstractFormat implements Format {
28 
29  private $_deserializedNodes = array();
30  private $_request = null;
31  private $_response = null;
32 
33  /**
34  * @see Format::deserialize()
35  */
36  public function deserialize(Request $request) {
37  $this->_request = $request;
38  $values = $request->getValues();
39  $values = $this->beforeDeserialize($values);
40  $values = $this->deserializeValues($values);
41  $values = $this->afterDeserialize($values);
42  $request->setValues($values);
43  }
44 
45  /**
46  * @see Format::serialize()
47  */
48  public function serialize(Response $response) {
49  $this->_response = $response;
50  $values = $response->getValues();
51  $values = $this->beforeSerialize($values);
52  $values = $this->serializeValues($values);
53  $values = $this->afterSerialize($values);
54  $response->setValues($values);
55  }
56 
57  /**
58  * Get the currently deserialized request
59  * @return Request
60  */
61  protected function getRequest() {
62  return $this->_request;
63  }
64 
65  /**
66  * Get the currently deserialized response
67  * @return Response
68  */
69  protected function getResponse() {
70  return $this->_response;
71  }
72 
73  /**
74  * Modify data before deserialization. The default implementation does nothing.
75  * @param $values The request values
76  * @return The modified values array
77  * @note Subclasses override this if necessary
78  */
79  protected function beforeDeserialize($values) {
80  return $values;
81  }
82 
83  /**
84  * Deserialize an array of values.
85  * @param $values The array/object of values
86  * @return The array/object of values
87  */
88  protected abstract function deserializeValues($values);
89 
90  /**
91  * Modify data after deserialization. The default implementation does nothing.
92  * @param $values The request values
93  * @return The modified values array
94  * @note Subclasses override this if necessary
95  */
96  protected function afterDeserialize($values) {
97  return $values;
98  }
99 
100  /**
101  * Modify data before serialization. The default implementation does nothing.
102  * @param $values The response values
103  * @return The modified values array
104  * @note Subclasses override this if necessary
105  */
106  protected function beforeSerialize($values) {
107  return $values;
108  }
109 
110  /**
111  * Serialize an array of values.
112  * @param $values The array/object of values
113  * @return The array/object of values
114  */
115  protected abstract function serializeValues($values);
116 
117  /**
118  * Modify data after serialization. The default implementation does nothing.
119  * @param $values The response values
120  * @return The modified values array
121  * @note Subclasses override this if necessary
122  */
123  protected function afterSerialize($values) {
124  return $values;
125  }
126 
127  /**
128  * Helper methods
129  */
130 
131  /**
132  * Get a node with the given oid to deserialize values from form fields into it.
133  * The method ensures that there is only one instance per oid.
134  * @param $oid The oid
135  * @return A reference to the Node instance
136  */
137  protected function getNode(ObjectId $oid) {
138  $oidStr = $oid->__toString();
139  if (!isset($this->_deserializedNodes[$oidStr])) {
140  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
141  $type = $oid->getType();
142  if ($persistenceFacade->isKnownType($type)) {
143  // don't create all values by default (-> don't use PersistenceFacade::create() directly, just for determining the class)
144  $class = get_class($persistenceFacade->create($type, BuildDepth::SINGLE));
145  $node = new $class;
146  }
147  else {
148  $node = new Node($type);
149  }
150  $node->setOID($oid);
151  $this->_deserializedNodes[$oidStr] = $node;
152  }
153  return $this->_deserializedNodes[$oidStr];
154  }
155 
156  protected function filterValue($value, AttributeDescription $attribute) {
157  // TODO: implement filtering by attribute type
158  }
159 }
160 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
AbstractFormat maybe used as base class for specialized formats.
afterDeserialize($values)
Modify data after deserialization.
setValues(array $values)
Set all key value pairs at once.
beforeDeserialize($values)
Modify data before deserialization.
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:27
static getInstance($name, $dynamicConfiguration=array())
getRequest()
Get the currently deserialized request.
afterSerialize($values)
Modify data after serialization.
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:20
beforeSerialize($values)
Modify data before serialization.
getResponse()
Get the currently deserialized response.
__toString()
Get a string representation of the object id.
Definition: ObjectId.php:204
getValues()
Get all key value pairs.
filterValue($value, AttributeDescription $attribute)
Instances of AttributeDescription describe attributes of PersistentObjects.
Format defines the interface for all format classes.
Definition: Format.php:24
getType()
Get the type (including namespace)
Definition: ObjectId.php:106
serializeValues($values)
Serialize an array of values.
deserializeValues($values)
Deserialize an array of values.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34