Completed
Push — master ( 229d8c...e7f8de )
by Dmitry
02:48
created

Convention   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
lcom 0
cbo 1
dl 0
loc 71
ccs 0
cts 54
cp 0
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 11 3
A getTarantoolType() 0 8 2
A isPrimitive() 0 4 1
C encode() 0 24 7
A decode() 0 8 3
A getDefaultValue() 0 8 3
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Mapper\Contracts;
6
7
class Convention implements Contracts\Convention
8
{
9
    public function getType($property)
10
    {
11
        if ($property == 'id') {
12
            return 'integer';
13
        }
14
        if (substr($property, -3) == '_at') {
15
            return 'integer';
16
        }
17
18
        return 'string';
19
    }
20
21
    public function getTarantoolType($type)
22
    {
23
        if ($type == 'string') {
24
            return 'STR';
25
        }
26
27
        return 'UNSIGNED';
28
    }
29
30
    public function isPrimitive($type)
31
    {
32
        return in_array($type, ['integer', 'string', 'array']);
33
    }
34
35
    public function encode($type, $value)
36
    {
37
        if (!$this->isPrimitive($type)) {
38
            if ($value instanceof Contracts\Entity) {
39
                return $value->getId();
40
            }
41
42
            return +$value;
43
        }
44
45
        if ($type == 'integer') {
46
            return $value ? +$value : 0;
47
        }
48
49
        if (is_null($value)) {
50
            return;
51
        }
52
53
        if (!is_array($value)) {
54
            return "$value";
55
        }
56
57
        return $value;
58
    }
59
60
    public function decode($type, $value)
61
    {
62
        if ($type == 'integer') {
63
            return $value ? +$value : 0;
64
        }
65
66
        return $value;
67
    }
68
69
    public function getDefaultValue($type)
70
    {
71
        if ($type == 'integer' || !$this->isPrimitive($type)) {
72
            return 0;
73
        }
74
75
        return '';
76
    }
77
}
78