StompHeadersEncoder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. namespace Enqueue\Stomp;
  4. class StompHeadersEncoder
  5. {
  6. const PROPERTY_PREFIX = '_property_';
  7. const TYPE_PREFIX = '_type_';
  8. const TYPE_STRING = 's';
  9. const TYPE_INT = 'i';
  10. const TYPE_FLOAT = 'f';
  11. const TYPE_BOOL = 'b';
  12. const TYPE_NULL = 'n';
  13. public static function encode(array $headers = [], array $properties = []): array
  14. {
  15. $encodedHeaders = self::doEncode($headers);
  16. foreach (self::doEncode($properties) as $key => $value) {
  17. $encodedHeaders[self::PROPERTY_PREFIX.$key] = $value;
  18. }
  19. return $encodedHeaders;
  20. }
  21. /**
  22. * Returns array [[headers], [properties]].
  23. */
  24. public static function decode(array $headers = []): array
  25. {
  26. $encodedHeaders = [];
  27. $encodedProperties = [];
  28. $prefixLength = strlen(self::PROPERTY_PREFIX);
  29. // separate headers/properties
  30. foreach ($headers as $key => $value) {
  31. if (0 === strpos($key, self::PROPERTY_PREFIX)) {
  32. $encodedProperties[substr($key, $prefixLength)] = $value;
  33. } else {
  34. $encodedHeaders[$key] = $value;
  35. }
  36. }
  37. $decodedHeaders = self::doDecode($encodedHeaders);
  38. $decodedProperties = self::doDecode($encodedProperties);
  39. return [$decodedHeaders, $decodedProperties];
  40. }
  41. private static function doEncode(array $headers = []): array
  42. {
  43. $encoded = [];
  44. foreach ($headers as $key => $value) {
  45. switch ($type = gettype($value)) {
  46. case 'string':
  47. $encoded[$key] = (string) $value;
  48. $encoded[self::TYPE_PREFIX.$key] = self::TYPE_STRING;
  49. break;
  50. case 'integer':
  51. $encoded[$key] = (string) $value;
  52. $encoded[self::TYPE_PREFIX.$key] = self::TYPE_INT;
  53. break;
  54. case 'double':
  55. $encoded[$key] = (string) $value;
  56. $encoded[self::TYPE_PREFIX.$key] = self::TYPE_FLOAT;
  57. break;
  58. case 'NULL':
  59. $encoded[$key] = '';
  60. $encoded[self::TYPE_PREFIX.$key] = self::TYPE_NULL;
  61. break;
  62. case 'boolean':
  63. $encoded[$key] = $value ? 'true' : 'false';
  64. $encoded[self::TYPE_PREFIX.$key] = self::TYPE_BOOL;
  65. break;
  66. default:
  67. throw new \LogicException(sprintf('Value type is not valid: "%s"', $type));
  68. }
  69. }
  70. return $encoded;
  71. }
  72. private static function doDecode(array $headers = []): array
  73. {
  74. $decoded = [];
  75. foreach ($headers as $key => $value) {
  76. // skip type header
  77. if (0 === strpos($key, self::TYPE_PREFIX)) {
  78. continue;
  79. }
  80. // copy value as is if here is no type header
  81. if (false == array_key_exists(self::TYPE_PREFIX.$key, $headers)) {
  82. $decoded[$key] = $value;
  83. continue;
  84. }
  85. switch ($headers[self::TYPE_PREFIX.$key]) {
  86. case self::TYPE_STRING:
  87. $decoded[$key] = (string) $value;
  88. break;
  89. case self::TYPE_INT:
  90. $decoded[$key] = (int) $value;
  91. break;
  92. case self::TYPE_FLOAT:
  93. $decoded[$key] = (float) $value;
  94. break;
  95. case self::TYPE_NULL:
  96. $decoded[$key] = null;
  97. break;
  98. case self::TYPE_BOOL:
  99. $decoded[$key] = 'true' === $value;
  100. break;
  101. default:
  102. throw new \LogicException(sprintf('Type is invalid: "%s"', $value));
  103. }
  104. }
  105. return $decoded;
  106. }
  107. }