Completed
Push — master ( 31d679...5a85ab )
by Dmitry
03:57
created

Convention::encode()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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