Test Setup Failed
Push — master ( d6fde7...1e848b )
by Php Easy Api
04:20
created

QuerySyntaxHelper::getIndexValueList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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
        $list   = [];
63
        $list[] = $this->getNullableValue($this->data['nullable'],$name);
64
        $list[] = (isset($this->data['autoIncrement'][$name])) ? 'AUTO_INCREMENT' : '';
65
        $list[] = (isset($this->data['primaryKey'][$name])) ? 'PRIMARY KEY' : '';
66
        $list[] = (isset($this->data['default'][$name])) ? ' DEFAULT "'.$this->data['default'][$name].'"' : '';
67
        $list[] = (isset($this->data['comment'][$name])) ? ' COMMENT "'.$this->data['comment'][$name].'"' : '';
68
69
        return $list;
70
71
    }
72
73
    /**
74
     * @param $name
75
     * @return array
76
     */
77
    protected function getUniqueValueList($name)
78
    {
79
        //get unique
80
        if(isset($this->data['unique'][$name])){
81
            $this->data['uniqueValueList'][]      = 'UNIQUE INDEX '.$this->data['unique'][$name]['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...
82
        }
83
    }
84
85
    /**
86
     * @param $name
87
     */
88
    protected function getIndexValueList($name)
89
    {
90
        //get index
91
        if(isset($this->data['index'][$name])){
92
            $this->data['indexValueList'][]  = 'INDEX '.$this->data['index'][$name]['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...
93
        }
94
    }
95
96
    /**
97
     * @param $name
98
     */
99
    protected function setIndex($name)
100
    {
101
        //get unique
102
        $this->getUniqueValueList($name);
103
104
        //get index
105
        $this->getIndexValueList($name);
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    protected function getCreateDefaultList()
112
    {
113
        $list               = [];
114
115
        foreach ($this->data['names'] as $key=>$name)
116
        {
117
            list(
118
                $nullableValue,$autoIncrementValue,
119
                $primaryKeyValue,$defaultValue,$commentValue
120
                ) = $this->getValueWithIsset($name);
121
122
            //set index values
123
            $this->setIndex($name);
124
125
            $list[]=''.$name.' '.$this->data['types'][$key].' '.$nullableValue.' '.$defaultValue.' '.$primaryKeyValue.' '.$autoIncrementValue.' '.$commentValue.'';
126
        }
127
128
        return $list;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    protected function getKeyList()
135
    {
136
        $keyList            = [];
137
138
        //multiple indexes
139
        if(isset($this->data['index']['indexes'])){
140
141
            foreach ($this->data['index']['indexes'] as $index_key=>$index_value){
142
143
                $indexesCommentValue = (isset($index_value['comment'])) ? 'COMMENT "'.$index_value['comment'].'"' : '';
144
145
                $keyType    = (isset($index_value['type'])) ? ucfirst($index_value['type']) : 'KEY';
146
147
                $keyList[]  = "".$keyType." ".$index_value['name']." (".implode(",",$index_value['value']).") ".$indexesCommentValue;
148
            }
149
        }
150
151
        return $keyList;
152
    }
153
154
    /**
155
     * @param $reference
156
     * @return string
157
     */
158
    protected function getReferenceSyntax($reference)
159
    {
160
        $list = [];
161
162
        foreach ($reference as $constraint=>$values){
163
164
            $list[]=',CONSTRAINT '.$constraint.' FOREIGN KEY ('.$values['key'].') 
165
            REFERENCES '.$values['references']['table'].'('.$values['references']['field'].') '.$this->getOnProcess($values);
166
        }
167
168
        return implode (" ",$list);
169
    }
170
171
    /**
172
     * @param $referenceValue
173
     */
174
    private function getOnProcess($referenceValue)
175
    {
176
        if(isset($referenceValue['on']) && isset($referenceValue['onOption'])){
177
            return ''.$referenceValue['on'].' '.strtoupper($referenceValue['onOption']).'';
178
        }
179
180
        return '';
181
    }
182
183
}
184
185