AbstractFormat.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 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 is 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 = [];
30 
31  /**
32  * @see Format::deserialize()
33  */
34  public function deserialize(Request $request) {
35  $request->setValues($this->beforeDeserialize($request));
36  $request->setValues($this->deserializeValues($request));
37  $request->setValues($this->afterDeserialize($request));
38  }
39 
40  /**
41  * @see Format::serialize()
42  */
43  public function serialize(Response $response) {
44  $response->setValues($this->beforeSerialize($response));
45  $response->setValues($this->serializeValues($response));
46  $response->setValues($this->afterSerialize($response));
47  }
48 
49  /**
50  * @see Format::getResponseHeaders()
51  */
52  public function getResponseHeaders(Response $response) {
53  $response->setHeader("Content-Type", $this->getMimeType($response)."; charset=utf-8");
54  return $response->getHeaders();
55  }
56 
57  /**
58  * Modify data before deserialization. The default implementation does nothing.
59  * @param $request The request
60  * @return Array/object of values
61  * @note Subclasses override this if necessary
62  */
63  protected function beforeDeserialize(Request $request) {
64  return $request->getValues();
65  }
66 
67  /**
68  * Deserialize an array of values.
69  * @param $request The request
70  * @return Array/object of values
71  */
72  protected abstract function deserializeValues(Request $request);
73 
74  /**
75  * Modify data after deserialization. The default implementation does nothing.
76  * @param $request The request
77  * @return Array/object of values
78  * @note Subclasses override this if necessary
79  */
80  protected function afterDeserialize(Request $request) {
81  return $request->getValues();
82  }
83 
84  /**
85  * Modify data before serialization. The default implementation does nothing.
86  * @param $response The response
87  * @return Array/object of values
88  * @note Subclasses override this if necessary
89  */
90  protected function beforeSerialize(Response $response) {
91  return $response->getValues();
92  }
93 
94  /**
95  * Serialize an array of values.
96  * @param $response The response
97  * @return Array/object of values
98  */
99  protected abstract function serializeValues(Response $response);
100 
101  /**
102  * Modify data after serialization. The default implementation does nothing.
103  * @param $response The response
104  * @return Array/object of values
105  * @note Subclasses override this if necessary
106  */
107  protected function afterSerialize(Response $response) {
108  return $response->getValues();
109  }
110 
111  /**
112  * Helper methods
113  */
114 
115  /**
116  * Get a node with the given oid to deserialize values from form fields into it.
117  * The method ensures that there is only one instance per oid.
118  * @param $oid The oid
119  * @return Node
120  */
121  protected function getNode(ObjectId $oid) {
122  $oidStr = $oid->__toString();
123  if (!isset($this->deserializedNodes[$oidStr])) {
124  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
125  $type = $oid->getType();
126  if ($persistenceFacade->isKnownType($type)) {
127  // don't create all values by default (-> don't use PersistenceFacade::create() directly, just for determining the class)
128  $class = get_class($persistenceFacade->create($type, BuildDepth::SINGLE));
129  $node = new $class;
130  }
131  else {
132  $node = new Node($type);
133  }
134  $node->setOID($oid);
135  $this->deserializedNodes[$oidStr] = $node;
136  }
137  return $this->deserializedNodes[$oidStr];
138  }
139 
140  protected function filterValue($value, AttributeDescription $attribute) {
141  // TODO: implement filtering by attribute type
142  }
143 }
144 ?>
serializeValues(Response $response)
Serialize an array of values.
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
deserializeValues(Request $request)
Deserialize an array of values.
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:18
getValues()
Get all key value pairs.
beforeSerialize(Response $response)
Modify data before serialization.
__toString()
Get a string representation of the object id.
Definition: ObjectId.php:215
afterDeserialize(Request $request)
Modify data after deserialization.
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:28
BuildDepth values are used to define the depth when loading object trees.
Definition: BuildDepth.php:19
Instances of AttributeDescription describe attributes of PersistentObjects.
filterValue($value, AttributeDescription $attribute)
setValues(array $values)
Set all key value pairs at once.
getType()
Get the type (including namespace)
Definition: ObjectId.php:97
Format defines the interface for all format classes.
Definition: Format.php:25
getMimeType(Response $response=null)
Get the MIME type of the format.
afterSerialize(Response $response)
Modify data after serialization.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34
static getInstance($name, $dynamicConfiguration=[])
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
AbstractFormat is used as base class for specialized formats.
setHeader($name, $value)
Set a header value.
beforeDeserialize(Request $request)
Modify data before deserialization.