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 | 3 | 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 | 3 | public function parse($schema) |
|
68 | |||
69 | /** |
||
70 | * Get array of schema. |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 3 | public function getSchemas() |
|
75 | { |
||
76 | 3 | if (is_null($this->schema)) { |
|
77 | return []; |
||
78 | } |
||
79 | |||
80 | 3 | return explode(',', str_replace(' ', '', $this->schema)); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Convert string migration to array. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 3 | public function toArray() |
|
92 | |||
93 | /** |
||
94 | * Render the migration to formatted script. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 2 | public function render() |
|
108 | |||
109 | /** |
||
110 | * Render up migration fields. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 1 | public function up() |
|
118 | |||
119 | /** |
||
120 | * Render down migration fields. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 1 | public function down() |
|
135 | |||
136 | /** |
||
137 | * Create field. |
||
138 | * |
||
139 | * @param string $column |
||
140 | * @param array $attributes |
||
141 | * @param string $type |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | 3 | public function createField($column, $attributes, $type = 'add') |
|
159 | |||
160 | /** |
||
161 | * Add relation column. |
||
162 | * |
||
163 | * @param int $key |
||
164 | * @param string $field |
||
165 | * @param string $column |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | protected function addRelationColumn($key, $field, $column) |
||
177 | |||
178 | /** |
||
179 | * Format field to script. |
||
180 | * |
||
181 | * @param int $key |
||
182 | * @param string $field |
||
183 | * @param string $column |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | 2 | protected function addColumn($key, $field, $column) |
|
203 | |||
204 | /** |
||
205 | * Format field to script. |
||
206 | * |
||
207 | * @param int $key |
||
208 | * @param string $field |
||
209 | * @param string $column |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 1 | protected function removeColumn($key, $field, $column) |
|
221 | |||
222 | /** |
||
223 | * Get column name from schema. |
||
224 | * |
||
225 | * @param string $schema |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 3 | public function getColumn($schema) |
|
233 | |||
234 | /** |
||
235 | * Get column attributes. |
||
236 | * |
||
237 | * @param string $column |
||
238 | * @param string $schema |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | 3 | public function getAttributes($column, $schema) |
|
248 | |||
249 | /** |
||
250 | * Determine whether the given column is exist in customAttributes array. |
||
251 | * |
||
252 | * @param string $column |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | 3 | public function hasCustomAttribute($column) |
|
260 | |||
261 | /** |
||
262 | * Get custom attributes value. |
||
263 | * |
||
264 | * @param string $column |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function getCustomAttribute($column) |
||
272 | } |
||
273 |