Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
JSONMessage.inc.php
1 <?php
2 
18 class JSONMessage {
20  var $_status;
21 
23  var $_content;
24 
26  var $_elementId;
27 
29  var $_event;
30 
33 
41  function JSONMessage($status = true, $content = '', $elementId = '0', $additionalAttributes = null) {
42  // Set internal state.
43  $this->setStatus($status);
44  $this->setContent($content);
45  $this->setElementId($elementId);
46  if (isset($additionalAttributes)) {
47  $this->setAdditionalAttributes($additionalAttributes);
48  }
49  }
50 
55  function getStatus () {
57  }
58 
63  function setStatus($status) {
64  assert(is_bool($status));
65  $this->_status = $status;
66  }
67 
72  function getContent() {
73  return $this->_content;
74  }
75 
80  function setContent($content) {
81  $this->_content = $content;
82  }
83 
88  function getElementId() {
89  return $this->_elementId;
90  }
91 
96  function setElementId($elementId) {
97  assert(is_string($elementId) || is_numeric($elementId));
98  $this->_elementId = $elementId;
99  }
100 
106  function setEvent($eventName, $eventData = null) {
107  assert(is_string($eventName));
108 
109  // Construct the even as an associative array.
110  $event = array('name' => $eventName);
111  if(!is_null($eventData)) $event['data'] = $eventData;
112 
113  $this->_event = $event;
114  }
115 
120  function getEvent() {
122  }
123 
128  function getAdditionalAttributes() {
130  }
131 
136  function setAdditionalAttributes($additionalAttributes) {
137  assert(is_array($additionalAttributes));
138  $this->_additionalAttributes = $additionalAttributes;
139  }
140 
145  function getString() {
146  // Construct an associative array that contains all information we require.
147  $jsonObject = array(
148  'status' => $this->getStatus(),
149  'content' => $this->getContent(),
150  'elementId' => $this->getElementId()
151  );
152  if(is_array($this->getAdditionalAttributes())) {
153  foreach($this->getAdditionalAttributes() as $key => $value) {
154  $jsonObject[$key] = $value;
155  }
156  }
157  if(is_array($this->getEvent())) {
158  $jsonObject['event'] = $this->getEvent();
159  }
161  // Encode the object.
162  return json_encode($jsonObject);
163  }
164 }
165 
166 ?>
setContent($content)
Class to represent a JSON (Javascript Object Notation) message.
JSONMessage($status=true, $content= '', $elementId= '0', $additionalAttributes=null)
setElementId($elementId)
setEvent($eventName, $eventData=null)
setAdditionalAttributes($additionalAttributes)
setStatus($status)