Test Setup Failed
Push — master ( c6f3d6...c97842 )
by Php Easy Api
03:48
created

QuerySyntaxHelper::getValueWithIsset()   B

Complexity

Conditions 9
Paths 64

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 13
nc 64
nop 1
dl 0
loc 23
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
namespace Migratio\GrammarStructure\Mysql;
4
5
6
class QuerySyntaxHelper
7
{
8
    /**
9
     * @param $nullable
10
     * @param $name
11
     * @return string
12
     */
13
    protected function getNullableValue($nullable,$name)
14
    {
15
        $nullableValue='';
16
17
        if(isset($nullable[$name])){
18
            if($nullable[$name]===false){
19
                $nullableValue='NOT NULL';
20
            }
21
            else{
22
                $nullableValue='NULL';
23
            }
24
        }
25
26
        return $nullableValue;
27
    }
28
29
    /**
30
     * @return void
31
     */
32
    protected function getCreateTableSyntax()
33
    {
34
        $this->syntax[] = 'CREATE TABLE '.$this->table.' (';
0 ignored issues
show
Bug Best Practice introduced by
The property syntax does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
    }
36
37
    /**
38
     * @param $object
39
     */
40
    protected function getWizardObjects($object)
41
    {
42
        $this->data['names']            = $object->getNames();
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->data['types']            = $object->getTypes();
44
        $this->data['default']          = $object->getDefault();
45
        $this->data['autoIncrement']    = $object->getAutoIncrement();
46
        $this->data['primaryKey']       = $object->getPrimaryKey();
47
        $this->data['tableCollation']   = $object->getCollation();
48
        $this->data['engine']           = $object->getEngine();
49
        $this->data['nullable']         = $object->getNullable();
50
        $this->data['comment']          = $object->getComment();
51
        $this->data['unique']           = $object->getUnique();
52
        $this->data['index']            = $object->getIndex();
53
        $this->data['references']       = $object->getReferences();
54
    }
55
56
    /**
57
     * @param $name
58
     * @return array
59
     */
60
    protected function getValueWithIsset($name)
61
    {
62
        $nameKey = array_search($name,$this->data['names']);
63
        
64
        $list   = [];
65
        $list[] = $this->getNullableValue($this->data['nullable'],$name);
66
        $list[] = (isset($this->data['autoIncrement'][$name])) ? 'AUTO_INCREMENT' : '';
67
        $list[] = (isset($this->data['primaryKey'][$name])) ? 'PRIMARY KEY' : '';
68
        
69
        if($this->data['types'][$nameKey]=='timestamp' && !isset($this->data['default'][$nameKey])){
70
            $this->data['default'][$name] = 'CURRENT_TIMESTAMP';
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71
        }
72
        
73
        if($this->data['types'][$nameKey]!=='timestamp'){
74
            $list[] = (isset($this->data['default'][$name])) ? ' DEFAULT "'.$this->data['default'][$name].'"' : '';
75
        }
76
        else{
77
            $list[] = (isset($this->data['default'][$name])) ? ' DEFAULT '.$this->data['default'][$name].'' : '';
78
        }
79
        
80
        $list[] = (isset($this->data['comment'][$name])) ? ' COMMENT "'.$this->data['comment'][$name].'"' : '';
81
82
        return $list;
83
84
    }
85
86
    /**
87
     * @param $name
88
     * @return array
89
     */
90
    protected function getUniqueValueList($name)
91
    {
92
        //get unique
93
        if(isset($this->data['unique'][$name])){
94
            $this->data['uniqueValueList'][]      = 'UNIQUE INDEX '.$name.' ('.$this->data['unique'][$name]['value'].' ASC)';
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
        }
96
    }
97
98
    /**
99
     * @param $name
100
     */
101
    protected function getIndexValueList($name)
102
    {
103
        //get index
104
        if(isset($this->data['index'][$name])){
105
            $this->data['indexValueList'][]  = 'INDEX '.$name.' ('.$this->data['index'][$name]['value'].')';
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106
        }
107
    }
108
109
    /**
110
     * @param $name
111
     */
112
    protected function setIndex($name)
113
    {
114
        //get unique
115
        $this->getUniqueValueList($name);
116
117
        //get index
118
        $this->getIndexValueList($name);
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    protected function getCreateDefaultList()
125
    {
126
        $list               = [];
127
128
        foreach ($this->data['names'] as $key=>$name)
129
        {
130
            list(
131
                $nullableValue,$autoIncrementValue,
132
                $primaryKeyValue,$defaultValue,$commentValue
133
                ) = $this->getValueWithIsset($name);
134
135
            //set index values
136
            $this->setIndex($name);
137
138
            $list[]=''.$name.' '.$this->data['types'][$key].' '.$nullableValue.' '.$defaultValue.' '.$primaryKeyValue.' '.$autoIncrementValue.' '.$commentValue.'';
139
        }
140
141
        return $list;
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    protected function getKeyList()
148
    {
149
        $keyList            = [];
150
151
        //multiple indexes
152
        if(isset($this->data['index']['indexes'])){
153
154
            foreach ($this->data['index']['indexes'] as $index_key=>$index_value){
155
156
                $indexesCommentValue = (isset($index_value['comment'])) ? 'COMMENT "'.$index_value['comment'].'"' : '';
157
158
                $keyType    = (isset($index_value['type'])) ? ucfirst($index_value['type']) : 'KEY';
159
160
                $keyList[]  = "".$keyType." ".$index_value['name']." (".implode(",",$index_value['value']).") ".$indexesCommentValue;
161
            }
162
        }
163
164
        return $keyList;
165
    }
166
167
    /**
168
     * @param $reference
169
     * @return string
170
     */
171
    protected function getReferenceSyntax($reference)
172
    {
173
        $list = [];
174
175
        foreach ($reference as $constraint=>$values){
176
177
            $list[]=',CONSTRAINT '.$constraint.' FOREIGN KEY ('.$values['key'].') 
178
            REFERENCES '.$values['references']['table'].'('.$values['references']['field'].') '.$this->getOnProcess($values);
179
        }
180
181
        return implode (" ",$list);
182
    }
183
184
    /**
185
     * @param $referenceValue
186
     */
187
    private function getOnProcess($referenceValue)
188
    {
189
        if(isset($referenceValue['on']) && isset($referenceValue['onOption'])){
190
            return ''.$referenceValue['on'].' '.strtoupper($referenceValue['onOption']).'';
191
        }
192
193
        return '';
194
    }
195
196
}
197
198