Response.php 681 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /**
  3. * examples:
  4. * Response::sendJson($data);
  5. * Response::sendPlainText($data);
  6. * Response::sendJsonExit($data);
  7. * Response::sendPlainTextExit($data);
  8. */
  9. class Response {
  10. public static function sendJson($data) {
  11. header("Content-type: application/json");
  12. echo json_encode($data);
  13. }
  14. public static function sendPlainText($data) {
  15. header("Content-type: text/plain");
  16. if (!is_scalar($data)) throw new Exception("Wrong data type - only scalar values allowed");
  17. echo $data;
  18. }
  19. public static function sendJsonExit($data) {
  20. self::sendJson($data);
  21. exit;
  22. }
  23. public static function sendPlainTextExit($data) {
  24. self::sendPlainText($data);
  25. exit;
  26. }
  27. }