1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the rybakit/msgpack.php package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace MessagePack; |
13
|
|
|
|
14
|
|
|
use MessagePack\Exception\InvalidOptionException; |
15
|
|
|
use MessagePack\Exception\PackingFailedException; |
16
|
|
|
use MessagePack\Exception\UnpackingFailedException; |
17
|
|
|
use MessagePack\Extension\TimestampExtension; |
18
|
|
|
|
19
|
|
|
final class MessagePack |
20
|
|
|
{ |
21
|
|
|
/** @var Extension[]|null */ |
22
|
|
|
private static $extensions; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @codeCoverageIgnore |
26
|
|
|
*/ |
27
|
|
|
private function __construct() |
28
|
|
|
{ |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param mixed $value |
33
|
|
|
* @param PackOptions|int|null $options |
34
|
|
|
* |
35
|
|
|
* @throws InvalidOptionException |
36
|
|
|
* @throws PackingFailedException |
37
|
|
|
*/ |
38
|
3 |
|
public static function pack($value, $options = null) : string |
39
|
|
|
{ |
40
|
3 |
|
return (new Packer($options, self::getBuiltInExtensions()))->pack($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param UnpackOptions|int|null $options |
45
|
|
|
* |
46
|
|
|
* @throws InvalidOptionException |
47
|
|
|
* @throws UnpackingFailedException |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
3 |
|
public static function unpack(string $data, $options = null) |
52
|
|
|
{ |
53
|
3 |
|
return (new BufferUnpacker($data, $options, self::getBuiltInExtensions()))->unpack(); |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
private static function getBuiltInExtensions() : array |
57
|
|
|
{ |
58
|
6 |
|
return self::$extensions ?? self::$extensions = [ |
59
|
6 |
|
new TimestampExtension(), |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|