Passed
Branch master (a5b543)
by Henri
01:26
created

SynchronizeTrait::mountTable_Maxlength()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
c 1
b 0
f 0
nc 16
nop 3
dl 0
loc 7
rs 9.6111
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
trait SynchronizeTrait{
6
    use CrudTrait;
7
8
    protected ?string $table = null;
9
    protected ?string $primary = null;
10
    protected bool $full = false;
11
12
    protected function create(string $table, string $primary)
13
    {
14
        $this->table = $table;
15
        $this->primary = $primary;
16
        $describe = $this->describe();
17
        
18
        $this->check_fail();
19
20
        $this->mountData($describe);
21
        $this->full = true;
22
        return $this;
23
    }
24
25
    protected function mountData(array $table): Datamanager
26
    {
27
        foreach ($table as $column) {
28
            foreach ($column as $propriety => $value) {
29
                $method = "mountTable_{$propriety}";
30
                $this->$method($column['Field'], $value);
31
            }
32
        }
33
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Datamanager\SynchronizeTrait which includes types incompatible with the type-hinted return HnrAzevedo\Datamanager\Datamanager.
Loading history...
34
    }
35
36
    protected function mountTable_Field(string $field, $value = null)
37
    {
38
        $this->$field = null;
39
    }
40
41
    protected function mountTable_Type(string $field, $value = null)
42
    {
43
        $type = $value;
44
45
        if(strpos($value,'(')){
46
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type;
47
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type;
48
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type;
49
        }
50
51
        $this->mountTable_Maxlength($field, $type, $value);
52
        $this->$field = ['type' => $type];
53
    }
54
55
    protected function mountTable_Maxlength(string $field, string $type, $default = null)
56
    {
57
        $maxlength = (in_array( $type , ['string','float','int'])) ? substr($default,(strpos($default,'(')+1),-1) : 0;
58
        $maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength;
59
        $maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength;
60
        $maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength;
61
        $this->$field = ['maxlength' => $maxlength];
62
    }
63
64
    protected function mountTable_Null(string $field, $value = null)
65
    {
66
        $this->$field = ['null' => ($value === 'YES') ? 1 : 0];
67
    }
68
69
    protected function mountTable_Key(string $field, $value = null)
70
    {
71
        $this->$field = ['key' => $value];
72
        $this->$field = ['upgradeable' => ($value == 'PRI') ? 0 : 1];
73
    }
74
75
    protected function mountTable_Extra(string $field, $value = null)
76
    {
77
        $this->$field = ['extra' => $value];
78
    }
79
80
    protected function mountTable_Default(string $field, $value = null)
81
    {
82
        $this->$field = ['default' => $value];
83
        $this->$field = ['value' => null];
84
        $this->$field = ['changed' => false];
85
        $this->select[$field] = true;
0 ignored issues
show
Bug Best Practice introduced by
The property select does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
    }
87
88
}