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

Convention::encode()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 0
cts 19
cp 0
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 12
nc 7
nop 2
crap 56
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