Passed
Pull Request — master (#63)
by Eugene
05:16
created

DateTimeExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pack() 0 8 2
A __construct() 0 3 1
A getType() 0 3 1
A unpackExt() 0 3 1
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