Completed
Pull Request — master (#21)
by Eugene
05:40
created

DateTimeTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 33
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getType() 0 4 1
A pack() 0 10 3
A unpack() 0 7 1
1
<?php
2
3
namespace App\MessagePack;
4
5
use MessagePack\BufferUnpacker;
6
use MessagePack\Packer;
7
use MessagePack\TypeTransformer\Extension;
8
9
class DateTimeTransformer implements Extension
10
{
11
    private $type;
12
13
    public function __construct($type)
14
    {
15
        $this->type = $type;
16
    }
17
18
    public function getType()
19
    {
20
        return $this->type;
21
    }
22
23
    public function pack(Packer $packer, $value)
24
    {
25
        if (!$value instanceof \DateTimeInterface && !$value instanceof \DateTime) {
26
            return null;
27
        }
28
29
        $data = $packer->packStr($value->format(\DateTime::RFC3339));
30
31
        return $packer->packExt($this->type, $data);
32
    }
33
34
    public function unpack(BufferUnpacker $unpacker, $length)
35
    {
36
        $unpacker->skip(1);
37
        $data = $unpacker->unpackStr($length - 1);
38
39
        return new \DateTime($data);
40
    }
41
}
42