forked from php-enqueue/enqueue-dev
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStompHeadersEncoder.php
More file actions
152 lines (124 loc) · 4.11 KB
/
StompHeadersEncoder.php
File metadata and controls
152 lines (124 loc) · 4.11 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Enqueue\Stomp;
class StompHeadersEncoder
{
const PROPERTY_PREFIX = '_property_';
const TYPE_PREFIX = '_type_';
const TYPE_STRING = 's';
const TYPE_INT = 'i';
const TYPE_FLOAT = 'f';
const TYPE_BOOL = 'b';
const TYPE_NULL = 'n';
/**
* @param array $headers
* @param array $properties
*
* @return array
*/
public static function encode(array $headers = [], array $properties = [])
{
$encodedHeaders = self::doEncode($headers);
foreach (self::doEncode($properties) as $key => $value) {
$encodedHeaders[self::PROPERTY_PREFIX.$key] = $value;
}
return $encodedHeaders;
}
/**
* @param array $headers
*
* @return array [[headers], [properties]]
*/
public static function decode(array $headers = [])
{
$encodedHeaders = [];
$encodedProperties = [];
$prefixLength = strlen(self::PROPERTY_PREFIX);
// separate headers/properties
foreach ($headers as $key => $value) {
if (0 === strpos($key, self::PROPERTY_PREFIX)) {
$encodedProperties[substr($key, $prefixLength)] = $value;
} else {
$encodedHeaders[$key] = $value;
}
}
$decodedHeaders = self::doDecode($encodedHeaders);
$decodedProperties = self::doDecode($encodedProperties);
return [$decodedHeaders, $decodedProperties];
}
/**
* @param array $headers
*
* @return array
*/
private static function doEncode($headers = [])
{
$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;
}
/**
* @param array $headers
*
* @return array
*/
private static function doDecode(array $headers = [])
{
$decoded = [];
foreach ($headers as $key => $value) {
// skip type header
if (0 === strpos($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;
}
}