Completed
Branch master (eb6d5f)
by Dmitry
02:18
created

Convention::getTarantoolType()   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 1
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 22
    public function getType($property)
10
    {
11 22
        if ($property == 'id') {
12 22
            return 'integer';
13
        }
14 22
        if (substr($property, -3) == '_at') {
15 1
            return 'integer';
16
        }
17
18 22
        return 'string';
19
    }
20
21 22
    public function getTarantoolType($type)
22
    {
23 22
        if ($type == 'string') {
24 22
            return 'STR';
25
        }
26
27 22
        return 'NUM';
28
    }
29
30 22
    public function isPrimitive($type)
31
    {
32 22
        return in_array($type, ['integer', 'string']);
33
    }
34
35 22
    public function encode($type, $value)
36
    {
37 22
        if (!$this->isPrimitive($type)) {
38 3
            if ($value instanceof Contracts\Entity) {
39 3
                return $value->getId();
40
            }
41
        }
42 22
        if ($type == 'integer') {
43 22
            return +$value;
44
        }
45
46 22
        return $value;
47
    }
48
49 22
    public function decode($type, $value)
50
    {
51 22
        if ($type == 'integer') {
52 22
            return +$value;
53
        }
54
55 8
        return $value;
56
    }
57
}
58