Completed
Push — master ( 3615fd...6bd7ca )
by Eugene
06:46
created

Packer::packNil()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\TypeTransformer\CanPack;
17
18
class Packer
19
{
20
    private const UTF8_REGEX = '/\A(?:
21
          [\x00-\x7F]++                      # ASCII
22
        | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
23
        |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
24
        | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
25
        |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
26
        |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
27
        | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
28
        |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
29
        )*+\z/x';
30
31
    private $isDetectStrBin;
32
    private $isForceStr;
33
    private $isDetectArrMap;
34
    private $isForceArr;
35
    private $isForceFloat32;
36
37
    /**
38
     * @var CanPack[]
39
     */
40
    private $transformers = [];
41
42
    /**
43
     * @param PackOptions|int|null $options
44
     * @param CanPack[] $transformers
45
     *
46
     * @throws InvalidOptionException
47 184
     */
48
    public function __construct($options = null, array $transformers = [])
49 184
    {
50 183
        if (null === $options) {
51 20
            $options = PackOptions::fromDefaults();
52 20
        } elseif (!$options instanceof PackOptions) {
53
            $options = PackOptions::fromBitmask($options);
54
        }
55 184
56 184
        $this->isDetectStrBin = $options->isDetectStrBinMode();
57 184
        $this->isForceStr = $options->isForceStrMode();
58 184
        $this->isDetectArrMap = $options->isDetectArrMapMode();
59 184
        $this->isForceArr = $options->isForceArrMode();
60 184
        $this->isForceFloat32 = $options->isForceFloat32Mode();
61
62 2
        if ([] !== $transformers) {
63
            $this->transformers = $transformers;
64 2
        }
65
    }
66 2
67
    public function extendWith(CanPack $transformer, CanPack ...$transformers) : self
68
    {
69 116
        $new = clone $this;
70
        $new->transformers[] = $transformer;
71 116
72 63
        if ([] !== $transformers) {
73
            $new->transformers = \array_merge($new->transformers, $transformers);
74 76
        }
75 35
76 1
        return $new;
77
    }
78 34
79 3
    public function pack($value)
80
    {
81 31
        if (\is_int($value)) {
82 21
            return $this->packInt($value);
83
        }
84
        if (\is_string($value)) {
85 14
            if ('' === $value) {
86
                return $this->isForceStr || $this->isDetectStrBin ? "\xa0" : "\xc4\x00";
87 53
            }
88 29
            if ($this->isForceStr) {
89 1
                return $this->packStr($value);
90
            }
91 28
            if ($this->isDetectStrBin && \preg_match(self::UTF8_REGEX, $value)) {
92 21
                return $this->packStr($value);
93 13
            }
94
95
            return $this->packBin($value);
96 12
        }
97 12
        if (\is_array($value)) {
98 12
            if ([] === $value) {
99
                return $this->isDetectArrMap || $this->isForceArr ? "\x90" : "\x80";
100
            }
101 7
            if ($this->isDetectArrMap) {
102
                if (!isset($value[0]) && !\array_key_exists(0, $value)) {
103 31
                    return $this->packMap($value);
104 7
                }
105
106 28
                return \array_values($value) === $value
107 10
                    ? $this->packArray($value)
108
                    : $this->packMap($value);
109 20
            }
110 6
111
            return $this->isForceArr ? $this->packArray($value) : $this->packMap($value);
112 14
        }
113 10
        if (null === $value) {
114
            return "\xc0";
115 4
        }
116 2
        if (\is_bool($value)) {
117 2
            return $value ? "\xc3" : "\xc2";
118 1
        }
119
        if (\is_float($value)) {
120
            return $this->packFloat($value);
121
        }
122
        if ($value instanceof Ext) {
123 3
            return $this->packExt($value->type, $value->data);
124
        }
125
        if ([] !== $this->transformers) {
126 1
            foreach ($this->transformers as $transformer) {
127
                if (null !== $packed = $transformer->pack($this, $value)) {
128 1
                    return $packed;
129
                }
130
            }
131 2
        }
132
133 2
        throw PackingFailedException::unsupportedType($value);
134
    }
135
136 98
    public function packNil()
137
    {
138 98
        return "\xc0";
139 66
    }
140 40
141
    public function packBool($bool)
142 34
    {
143 12
        return $bool ? "\xc3" : "\xc2";
144
    }
145 26
146 12
    public function packInt($int)
