|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ekho\Logstash\Lumberjack; |
|
3
|
|
|
|
|
4
|
|
|
class Encoder implements EncoderInterface |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* @param array $hash |
|
8
|
|
|
* @param int $sequence |
|
9
|
|
|
* @return string |
|
10
|
|
|
*/ |
|
11
|
1 |
|
public function toCompressedFrame($hash, $sequence) |
|
12
|
|
|
{ |
|
13
|
1 |
|
$frame = $this->toFrame($hash, $sequence); |
|
14
|
1 |
|
$compressedFrame = gzcompress($frame); |
|
15
|
1 |
|
return pack("AANA".strlen($compressedFrame), "1", "C", strlen($compressedFrame), $compressedFrame); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $hash |
|
20
|
|
|
* @param int $sequence |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function toFrame($hash, $sequence) |
|
24
|
|
|
{ |
|
25
|
1 |
|
$frame = array("1", "D", $sequence); |
|
26
|
1 |
|
$pack = "AAN"; |
|
27
|
1 |
|
$keys = $this->deepKeys($hash); |
|
28
|
1 |
|
array_push($frame, count($keys)); |
|
29
|
1 |
|
$pack .= "N"; |
|
30
|
1 |
|
foreach ($keys as $k) { |
|
31
|
1 |
|
$val = $this->deepGet($hash, $k); |
|
32
|
1 |
|
$keyLength = strlen($k); |
|
33
|
1 |
|
$valLength = strlen($val); |
|
34
|
1 |
|
array_push($frame, $keyLength); |
|
35
|
1 |
|
$pack .= "N"; |
|
36
|
1 |
|
array_push($frame, $k); |
|
37
|
1 |
|
$pack .= "A{$keyLength}"; |
|
38
|
1 |
|
array_push($frame, $valLength); |
|
39
|
1 |
|
$pack .= "N"; |
|
40
|
1 |
|
array_push($frame, $val); |
|
41
|
1 |
|
$pack .= "A{$valLength}"; |
|
42
|
|
|
} |
|
43
|
1 |
|
array_unshift($frame, $pack); |
|
44
|
1 |
|
return call_user_func_array('pack', $frame); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $hash |
|
49
|
|
|
* @param string $key |
|
50
|
|
|
* @return null|string |
|
51
|
|
|
*/ |
|
52
|
1 |
|
private function deepGet($hash, $key) |
|
53
|
|
|
{ |
|
54
|
1 |
|
if ($key === null) { |
|
55
|
1 |
|
return $this->stringifyValue($hash); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
if (is_array($hash) && array_key_exists($key, $hash)) { |
|
59
|
1 |
|
return $this->stringifyValue($hash[$key]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
if (strpos($key, '.') === false) { |
|
63
|
1 |
|
if (!is_array($hash) || !array_key_exists($key, $hash)) { |
|
64
|
1 |
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->stringifyValue($hash[$key]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
list($key, $subkey) = explode('.', $key, 2); |
|
71
|
|
|
|
|
72
|
1 |
|
return isset($hash[$key]) ? $this->deepGet($hash[$key], $subkey) : null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param mixed $value |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
7 |
|
private function stringifyValue($value) |
|
80
|
|
|
{ |
|
81
|
7 |
|
if (is_scalar($value)) { |
|
82
|
4 |
|
if (is_bool($value)) { |
|
83
|
2 |
|
$value = (int) $value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
return (string) $value; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
return json_encode($value); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param array $hash |
|
94
|
|
|
* @param string $prefix |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
1 |
|
private function deepKeys($hash, $prefix = "") |
|
98
|
|
|
{ |
|
99
|
1 |
|
$keys = array(); |
|
100
|
1 |
|
foreach ($hash as $k => $v) { |
|
101
|
1 |
|
if (is_scalar($v)) { |
|
102
|
1 |
|
array_push($keys, "{$prefix}{$k}"); |
|
103
|
|
|
} |
|
104
|
1 |
|
if (is_array($v)) { |
|
105
|
1 |
|
$keys = array_merge($keys, $this->deepKeys($hash[$k], "{$prefix}{$k}.")); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
return $keys; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|