Completed
Push — master ( b0984e...c79602 )
by Dmitry
03:30
created

Convention::getDefaultValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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