Completed
Push — master ( 50c783...da0edd )
by Dmitry
03:37
created

Convention   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 96.43%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 62
wmc 14
lcom 0
cbo 1
ccs 27
cts 28
cp 0.9643
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
B encode() 0 24 6
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Mapper\Contracts;
6
7
class Convention implements Contracts\Convention
8
{
9 53
    public function getType($property)
10
    {
11 53
        if ($property == 'id') {
12 53
            return 'integer';
13
        }
14 53
        if (substr($property, -3) == '_at') {
15 1
            return 'integer';
16
        }
17
18 53
        return 'string';
19
    }
20
21 53
    public function getTarantoolType($type)
22
    {
23 53
        if ($type == 'string') {
24 53
            return 'STR';
25
        }
26
27 53
        return 'NUM';
28
    }
29
30 53
    public function isPrimitive($type)
31
    {
32 53
        return in_array($type, ['integer', 'string', 'array']);
33
    }
34
35 53
    public function encode($type, $value)
36
    {
37 53
        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 53
        if ($type == 'integer') {
46 53
            return +$value;
47
        }
48
49 53
        if (is_null($value)) {
50
            return;
51
        }
52
53 53
        if (!is_array($value)) {
54 53
            return "$value";
55
        }
56
57 3
        return $value;
58
    }
59
60 53
    public function decode($type, $value)
61
    {
62 53
        if ($type == 'integer') {
63 53
            return +$value;
64
        }
65
66 25
        return $value;
67
    }
68
}
69