api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResponseUtils.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Tqdev\PhpCrudApi;
  3. use Psr\Http\Message\ResponseInterface;
  4. class ResponseUtils
  5. {
  6. public static function output(ResponseInterface $response)
  7. {
  8. $status = $response->getStatusCode();
  9. $headers = $response->getHeaders();
  10. $body = $response->getBody()->getContents();
  11. http_response_code($status);
  12. foreach ($headers as $key => $values) {
  13. foreach ($values as $value) {
  14. header("$key: $value");
  15. }
  16. }
  17. echo $body;
  18. }
  19. public static function addExceptionHeaders(ResponseInterface $response, \Throwable $e): ResponseInterface
  20. {
  21. $response = $response->withHeader('X-Exception-Name', get_class($e));
  22. $response = $response->withHeader('X-Exception-Message', preg_replace('|\n|', ' ', trim($e->getMessage())));
  23. $response = $response->withHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());
  24. return $response;
  25. }
  26. public static function toString(ResponseInterface $response): string
  27. {
  28. $status = $response->getStatusCode();
  29. $headers = $response->getHeaders();
  30. $body = $response->getBody()->getContents();
  31. $str = "$status\n";
  32. foreach ($headers as $key => $values) {
  33. foreach ($values as $value) {
  34. $str .= "$key: $value\n";
  35. }
  36. }
  37. if ($body !== '') {
  38. $str .= "\n";
  39. $str .= "$body\n";
  40. }
  41. return $str;
  42. }
  43. }