Passed
Branch master (8a7ad0)
by Henri
01:23 queued 10s
created

SynchronizeTrait::mountTable_Null()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 3
rs 10
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
        $maxlength = null;
45
46
        if(strpos($value,'(')){
47
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type;
48
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type;
49
            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type;
50
        }
51
52
        $maxlength = (in_array( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength;
53
        $maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength;
54
        $maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength;
55
        $maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength;
56
57
        $this->$field = ['maxlength' => $maxlength];
58
        $this->$field = ['type' => $type];
59
    }
60
61
    protected function mountTable_Null(string $field, $value = null)
62
    {
63
        $this->$field = ['null' => ($value === 'YES') ? 1 : 0];
64
    }
65
66
    protected function mountTable_Key(string $field, $value = null)
67
    {
68
        $this->$field = ['key' => $value];
69
        $this->$field = ['upgradeable' => ($value == 'PRI') ? 0 : 1];
70
    }
71
72
    protected function mountTable_Extra(string $field, $value = null)
73
    {
74
        $this->$field = ['extra' => $value];
75
    }
76
77
    protected function mountTable_Default(string $field, $value = null)
78
    {
79
        $this->$field = ['default' => $value];
80
        $this->$field = ['value' => null];
81
        $this->$field = ['changed' => false];
82
        $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...
83
    }
84
85
}