|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Binn. Serialize to bin string. |
|
4
|
|
|
* Binn Specification: https://github.com/liteserver/binn/blob/master/spec.md |
|
5
|
|
|
* |
|
6
|
|
|
* Note! This class not support Map and Object, only List support. Sorry, i am working on this. |
|
7
|
|
|
* |
|
8
|
|
|
* Original Binn Library for C++ - https://github.com/liteserver/binn |
|
9
|
|
|
* |
|
10
|
|
|
* |
|
11
|
|
|
* @author Nikita Kuznetsov (NiK) |
|
12
|
|
|
* @copyright Copyright (c) 2016, Nikita Kuznetsov ([email protected]) |
|
13
|
|
|
* @license GNU GPL |
|
14
|
|
|
* @link http://www.gameap.ru |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Knik\Binn; |
|
19
|
|
|
|
|
20
|
|
|
class Binn extends BinnAbstract { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Size bin string in bytes |
|
24
|
|
|
* |
|
25
|
|
|
* @var int |
|
26
|
|
|
* @access protected |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $size = 0; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Bin string |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
* @access protected |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $binnString = ""; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Binn constructor |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->setContainersClasses([ |
|
44
|
|
|
self::BINN_LIST => BinnList::class, |
|
45
|
|
|
self::BINN_MAP => BinnMap::class, |
|
46
|
|
|
self::BINN_OBJECT => BinnObject::class, |
|
47
|
|
|
]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $array |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function serialize($array = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->binnFree(); |
|
57
|
|
|
|
|
58
|
|
|
foreach ($this->containersClasses as $contanerType => $containersClass) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($containersClass::validArray($array)) { |
|
61
|
|
|
$container = new $containersClass(); |
|
62
|
|
|
return $container->serialize($array); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $binnString |
|
69
|
|
|
* @return array|null |
|
70
|
|
|
*/ |
|
71
|
|
|
public function unserialize($binnString = '') |
|
72
|
|
|
{ |
|
73
|
|
|
if (empty($binnString)) { |
|
74
|
|
|
return $this->getBinnArr(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->binnFree(); |
|
78
|
|
|
|
|
79
|
|
|
$type = $this->unpack(Binn::BINN_UINT8, $binnString[0]); |
|
80
|
|
|
|
|
81
|
|
|
if (array_key_exists($type, $this->containersClasses)) { |
|
82
|
|
|
$binnContainer = new $this->containersClasses[$type]($binnString); |
|
83
|
|
|
return $binnContainer->unserialize(); |
|
84
|
|
|
} else { |
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |