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