wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.ControllerMessage.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.ControllerMessage.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 /**
21  * Some constants describing the different message formats
22  */
23 define("MSG_FORMAT_HTML", "HTML");
24 define("MSG_FORMAT_JSON", "JSON");
25 define("MSG_FORMAT_SOAP", "SOAP");
26 
27 /**
28  * @class ControllerMessage
29  * @ingroup Presentation
30  * @brief ControllerMessages are sent between Controllers and are used to transfer data
31  * between them. ControllerMessages are dispatched by ActionMapper, which decides upon the
32  * message's controller, context and action parameter to which Controller it will
33  * be send.
34  *
35  * @author ingo herwig <ingo@wemove.com>
36  */
38 {
39  var $_sender = null;
40  var $_context = null;
41  var $_action = null;
42  var $_format = null;
43  var $_data = array();
44 
45  /**
46  * Constructor
47  * @param sender The name of the controller that sent the message
48  * @param context The name of the context of the message
49  * @param action The name of the action that the message initiates
50  * @param data An associative array containing the message variables
51  * together with their values.
52  */
53  function ControllerMessage($sender, $context, $action, $data)
54  {
55  if (func_num_args() != 4) {
56  WCMFException::throwEx("Message constructor called with wrong argument number: ", __FILE__, __LINE__);
57  }
58  $this->_sender = $sender;
59  $this->_context = $context;
60  $this->_action = $action;
61  $this->_data = $data;
62  }
63 
64  /**
65  * Set the name of the sending Controller
66  * @param sender The name of the Controller
67  */
68  function setSender($sender)
69  {
70  $this->_sender = $sender;
71  }
72 
73  /**
74  * Get the name of the sending Controller
75  * @return The name of the Controller
76  */
77  function getSender()
78  {
79  return $this->_sender;
80  }
81 
82  /**
83  * Set the name of the context
84  * @param context The name of the context
85  */
86  function setContext($context)
87  {
88  $this->_context = $context;
89  }
90 
91  /**
92  * Get the name of the context
93  * @return The name of the context
94  */
95  function getContext()
96  {
97  return $this->_context;
98  }
99 
100  /**
101  * Set the name of the action
102  * @param action The name of the action
103  */
104  function setAction($action)
105  {
106  $this->_action = $action;
107  }
108 
109  /**
110  * Get the name of the action
111  * @return The name of the action
112  */
113  function getAction()
114  {
115  return $this->_action;
116  }
117 
118  /**
119  * Set the message format
120  * @param format One of the MSG_FORMAT constants
121  */
122  function setFormat($format)
123  {
124  $this->_format = $format;
125  }
126 
127  /**
128  * Get the message format
129  * @return format One of the MSG_FORMAT constants
130  */
131  function getFormat()
132  {
133  return $this->_format;
134  }
135 
136  /**
137  * Set a value
138  * @param name The name of the variable
139  * @param value The value of the variable
140  */
141  function setValue($name, $value)
142  {
143  $this->_data[$name] = $value;
144  }
145 
146  /**
147  * Append a value
148  * @param name The name of the variable
149  * @param value The value to append to the variable
150  */
151  function appendValue($name, $value)
152  {
153  $this->_data[$name] .= $value;
154  }
155 
156  /**
157  * Check for existance of a value
158  * @param name The name of the variable
159  * @return True/False wether the value exists or not exist
160  */
161  function hasValue($name)
162  {
163  return array_key_exists($name, $this->_data);
164  }
165 
166  /**
167  * Get a value
168  * @param name The name of the variable
169  * @param default The default value if the value is not defined [default: null]
170  * @return The value or default, if it does not exist
171  */
172  function getValue($name, $default=null)
173  {
174  if (!$this->hasValue($name)) {
175  return $default;
176  }
177  else {
178  return $this->_data[$name];
179  }
180  }
181 
182  /**
183  * Get a value as boolean
184  * @param name The name of the variable
185  * @param default The default value if the value is not defined [default: false]
186  * @return The value or null if it does not exist
187  */
188  function getBooleanValue($name, $default=false)
189  {
190  if (!$this->hasValue($name)) {
191  return $default;
192  }
193  else {
194  return ($this->_data[$name] === true || strtolower($this->_data[$name]) === "true"
195  || intval($this->_data[$name]) === 1);
196  }
197  }
198 
199  /**
200  * Get all values as an associative array
201  * @return A reference to an data array
202  */
203  function &getData()
204  {
205  return $this->_data;
206  }
207 
208  /**
209  * Set all values at once
210  * @param data A reference to the data
211  */
212  function setData(&$data)
213  {
214  $this->_data = &$data;
215  }
216 
217  /**
218  * Remove a value
219  * @param name The name of the variable
220  */
221  function clearValue($name)
222  {
223  unset($this->_data[$name]);
224  }
225 
226  /**
227  * Remove all values
228  */
229  function clearValues()
230  {
231  $this->_data = array();
232  }
233 
234  /**
235  * Get a string repr�sentation of the message
236  * @return The string
237  */
238  function toString()
239  {
240  $str = 'sender='.$this->_sender.', ';
241  $str .= 'context='.$this->_context.', ';
242  $str .= 'action='.$this->_action.', ';
243  $str .= 'format='.$this->_format.', ';
244  $str .= 'data='.StringUtil::getDump($this->_data);
245  return $str;
246  }
247 }
248 ?>
throwEx($message, $file='', $line='')
ControllerMessages are sent between Controllers and are used to transfer data between them...
getBooleanValue($name, $default=false)
ControllerMessage($sender, $context, $action, $data)
getValue($name, $default=null)