1: <?php
2: /**
3: * GenericResult Result superclass.
4: *
5: * @package Elgg.Core
6: * @subpackage WebServicesAPI
7: */
8: abstract class GenericResult {
9: /**
10: * The status of the result.
11: * @var int
12: */
13: private $status_code;
14:
15: /**
16: * Message returned along with the status which is almost always an error message.
17: * This must be human readable, understandable and localised.
18: * @var string
19: */
20: private $message;
21:
22: /**
23: * Result store.
24: * Attach result specific informaton here.
25: *
26: * @var mixed. Should probably be an object of some sort.
27: */
28: private $result;
29:
30: /**
31: * Set a status code and optional message.
32: *
33: * @param int $status The status code.
34: * @param string $message The message.
35: *
36: * @return void
37: */
38: protected function setStatusCode($status, $message = "") {
39: $this->status_code = $status;
40: $this->message = $message;
41: }
42:
43: /**
44: * Set the result.
45: *
46: * @param mixed $result The result
47: *
48: * @return void
49: */
50: protected function setResult($result) {
51: $this->result = $result;
52: }
53:
54: /**
55: * Return the current status code
56: *
57: * @return string
58: */
59: protected function getStatusCode() {
60: return $this->status_code;
61: }
62:
63: /**
64: * Return the current status message
65: *
66: * @return string
67: */
68: protected function getStatusMessage() {
69: return $this->message;
70: }
71:
72: /**
73: * Return the current result
74: *
75: * @return string
76: */
77: protected function getResult() {
78: return $this->result;
79: }
80:
81: /**
82: * Serialise to a standard class.
83: *
84: * DEVNOTE: The API is only interested in data, we can not easily serialise
85: * custom classes without the need for 1) the other side being PHP, 2) you need to have the class
86: * definition installed, 3) its the right version!
87: *
88: * Therefore, I'm not bothering.
89: *
90: * Override this to include any more specific information, however api results
91: * should be attached to the class using setResult().
92: *
93: * if $CONFIG->debug is set then additional information about the runtime environment and
94: * authentication will be returned.
95: *
96: * @return stdClass Object containing the serialised result.
97: */
98: public function export() {
99: global $ERRORS, $CONFIG, $_PAM_HANDLERS_MSG;
100:
101: $result = new stdClass;
102:
103: $result->status = $this->getStatusCode();
104: if ($this->getStatusMessage() != "") {
105: $result->message = $this->getStatusMessage();
106: }
107:
108: $resultdata = $this->getResult();
109: if (isset($resultdata)) {
110: $result->result = $resultdata;
111: }
112:
113: if (isset($CONFIG->debug)) {
114: if (count($ERRORS)) {
115: $result->runtime_errors = $ERRORS;
116: }
117:
118: if (count($_PAM_HANDLERS_MSG)) {
119: $result->pam = $_PAM_HANDLERS_MSG;
120: }
121: }
122:
123: return $result;
124: }
125: }
126: