Completed
Push — master ( eb6d5f...893c58 )
by Dmitry
03:11
created

Convention::decode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Mapper\Contracts;
6
7
class Convention implements Contracts\Convention
8
{
9 24
    public function getType($property)
10
    {
11 24
        if ($property == 'id') {
12 24
            return 'integer';
13
        }
14 24
        if (substr($property, -3) == '_at') {
15 1
            return 'integer';
16
        }
17
18 24
        return 'string';
19
    }
20
21 24
    public function getTarantoolType($type)
22
    {
23 24
        if ($type == 'string') {
24 24
            return 'STR';
25
        }
26
27 24
        return 'NUM';
28
    }
29
30 24
    public function isPrimitive($type)
31
    {
32 24
        return in_array($type, ['integer', 'string']);
33
    }
34
35 24
    public function encode($type, $value)
36
    {
37 24
        if (!$this->isPrimitive($type)) {
38 4
            if ($value instanceof Contracts\Entity) {
39 3
                return $value->getId();
40
            }
41
        }
42 24
        if ($type == 'integer') {
43 24
            return +$value;
44
        }
45
46 24
        return $value;
47
    }
48
49 24
    public function decode($type, $value)
50
    {
51 24
        if ($type == 'integer') {
52 24
            return +$value;
53
        }
54
55 8
        return $value;
56
    }
57
}
58