HeaderConverter.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Buzz\Converter;
  3. /**
  4. * Convert between Buzz style:
  5. * array(
  6. * 'foo: bar',
  7. * 'baz: biz',
  8. * )
  9. *
  10. * and PSR style:
  11. * array(
  12. * 'foo' => 'bar'
  13. * 'baz' => ['biz', 'buz'],
  14. * )
  15. *
  16. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  17. */
  18. class HeaderConverter
  19. {
  20. /**
  21. * Convert from Buzz style headers to PSR style
  22. * @param array $headers
  23. *
  24. * @return array
  25. */
  26. public static function toBuzzHeaders(array $headers)
  27. {
  28. $buzz = [];
  29. foreach ($headers as $key => $values) {
  30. if (!is_array($values)) {
  31. $buzz[] = sprintf('%s: %s', $key, $values);
  32. } else {
  33. foreach ($values as $value) {
  34. $buzz[] = sprintf('%s: %s', $key, $value);
  35. }
  36. }
  37. }
  38. return $buzz;
  39. }
  40. /**
  41. * Convert from PSR style headers to Buzz style
  42. * @param array $headers
  43. * @return array
  44. */
  45. public static function toPsrHeaders(array $headers)
  46. {
  47. $psr = [];
  48. foreach ($headers as $header) {
  49. list($key, $value) = explode(':', $header, 2);
  50. $psr[trim($key)][] = trim($value);
  51. }
  52. return $psr;
  53. }
  54. }