Passed
Push — master ( e376c5...c7f7e6 )
by Francesc
02:40
created

TableInformation::getTablesMsg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: shawe [email protected]
5
 * Date: 26/05/18
6
 * Time: 22:11
7
 */
8
9
namespace FacturaScriptsUtils\Console\Command\Common;
10
11
use FacturaScripts\Core\Base\DataBase;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base\DataBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Trait TableInformation.
15
 *
16
 * @author Francesc Pineda Segarra <[email protected]>
17
 */
18
trait TableInformation
19
{
20
21
    /**
22
     * Folder destiny path.
23
     *
24
     * @var string
25
     */
26
    private $dstFolder;
27
28
    /**
29
     * Name of the model.
30
     *
31
     * @var string
32
     */
33
    private $modelName;
34
35
    /**
36
     * Name of the table.
37
     *
38
     * @var string
39
     */
40
    private $tableName;
41
42
    /**
43
     * Text of the table field prefix.
44
     *
45
     * @var string
46
     */
47
    private $fieldPrefix;
48
    /**
49
     * Extra fields.
50
     *
51
     * @var array
52
     */
53
    private $extraFields = [];
54
55
    /**
56
     * Set destiny folder.
57
     *
58
     * @param string $dstFolder
59
     *
60
     * @return $this
61
     */
62
    public function setDstFolder(string $dstFolder): self
63
    {
64
        $this->dstFolder = $dstFolder;
65
        return $this;
66
    }
67
68
    /**
69
     * Set the model name to use.
70
     *
71
     * @param string $modelName
72
     *
73
     * @return $this
74
     */
75
    public function setModelName(string $modelName): self
76
    {
77
        $this->modelName = $modelName;
78
        return $this;
79
    }
80
81
    /**
82
     * Set table name to use.
83
     *
84
     * @param string $tableName
85
     *
86
     * @return $this
87
     */
88
    public function setTableName(string $tableName): self
89
    {
90
        $this->tableName = $tableName;
91
        return $this;
92
    }
93
94
    /**
95
     * Return the folder destiny path.
96
     *
97
     * @return string
98
     */
99
    public function getDstFolder(): string
100
    {
101
        return $this->dstFolder;
102
    }
103
104
    /**
105
     * Return the model name.
106
     *
107
     * @return string
108
     */
109
    public function getModelName(): string
110
    {
111
        return $this->modelName;
112
    }
113
114
    /**
115
     * Return the table name.
116
     *
117
     * @return string
118
     */
119
    public function getTableName(): string
120
    {
121
        return $this->tableName;
122
    }
123
124
    /**
125
     * Returns an array with tables.
126
     *
127
     * @param DataBase $dataBase
128
     *
129
     * @return array
130
     */
131
    public function getTables(DataBase $dataBase): array
132
    {
133
        return $dataBase->getTables();
134
    }
135
136
    /**
137
     * Returns an array with the columns of a given table.
138
     *
139
     * @param DataBase $dataBase
140
     * @param string   $tableName
141
     *
142
     * @return array
143
     */
144
    public function getItemsFromTable(DataBase $dataBase, string $tableName): array
145
    {
146
        return $dataBase->getColumns($tableName);
147
    }
148
149
    /**
150
     * Returns an array with the constraints of a table.
151
     *
152
     * @param DataBase $dataBase
153
     * @param string   $tableName
154
     *
155
     * @return array
156
     */
157
    public function getConstraintFromTable(DataBase $dataBase, $tableName): array
158
    {
159
        return $dataBase->getConstraints($tableName, true);
160
    }
161
162
    /**
163
     * Return constraints grouped by name.
164
     *
165
     * @return array
166
     */
167
    public function getConstraintsGroupByName(): array
168
    {
169
        $constraint = [];
170
        foreach ($this->getConstraintFromTable($this->dataBase, $this->getTableName()) as $cons) {
171
            if (isset($constraint[$cons['name']])) {
172
                $constraint[$cons['name']]['column_name'] .= ', ' . $cons['column_name'];
173
            } else {
174
                $constraint[$cons['name']] = $cons;
175
            }
176
        }
177
        \ksort($constraint);
178
        return $constraint;
179
    }
180
181
    /**
182
     * Return items from table by type.
183
     *
184
     * @param string $type
185
     *
186
     * @return array
187
     */
188
    public function getItemsFromTableBy(string $type = ''): array
189
    {
190
        $items = [];
191
        foreach ($this->getItemsFromTable($this->dataBase, $this->getTableName()) as $item) {
192
            $colType = $this->parseType($item['type'], 'view');
193
            if (0 === \strpos($colType, $type)) {
194
                $items[] = $item;
195
            }
196
        }
197
        return $items;
198
    }
199
200
    /**
201
     * Return items from table ordered, primary key first.
202
     *
203
     * @return array
204
     */
205
    public function getItemsFromTableOrdered(): array
206
    {
207
        $items = [];
208
        $primaryKey = null;
209
        foreach ($this->getItemsFromTable($this->dataBase, $this->getTableName()) as $item) {
210
            $items[] = $item;
211
            if (0 === \strpos($item['default'], 'nextval')) {
212
                $primaryKey = array_pop($items);
213
            }
214
        }
215
        array_unshift($items, $primaryKey);
216
        return $items;
217
    }
218
219
    /**
220
     * Return a list of tables.
221
     *
222
     * @return string
223
     */
224
    public function getTablesMsg(): string
225
    {
226
        return \implode(', ', $this->getTables($this->dataBase));
227
    }
228
229
    /**
230
     * Convert type to XML/PHP type.
231
     *
232
     * @param string $type
233
     * @param string $usedOn
234
     *
235
     * @return string
236
     */
237
    public function parseType($type = '', string $usedOn = ''): string
238
    {
239
        switch ($usedOn) {
240
            case 'table':
241
                $type = str_replace('varchar', 'character varying', $type);
242
                $type = str_replace('string', 'character varying', $type);
243
                $type = preg_replace('/^double$/', 'double precision', $type);
244
                $type = preg_replace('/^int\(\d+\)/', 'integer', $type);
245
                $type = preg_replace('/tinyint\(1\)/', 'boolean', $type);
246
                $type = preg_replace('/tinyint-$/', 'bool', $type);
247
                $type = preg_replace('/^timestamp$/', 'timestamp without time zone', $type);
248
                break;
249
            case 'php':
250
                $type = preg_replace('/^varchar\(\d+\)/', 'string', $type);
251
                $type = preg_replace('/^character varying\(\d+\)/', 'string', $type);
252
                $type = str_replace('text', 'string', $type);
253
                $type = str_replace('double precision', 'float', $type);
254
                $type = preg_replace('/^int\(\d+\)/', 'int', $type);
255
                $type = preg_replace('/tinyint\(1\)/', 'bool', $type);
256
                $type = preg_replace('/^timestamp$/', 'string', $type);
257
                $type = str_replace('date', 'string', $type);
258
                break;
259
            case 'view':
260
                $type = preg_replace('/^character varying\(\d+\)/', 'text', $type);
261
                $type = str_replace('double precision', 'number-decimal', $type);
262
                $type = str_replace('integer', 'number', $type);
263
                $type = str_replace('boolean', 'checkbox', $type);
264
                $type = preg_replace('/^timestamp$/', 'text', $type);
265
                $type = str_replace('time without time zone', 'text', $type);
266
                $type = str_replace('date', 'datepicker', $type);
267
                break;
268
            case 'xml-view':
269
                $type = preg_replace('/^character varying\(\d+\)/', 'text', $type);
270
                $type = preg_replace('/^varchar\(\d+\)/', 'text', $type);
271
                $type = preg_replace('/^double$/', 'number-decimal', $type);
272
                $type = preg_replace('/^int\(\d+\)/', 'number', $type);
273
                $type = str_replace('boolean', 'checkbox', $type);
274
                $type = preg_replace('/^timestamp$/', 'text', $type);
275
                $type = str_replace('time without time zone', 'text', $type);
276
                $type = str_replace('date', 'datepicker', $type);
277
                break;
278
        }
279
280
        return $type;
281
    }
282
283
    /**
284
     * Add extra fields details.
285
     *
286
     * @param array  $fields
287
     * @param string $prefix
288
     */
289
    public function addExtraFields(array $fields, string $prefix = '')
290
    {
291
        $this->fieldPrefix = $prefix;
292
        foreach ($fields as $field) {
293
            $this->extraFields[$field['name']] = [
294
                'type' => $this->parseType($field['type'], 'table'),
295
                'key' => '',
296
                'default' => $field['default'],
297
                'extra' => '',
298
                'is_nullable' => strtoupper($field['is_nullable']),
299
                'name' => $this->fieldPrefix . $field['name'],
300
                'length' => $field['length'],
301
            ];
302
        }
303
    }
304
305
    /**
306
     * Return a list of fields like getItemsFromTable method.
307
     *
308
     * @param DataBase $dataBase
309
     * @param string   $tableName
310
     *
311
     * @return array
312
     */
313
    public function getExtraItemsFromTable(DataBase $dataBase, string $tableName): array
314
    {
315
        $fields = [];
316
        /// Fill with original fields
317
        $items = $this->getItemsFromTable($dataBase, $tableName);
318
        foreach ($items as $key => $item) {
319
            if (strpos($item['name'], $this->fieldPrefix) !== false) {
320
                $fields[$key] = $item;
321
            }
322
        }
323
        /// Fill with extra fields
324
        foreach ($this->extraFields as $key => $item) {
325
            if (strpos($item['name'], $this->fieldPrefix) !== false) {
326
                $fields[$this->fieldPrefix . $key] = $item;
327
            }
328
        }
329
        return $fields;
330
    }
331
}
332