|
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; |
|
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
|
31 |
|
public function getType() : int |
|
27
|
|
|
{ |
|
28
|
31 |
|
return self::TYPE; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param object $value |
|
33
|
|
|
*/ |
|
34
|
18 |
|
public function pack(Packer $packer, $value) : ?string |
|
35
|
|
|
{ |
|
36
|
18 |
|
if (!$value instanceof Decimal) { |
|
|
|
|
|
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// @see https://github.com/php-decimal/ext-decimal/issues/22#issuecomment-512364914 |
|
41
|
18 |
|
$data = $value->toFixed(self::PRECISION); |
|
42
|
|
|
|
|
43
|
18 |
|
if ('-' === $data[0]) { |
|
44
|
4 |
|
$nibble = 'd'; |
|
45
|
4 |
|
$data = \substr($data, 1); |
|
46
|
|
|
} else { |
|
47
|
14 |
|
$nibble = 'c'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
18 |
|
$pieces = \explode('.', $data, 2); |
|
51
|
18 |
|
$pieces[1] = \rtrim($pieces[1], '0'); |
|
52
|
|
|
|
|
53
|
18 |
|
$data = "{$pieces[0]}{$pieces[1]}{$nibble}"; |
|
54
|
18 |
|
if (0 !== \strlen($data) % 2) { |
|
55
|
11 |
|
$data = '0'.$data; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
18 |
|
return $packer->packExt(self::TYPE, |
|
59
|
18 |
|
$packer->packInt('' === $pieces[1] ? 0 : \strlen($pieces[1])).\hex2bin($data) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return Decimal |
|
65
|
|
|
*/ |
|
66
|
18 |
|
public function unpackExt(BufferUnpacker $unpacker, int $extLength) |
|
67
|
|
|
{ |
|
68
|
18 |
|
$scale = $unpacker->unpackInt(); |
|
69
|
18 |
|
$data = $unpacker->read($extLength - 1); |
|
70
|
18 |
|
$data = \bin2hex($data); |
|
71
|
|
|
|
|
72
|
18 |
|
$sign = 'd' === $data[-1] ? '-' : ''; |
|
73
|
18 |
|
$dec = \substr($data, 0, -1); |
|
74
|
|
|
|
|
75
|
18 |
|
if (0 !== $scale) { |
|
76
|
11 |
|
$length = \strlen($dec); |
|
77
|
11 |
|
$dec = ($length <= $scale) |
|
78
|
5 |
|
? \substr_replace($dec, '0.'.\str_repeat('0', $scale - $length), -$scale, 0) |
|
79
|
11 |
|
: \substr_replace($dec, '.', -$scale, 0); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
18 |
|
return new Decimal($sign.$dec, self::PRECISION); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.