Passed
Pull Request — master (#63)
by Eugene
02:57
created

DateTimeExtension::pack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
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\Tests\Integration\MessagePack;
15
16
use MessagePack\BufferUnpacker;
17
use MessagePack\Packer;
18
use MessagePack\TypeTransformer\Extension;
19
20
class DateTimeExtension implements Extension
21
{
22
    private $type;
23
24
    public function __construct(int $type)
25
    {
26
        $this->type = $type;
27
    }
28
29
    public function getType() : int
30
    {
31
        return $this->type;
32
    }
33
34
    public function pack(Packer $packer, $value) : ?string
35
    {
36
        if (!$value instanceof \DateTimeInterface) {
37
            return null;
38
        }
39
40
        return $packer->packExt($this->type,
41
            $packer->packStr($value->format('Y-m-d\TH:i:s.uP'))
42
        );
43
    }
44
45
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
46
    {
47
        return new \DateTimeImmutable($unpacker->unpackStr());
48
    }
49
}
50