1 | <?php |
||
7 | class SchemaParser implements Arrayable |
||
8 | { |
||
9 | /** |
||
10 | * The array of custom attributes. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $customAttributes = [ |
||
15 | 'remember_token' => 'rememberToken()', |
||
16 | 'soft_delete' => 'softDeletes()', |
||
17 | ]; |
||
18 | |||
19 | /** |
||
20 | * The migration schema. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $schema; |
||
25 | |||
26 | /** |
||
27 | * The relationship keys. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $relationshipKeys = [ |
||
32 | 'belongsTo', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Create new instance. |
||
37 | * |
||
38 | * @param string|null $schema |
||
39 | */ |
||
40 | 9 | public function __construct($schema = null) |
|
44 | |||
45 | /** |
||
46 | * Parse a string to array of formatted schema. |
||
47 | * |
||
48 | * @param string $schema |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | 9 | public function parse($schema) |
|
53 | { |
||
54 | 9 | $this->schema = $schema; |
|
55 | |||
56 | 9 | $parsed = []; |
|
57 | |||
58 | 9 | foreach ($this->getSchemas() as $schemaArray) { |
|
59 | 3 | $column = $this->getColumn($schemaArray); |
|
60 | |||
61 | 3 | $attributes = $this->getAttributes($column, $schemaArray); |
|
62 | |||
63 | 3 | $parsed[$column] = $attributes; |
|
64 | 9 | } |
|
65 | |||
66 | 9 | return $parsed; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get array of schema. |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 9 | public function getSchemas() |
|
82 | |||
83 | /** |
||
84 | * Convert string migration to array. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 9 | public function toArray() |
|
92 | |||
93 | /** |
||
94 | * Render the migration to formatted script. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 8 | public function render() |
|
99 | { |
||
100 | 8 | $results = ''; |
|
101 | |||
102 | 8 | foreach ($this->toArray() as $column => $attributes) { |
|
103 | 2 | $results .= $this->createField($column, $attributes); |
|
104 | 8 | } |
|
105 | |||
106 | 8 | return $results; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Render up migration fields. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 3 | public function up() |
|
118 | |||
119 | /** |
||
120 | * Render down migration fields. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 3 | public function down() |
|
125 | { |
||
126 | 3 | $results = ''; |
|
127 | |||
128 | 3 | foreach ($this->toArray() as $column => $attributes) { |
|
129 | 1 | $attributes = [head($attributes)]; |
|
130 | 1 | $results .= $this->createField($column, $attributes, 'remove'); |
|
131 | 3 | } |
|
132 | |||
133 | 3 | return $results; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Create field. |
||
138 | * |
||
139 | * @param string $column |
||
140 | * @param array $attributes |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 3 | public function createField($column, $attributes, $type = 'add') |
|
145 | { |
||
146 | 3 | $results = "\t\t\t" . '$table'; |
|
147 | |||
148 | 3 | foreach ($attributes as $key => $field) { |
|
149 | 3 | if (in_array($column, $this->relationshipKeys)) { |
|
150 | $results .= $this->addRelationColumn($key, $field, $column); |
||
151 | } else { |
||
152 | 3 | $results .= $this->{"{$type}Column"}($key, $field, $column); |
|
153 | } |
||
154 | 3 | } |
|
155 | |||
156 | 3 | return $results .= ';' . PHP_EOL; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Add relation column. |
||
161 | * |
||
162 | * @param int $key |
||
163 | * @param string $field |
||
164 | * @param string $column |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | protected function addRelationColumn($key, $field, $column) |
||
176 | |||
177 | /** |
||
178 | * Format field to script. |
||
179 | * |
||
180 | * @param int $key |
||
181 | * @param string $field |
||
182 | * @param string $column |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 2 | protected function addColumn($key, $field, $column) |
|
202 | |||
203 | /** |
||
204 | * Format field to script. |
||
205 | * |
||
206 | * @param int $key |
||
207 | * @param string $field |
||
208 | * @param string $column |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | 1 | protected function removeColumn($key, $field, $column) |
|
220 | |||
221 | /** |
||
222 | * Get column name from schema. |
||
223 | * |
||
224 | * @param string $schema |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 3 | public function getColumn($schema) |
|
232 | |||
233 | /** |
||
234 | * Get column attributes. |
||
235 | * |
||
236 | * @param string $column |
||
237 | * @param string $schema |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | 3 | public function getAttributes($column, $schema) |
|
247 | |||
248 | /** |
||
249 | * Determine whether the given column is exist in customAttributes array. |
||
250 | * |
||
251 | * @param string $column |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 3 | public function hasCustomAttribute($column) |
|
259 | |||
260 | /** |
||
261 | * Get custom attributes value. |
||
262 | * |
||
263 | * @param string $column |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function getCustomAttribute($column) |
||
271 | } |
||
272 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.