147
    {
148 16
        if ($int >= 0) {
149 10
            if ($int <= 0x7f) {
150
                return \chr($int);
151
            }
152 8
            if ($int <= 0xff) {
153
                return "\xcc".\chr($int);
154
            }
155 34
            if ($int <= 0xffff) {
156 8
                return "\xcd".\chr($int >> 8).\chr($int);
157
            }
158 26
            if ($int <= 0xffffffff) {
159 6
                return \pack('CN', 0xce, $int);
160
            }
161 20
162 6
            return \pack('CJ', 0xcf, $int);
163
        }
164 14
165 6
        if ($int >= -0x20) {
166
            return \chr(0xe0 | $int);
167
        }
168 8
        if ($int >= -0x80) {
169
            return "\xd0".\chr($int);
170
        }
171 9
        if ($int >= -0x8000) {
172
            return "\xd1".\chr($int >> 8).\chr($int);
173 9
        }
174 1
        if ($int >= -0x80000000) {
175 9
            return \pack('CN', 0xd2, $int);
176
        }
177
178 38
        return \pack('CJ', 0xd3, $int);
179
    }
180 38
181
    public function packFloat($float)
182 38
    {
183 24
        return $this->isForceFloat32
184
            ? "\xca".\pack('G', $float)
185 14
            : "\xcb".\pack('E', $float);
186 8
    }
187
188 6
    public function packStr($str)
189 4
    {
190
        $length = \strlen($str);
191
192 2
        if ($length < 32) {
193
            return \chr(0xa0 | $length).$str;
194
        }
195 21
        if ($length <= 0xff) {
196
            return "\xd9".\chr($length).$str;
197 21
        }
198
        if ($length <= 0xffff) {
199 21
            return "\xda".\chr($length >> 8).\chr($length).$str;
200 17
        }
201
202 4
        return \pack('CN', 0xdb, $length).$str;
203 2
    }
204
205
    public function packBin($str)
206 2
    {
207
        $length = \strlen($str);
208
209 21
        if ($length <= 0xff) {
210
            return "\xc4".\chr($length).$str;
211 21
        }
212
        if ($length <= 0xffff) {
213 21
            return "\xc5".\chr($length >> 8).\chr($length).$str;
214 20
        }
215
216
        return \pack('CN', 0xc6, $length).$str;
217 21
    }
218
219
    public function packArray($array)
220 21
    {
221
        $data = $this->packArrayHeader(\count($array));
222 21
223 15
        foreach ($array as $val) {
224
            $data .= $this->pack($val);
225 6
        }
226 4
227
        return $data;
228
    }
229 2
230
    public function packArrayHeader($size)
231
    {
232 24
        if ($size <= 0xf) {
233
            return \chr(0x90 | $size);
234 24
        }
235
        if ($size <= 0xffff) {
236 24
            return "\xdc".\chr($size >> 8).\chr($size);
237 1
        }
238 1
239 1
        return \pack('CN', 0xdd, $size);
240
    }
241
242 1
    public function packMap($map)
243
    {
244
        $data = $this->packMapHeader(\count($map));
245 23
246 22
        if ($this->isForceStr) {
247 22
            foreach ($map as $key => $val) {
248 4
                $data .= \is_string($key) ? $this->packStr($key) : $this->packInt($key);
249 22
                $data .= $this->pack($val);
250 22
            }
251
252
            return $data;
253 22
        }
254
255
        if ($this->isDetectStrBin) {
256 1
            foreach ($map as $key => $val) {
257 1
                $data .= \is_string($key)
258 1
                    ? (\preg_match(self::UTF8_REGEX, $key) ? $this->packStr($key) : $this->packBin($key))
259
                    : $this->packInt($key);
260
                $data .= $this->pack($val);
261 1
            }
262
263
            return $data;
264 24
        }
265
266 24
        foreach ($map as $key => $val) {
267 18
            $data .= \is_string($key) ? $this->packBin($key) : $this->packInt($key);
268
            $data .= $this->pack($val);
269 6
        }
270 4
271
        return $data;
272
    }
273 2
274
    public function packMapHeader($size)
275
    {
276 18
        if ($size <= 0xf) {
277
            return \chr(0x80 | $size);
278 18
        }
279
        if ($size <= 0xffff) {
280 18
            return "\xde".\chr($size >> 8).\chr($size);
281 18
        }
282 16
283 14
        return \pack('CN', 0xdf, $size);
284 12
    }
285 10
286
    public function packExt($type, $data)
287
    {
288 8
        $length = \strlen($data);
289 4
290
        switch ($length) {
291 4
            case 1: return "\xd4".\chr($type).$data;
292 2
            case 2: return "\xd5".\chr($type).$data;
293
            case 4: return "\xd6".\chr($type).$data;
294
            case 8: return "\xd7".\chr($type).$data;
295 2
            case 16: return "\xd8".\chr($type).$data;
296
        }
297
298
        if ($length <= 0xff) {
299
            return "\xc7".\chr($length).\chr($type).$data;
300
        }
301
        if ($length <= 0xffff) {
302
            return \pack('CnC', 0xc8, $length, $type).$data;
303
        }
304
305
        return \pack('CNC', 0xc9, $length, $type).$data;
306
    }
307
}
308