Passed
Push — master ( 47c498...8f00fc )
by Eugene
02:11
created

DecimalExtension::pack()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
nc 5
nop 2
dl 0
loc 26
ccs 14
cts 15
cp 0.9333
crap 5.0073
rs 9.4555
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client 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
declare(strict_types=1);
13
14
namespace Tarantool\Client\Packer\Extension;
15
16
use Decimal\Decimal;
0 ignored issues
show
Bug introduced by
The type Decimal\Decimal was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use MessagePack\BufferUnpacker;
18
use MessagePack\Packer;
19
use MessagePack\TypeTransformer\Extension;
20
21
class DecimalExtension implements Extension
22
{
23
    private const TYPE = 1;
24
    private const PRECISION = 38;
25
26 41
    public function getType() : int
27
    {
28 41
        return self::TYPE;
29
    }
30
31
    /**
32
     * @param object $value
33
     */
34 36
    public function pack(Packer $packer, $value) : ?string
35
    {
36 36
        if (!$value instanceof Decimal) {
37
            return null;
38
        }
39
40
        // @see https://github.com/php-decimal/ext-decimal/issues/22#issuecomment-512364914
41 36
        $data = $value->toFixed(self::PRECISION);
42
43 36
        if ('-' === $data[0]) {
44 8
            $nibble = 'd';
45 8
            $data = \substr($data, 1);
46
        } else {
47 28
            $nibble = 'c';
48
        }
49
50 36
        $pieces = \explode('.', $data, 2);
51 36
        $pieces[1] = \rtrim($pieces[1], '0');
52
53 36
        $data = "{$pieces[0]}{$pieces[1]}{$nibble}";
54 36
        if (0 !== \strlen($data) % 2) {
55 22
            $data = '0'.$data;
56
        }
57
58 36
        return $packer->packExt(self::TYPE,
59 36
            $packer->packInt('' === $pieces[1] ? 0 : \strlen($pieces[1])).\hex2bin($data)
60
        );
61
    }
62
63
    /**
64
     * @return Decimal
65
     */
66 36
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
67
    {
68 36
        $scale = $unpacker->unpackInt();
69 36
        $data = $unpacker->read($extLength - 1);
70 36
        $data = \bin2hex($data);
71
72 36
        $sign = 'd' === $data[-1] ? '-' : '';
73 36
        $dec = \substr($data, 0, -1);
74
75 36
        if (0 !== $scale) {
76 22
            $length = \strlen($dec);
77 22
            $dec = ($length <= $scale)
78 10
                ? \substr_replace($dec, '0.'.\str_repeat('0', $scale - $length), -$scale, 0)
79 22
                : \substr_replace($dec, '.', -$scale, 0);
80
        }
81
82 36
        return new Decimal($sign.$dec, self::PRECISION);
83
    }
84
}
85