Format.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Interface KO7_REST_Format
  4. * This interface needs to be extended from every REST-Formatter
  5. *
  6. * The following formatter come shipped with Koseven:
  7. * - JSON
  8. * - XML
  9. * - HTML
  10. *
  11. * @package KO7\REST
  12. *
  13. * @copyright (c) since 2016 Koseven Team
  14. * @license https://koseven.ga/LICENSE
  15. */
  16. abstract class KO7_REST_Format {
  17. /**
  18. * Default Output Format
  19. *
  20. * @var string
  21. */
  22. public static $default_format = 'json';
  23. /**
  24. * Holds an instance of the request class
  25. *
  26. * @var Request
  27. */
  28. protected $_request;
  29. /**
  30. * Holds an instance of the response class
  31. *
  32. * @var Response
  33. */
  34. protected $_response;
  35. /**
  36. * Holds the response body
  37. *
  38. * @var array|string
  39. */
  40. protected $_body;
  41. /**
  42. * Factory Method for REST Formatter
  43. *
  44. * @param Request $request Request Class
  45. * @param Response $response Response Class
  46. *
  47. * @throws REST_Exception
  48. *
  49. * @return REST_Format
  50. */
  51. public static function factory(Request $request, Response $response) : REST_Format
  52. {
  53. // Check if format is set by route, otherwise use default
  54. if ($request->format() === NULL)
  55. {
  56. $request->format(static::$default_format);
  57. }
  58. $formatter = 'REST_Format_'.$request->format();
  59. // Check if formatter Exists
  60. if ( ! class_exists($formatter))
  61. {
  62. throw new REST_Exception('Formatter :formatter does not exist.', [
  63. ':formatter' => get_class($formatter)
  64. ]);
  65. }
  66. $formatter = new $formatter($request, $response);
  67. // Check if client extends Request_Client_External
  68. if ( ! $formatter instanceof REST_Format)
  69. {
  70. throw new REST_Exception(':formatter is not a valid REST formatter.', [
  71. ':formatter' => get_class($formatter)
  72. ]);
  73. }
  74. // Set response content type by format used
  75. $response->headers('Content-Type', File::mime_by_ext($request->param('format')));
  76. return $formatter;
  77. }
  78. /**
  79. * KO7_REST_Format constructor.
  80. *
  81. * @param Request $request
  82. */
  83. public function __construct(Request $request, Response $response)
  84. {
  85. $this->_request = $request;
  86. $this->_response = $response;
  87. // Make sure body is an array
  88. $body = $response->body();
  89. if (is_string($body))
  90. {
  91. $body = [
  92. 'body' => $body
  93. ];
  94. }
  95. $this->_body = $body;
  96. }
  97. /**
  98. * Function for formatting the body
  99. *
  100. * @return string
  101. */
  102. abstract public function format() : string;
  103. }