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

Convention   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 51
ccs 23
cts 24
cp 0.9583
rs 10

5 Methods

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