1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yeelight\Generators; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class TransformerGenerator |
7
|
|
|
* |
8
|
|
|
* @category Yeelight |
9
|
|
|
* |
10
|
|
|
* @package Yeelight\Generators |
11
|
|
|
* |
12
|
|
|
* @author Sheldon Lee <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
15
|
|
|
* |
16
|
|
|
* @link https://www.yeelight.com |
17
|
|
|
*/ |
18
|
|
|
class TransformerGenerator extends Generator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Get stub name. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $stub = 'transformer/transformer'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get root namespace. |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function getRootNamespace() |
33
|
|
|
{ |
34
|
|
|
return parent::getRootNamespace() . |
35
|
|
|
parent::getConfigGeneratorClassPath($this->getPathConfigNode()); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get generator path config node. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getPathConfigNode() |
44
|
|
|
{ |
45
|
|
|
return 'transformers'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get destination path for generated file. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getPath() |
54
|
|
|
{ |
55
|
|
|
return $this->getBasePath() . |
56
|
|
|
'/' . |
57
|
|
|
parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . |
|
|
|
|
58
|
|
|
'/' . |
59
|
|
|
$this->getName() . |
60
|
|
|
'Transformer.php'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get base path of destination file. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getBasePath() |
69
|
|
|
{ |
70
|
|
|
return config('repository.generator.basePath', app_path()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get default Columns description. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getColumns() |
79
|
|
|
{ |
80
|
|
|
$fields = $this->fields; |
|
|
|
|
81
|
|
|
$result = ''; |
82
|
|
|
if (!empty($fields)) { |
83
|
|
|
foreach ($fields as $index => $field) { |
84
|
|
|
$type = $this->getTypeFromField($field); |
85
|
|
|
$result .= "\t\t\t'{$field['name']}' => ($type) ".'$model->'."{$field['name']},\r\n"; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$result .= ''; |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getTypeFromField($filed) |
94
|
|
|
{ |
95
|
|
|
switch ($filed['type']) { |
96
|
|
|
case 'string': |
97
|
|
|
case 'text': |
98
|
|
|
case 'date': |
99
|
|
|
case 'time': |
100
|
|
|
case 'dateTime': |
101
|
|
|
case 'timestamp': |
102
|
|
|
case 'char': |
103
|
|
|
case 'mediumText': |
104
|
|
|
case 'longText': |
105
|
|
|
case 'enum': |
106
|
|
|
case 'json': |
107
|
|
|
case 'jsonb': |
108
|
|
|
case 'dateTimeTz': |
109
|
|
|
case 'timeTz': |
110
|
|
|
case 'timestampTz': |
111
|
|
|
case 'nullableTimestamps': |
112
|
|
|
case 'binary': |
113
|
|
|
case 'ipAddress': |
114
|
|
|
case 'macAddress': |
115
|
|
|
return 'string'; |
116
|
|
|
|
117
|
|
|
case 'integer': |
118
|
|
|
case 'tinyInteger': |
119
|
|
|
case 'smallInteger': |
120
|
|
|
case 'mediumInteger': |
121
|
|
|
case 'bigInteger': |
122
|
|
|
case 'unsignedTinyInteger': |
123
|
|
|
case 'unsignedSmallInteger': |
124
|
|
|
case 'unsignedMediumInteger': |
125
|
|
|
case 'unsignedInteger': |
126
|
|
|
case 'unsignedBigInteger': |
127
|
|
|
return 'int'; |
128
|
|
|
|
129
|
|
|
case 'float': |
130
|
|
|
case 'decimal': |
131
|
|
|
return 'float'; |
132
|
|
|
|
133
|
|
|
case 'double': |
134
|
|
|
return 'double'; |
135
|
|
|
|
136
|
|
|
case 'boolean': |
137
|
|
|
return 'boolean'; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get array replacements. |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function getReplacements() |
147
|
|
|
{ |
148
|
|
|
$modelGenerator = new ModelGenerator( |
149
|
|
|
[ |
150
|
|
|
'name' => $this->name, |
|
|
|
|
151
|
|
|
] |
152
|
|
|
); |
153
|
|
|
$model = $modelGenerator->getRootNamespace().'\\'.$modelGenerator->getName(); |
154
|
|
|
$model = str_replace([ |
155
|
|
|
'\\', |
156
|
|
|
'/', |
157
|
|
|
], '\\', $model); |
158
|
|
|
|
159
|
|
|
return array_merge( |
160
|
|
|
parent::getReplacements(), |
161
|
|
|
[ |
162
|
|
|
'model' => $model, |
163
|
|
|
'transformer_fields' => $this->getColumns(), |
164
|
|
|
'_id_name' => $this->getIdName(), |
165
|
|
|
] |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|