GenericResult.php 2.6 KB

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