Binn::serialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Binn. Serialize to bin string.
4
 * Binn Specification: https://github.com/liteserver/binn/blob/master/spec.md
5
 *
6
 * Original Binn Library for C++ - https://github.com/liteserver/binn
7
 *
8
 *
9
 * @author      Nikita Kuznetsov (NiK)
10
 * @copyright   Copyright (c) 2016, Nikita Kuznetsov ([email protected])
11
 * @license     GNU GPL
12
 * @link        http://www.gameap.ru
13
 *
14
 */
15
16
namespace Knik\Binn;
17
18
class Binn extends BinnAbstract
19
{
20
    /**
21
     * State. Will be removed in 1.0
22
     * @var array
23
     */
24
    protected $items = [];
25
26
    /**
27
     * State. Will be removed in 1.0
28
     * @var string
29
     */
30
    protected $binn = '';
31
32
    public function serialize($data = null)
33
    {
34
        if ($data === null) {
35
            return $this->encoder->encode($this->items, 'binn');
36
        }
37
38
        return $this->encoder->encode($data, 'binn');
39
    }
40
41 17
    public function unserialize($binnString = null)
42
    {
43 17
        if ($binnString === null) {
44 17
            return $this->decoder->decode($this->binn, 'binn');
45 17
        }
46 17
47
        return $this->decoder->decode($binnString, 'binn');
48 17
    }
49
50
    /**
51
     * @deprecated use serialize/unserialize
52
     */
53
    public function binnOpen(string $binn = ''): void
54 14
    {
55
        $this->binn  = $binn;
56 14
        $this->items = $this->unserialize($binn);
57
    }
58 14
59
    /**
60 12
     * @deprecated use serialize/unserialize
61 12
     */
62 12
    public function getBinnVal(): string
63
    {
64
        $this->binn = $this->serialize();
65 2
        return $this->binn;
66
    }
67
68
    /**
69
     * @deprecated
70
     */
71 9
    public function getBinnArr(): array
72
    {
73 9
        return $this->items;
74 3
    }
75
76
    /**
77 9
     * @deprecated
78
     */
79 9
    public function binnSize(): int
80
    {
81 9
        return strlen($this->binn);
82 9
    }
83 9
84
    /**
85 3
     * @deprecated
86
     */
87
    public function binnFree()
88
    {
89
        $this->binn = '';
90
        $this->items = [];
91
92
        return $this;
93
    }
94
95
    public function toArray(): array
96
    {
97
        return $this->items;
98
    }
99
}
100