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