AbstractControllerMessage.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 
17 
18 /**
19  * AbstractControllerMessage is the base class for request/response
20  * implementations.
21  *
22  * @author ingo herwig <ingo@wemove.com>
23  */
24 abstract class AbstractControllerMessage implements ControllerMessage {
25 
26  /**
27  * The name of the controller from which the message origins.
28  */
29  private $_sender = null;
30 
31  /**
32  * The name of the context of the message.
33  */
34  private $_context = null;
35 
36  /**
37  * The name of the action that should be executed with this message.
38  */
39  private $_action = null;
40 
41  /**
42  * The format of the message.
43  */
44  private $_format = null;
45 
46  /**
47  * The formatter used for de-, serialization into the format.
48  */
49  private $_formatter = null;
50 
51  /**
52  * The message headers
53  */
54  private $_headers = array();
55 
56  /**
57  * Key value pairs of data contained in this message.
58  */
59  private $_values = array();
60 
61  /**
62  * Key value pairs of user defined properties contained in this message.
63  */
64  private $_properties = array();
65 
66  /**
67  * A list of errors associated with this message.
68  */
69  private $_errors = array();
70 
71  /**
72  * Constructor
73  * @param $formatter
74  */
75  public function __construct(Formatter $formatter) {
76  $this->_formatter = $formatter;
77  }
78 
79  /**
80  * @see ControllerMessage::setSender()
81  */
82  public function setSender($sender) {
83  $this->_sender = $sender;
84  }
85 
86  /**
87  * @see ControllerMessage::getSender()
88  */
89  public function getSender() {
90  return $this->_sender;
91  }
92 
93  /**
94  * @see ControllerMessage::setContext()
95  */
96  public function setContext($context) {
97  $this->_context = $context;
98  }
99 
100  /**
101  * @see ControllerMessage::getContext()
102  */
103  public function getContext() {
104  return $this->_context;
105  }
106 
107  /**
108  * @see ControllerMessage::setAction()
109  */
110  public function setAction($action) {
111  $this->_action = $action;
112  }
113 
114  /**
115  * @see ControllerMessage::getAction()
116  */
117  public function getAction() {
118  return $this->_action;
119  }
120 
121  /**
122  * @see ControllerMessage::setFormat()
123  */
124  public function setFormat($format) {
125  $this->_format = $format;
126  }
127 
128  /**
129  * @see ControllerMessage::getFormat()
130  */
131  public function getFormat() {
132  if ($this->_format == null) {
133  $this->_format = $this->_formatter->getFormatFromMimeType($this->getHeader('Content-Type'));
134  }
135  return $this->_format;
136  }
137 
138  /**
139  * @see ControllerMessage::setHeader()
140  */
141  public function setHeader($name, $value) {
142  $this->_headers[$name] = $value;
143  }
144 
145  /**
146  * @see ControllerMessage::setHeaders()
147  */
148  public function setHeaders(array $headers) {
149  $this->_headers = $headers;
150  }
151 
152  /**
153  * @see ControllerMessage::getHeader()
154  */
155  public function getHeader($name, $default=null) {
156  if ($this->hasHeader($name)) {
157  return $this->_headers[$name];
158  }
159  else {
160  return $default;
161  }
162  }
163 
164  /**
165  * @see ControllerMessage::getHeaders()
166  */
167  public function getHeaders() {
168  return $this->_headers;
169  }
170 
171  /**
172  * @see ControllerMessage::clearHeader()
173  */
174  public function clearHeader($name) {
175  unset($this->_headers[$name]);
176  }
177 
178  /**
179  * @see ControllerMessage::clearHeaders()
180  */
181  public function clearHeaders() {
182  $this->_headers = array();
183  }
184 
185  /**
186  * @see ControllerMessage::hasHeader()
187  */
188  public function hasHeader($name) {
189  return array_key_exists($name, $this->_headers);
190  }
191 
192  /**
193  * @see ControllerMessage::setValue()
194  */
195  public function setValue($name, $value) {
196  $this->_values[$name] = $value;
197  }
198 
199  /**
200  * @see ControllerMessage::setValues()
201  */
202  public function setValues(array $values) {
203  $this->_values = $values;
204  }
205 
206  /**
207  * @see ControllerMessage::getValue()
208  */
209  public function getValue($name, $default=null, $filter=null, $options=null) {
210  if ($this->hasValue($name)) {
211  $value = $this->_values[$name];
212  return ($filter != null) ? filter_var($value, $filter, $options) : $value;
213  }
214  else {
215  return $default;
216  }
217  }
218 
219  /**
220  * @see ControllerMessage::getBooleanValue()
221  */
222  public function getBooleanValue($name, $default=false) {
223  if ($this->hasValue($name)) {
224  return StringUtil::getBoolean($this->_values[$name]);
225  }
226  else {
227  return $default;
228  }
229  }
230 
231  /**
232  * @see ControllerMessage::getValues()
233  */
234  public function getValues() {
235  return $this->_values;
236  }
237 
238  /**
239  * @see ControllerMessage::clearValue()
240  */
241  public function clearValue($name) {
242  unset($this->_values[$name]);
243  }
244 
245  /**
246  * @see ControllerMessage::clearValues()
247  */
248  public function clearValues() {
249  $this->_values = array();
250  }
251 
252  /**
253  * @see ControllerMessage::hasValue()
254  */
255  public function hasValue($name) {
256  return array_key_exists($name, $this->_values);
257  }
258 
259  /**
260  * @see ControllerMessage::setProperty()
261  */
262  public function setProperty($name, $value) {
263  $this->_properties[$name] = $value;
264  }
265 
266  /**
267  * @see ControllerMessage::getProperty()
268  */
269  public function getProperty($name) {
270  if (isset($this->_properties[$name])) {
271  return $this->_properties[$name];
272  }
273  return null;
274  }
275 
276  /**
277  * @see ControllerMessage::addError()
278  */
279  public function addError(ApplicationError $error) {
280  $this->_errors[] = $error;
281  }
282 
283  /**
284  * @see ControllerMessage::setErrors()
285  */
286  public function setErrors(array $errors) {
287  $this->_errors = $errors;
288  }
289 
290  /**
291  * @see ControllerMessage::getErrors()
292  */
293  public function getErrors() {
294  return $this->_errors;
295  }
296 
297  /**
298  * @see ControllerMessage::clearErrors()
299  */
300  public function clearErrors() {
301  $this->_errors = array();
302  }
303 
304  /**
305  * @see ControllerMessage::hasErrors()
306  */
307  public function hasErrors() {
308  return sizeof($this->_errors) > 0;
309  }
310 
311  /**
312  * Get the Formatter instance
313  * @return Formatter
314  */
315  protected function getFormatter() {
316  return $this->_formatter;
317  }
318 
319  /**
320  * Get a string representation of the message
321  * @return The string
322  */
323  public function __toString() {
324  $str = 'sender='.$this->_sender.', ';
325  $str .= 'context='.$this->_context.', ';
326  $str .= 'action='.$this->_action.', ';
327  $str .= 'format='.$this->_format.', ';
328  $str .= 'values='.StringUtil::getDump($this->_values);
329  $str .= 'errors='.StringUtil::getDump($this->_errors);
330  return $str;
331  }
332 }
333 ?>
getValue($name, $default=null, $filter=null, $options=null)
static getBoolean($string)
Get the boolean value of a string.
Definition: StringUtil.php:303
Messages are sent between Controllers and are used to transfer data between them. ...
Formatter is the single entry point for request/response formatting.
Definition: Formatter.php:23
__toString()
Get a string representation of the message.
AbstractControllerMessage is the base class for request/response implementations. ...
ApplicationError is used to signal errors that occur while processing a request.