Compose.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Luracast\Restler;
  3. /**
  4. * Default Composer to provide standard structure for all HTTP responses
  5. *
  6. * @category Framework
  7. * @package Restler
  8. * @subpackage result
  9. * @author R.Arul Kumaran <arul@luracast.com>
  10. * @copyright 2010 Luracast
  11. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  12. * @link http://luracast.com/products/restler/
  13. *
  14. */
  15. class Compose implements iCompose
  16. {
  17. /**
  18. * @var bool When restler is not running in production mode, this value will
  19. * be checked to include the debug information on error response
  20. */
  21. public static $includeDebugInfo = true;
  22. /**
  23. * Current Restler instance
  24. * Injected at runtime
  25. *
  26. * @var Restler
  27. */
  28. public $restler;
  29. /**
  30. * Result of an api call is passed to this method
  31. * to create a standard structure for the data
  32. *
  33. * @param mixed $result can be a primitive or array or object
  34. *
  35. * @return mixed
  36. */
  37. public function response($result)
  38. {
  39. //TODO: check Defaults::language and change result accordingly
  40. return $result;
  41. }
  42. /**
  43. * When the api call results in RestException this method
  44. * will be called to return the error message
  45. *
  46. * @param RestException $exception exception that has reasons for failure
  47. *
  48. * @return array
  49. */
  50. public function message(RestException $exception)
  51. {
  52. //TODO: check Defaults::language and change result accordingly
  53. $r = array(
  54. 'error' => array(
  55. 'code' => $exception->getCode(),
  56. 'message' => $exception->getErrorMessage(),
  57. ) + $exception->getDetails()
  58. );
  59. if (!Scope::get('Restler')->getProductionMode() && self::$includeDebugInfo) {
  60. $r += array(
  61. 'debug' => array(
  62. 'source' => $exception->getSource(),
  63. 'stages' => $exception->getStages(),
  64. )
  65. );
  66. }
  67. return $r;
  68. }
  69. }