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

Convention   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 13
c 3
b 1
f 1
lcom 0
cbo 1
dl 0
loc 58
ccs 26
cts 26
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 11 3
A getTarantoolType() 0 8 2
A isPrimitive() 0 4 1
B encode() 0 20 5
A decode() 0 8 2
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