base-api-scramble-mcp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,459 @@
1
+ # Export Templates Documentation
2
+
3
+ Template-template export Excel yang fleksibel dan tidak bergantung pada model Laravel. Semua data harus di-mapping terlebih dahulu sebelum export.
4
+
5
+ ## 🎯 Export Templates
6
+
7
+ ### 1. FlexibleExport Template
8
+
9
+ Template untuk export single sheet dengan mapping data yang fleksibel.
10
+
11
+ **Features:**
12
+ - ✅ Support array data tanpa model
13
+ - ✅ Custom data mapping dengan callback functions
14
+ - ✅ Dot notation untuk nested data
15
+ - ✅ Custom styling dan column widths
16
+ - ✅ Auto-size columns
17
+
18
+ **Usage:**
19
+ ```php
20
+ use App\Exports\Templates\FlexibleExport;
21
+
22
+ // Sample data
23
+ $rawData = [
24
+ ['id' => 1, 'name' => 'John', 'email' => 'john@test.com', 'created_at' => '2024-01-15'],
25
+ ['id' => 2, 'name' => 'Jane', 'email' => 'jane@test.com', 'created_at' => '2024-01-16'],
26
+ ];
27
+
28
+ $headers = ['ID', 'Full Name', 'Email', 'Join Date'];
29
+
30
+ // Data mapper dengan callback
31
+ $dataMapper = [
32
+ 'id',
33
+ 'name',
34
+ 'email',
35
+ function ($item) {
36
+ return date('d/m/Y', strtotime($item['created_at']));
37
+ }
38
+ ];
39
+
40
+ $export = FlexibleExport::create($rawData, $headers, [
41
+ 'sheet_title' => 'Users',
42
+ 'report_title' => 'User Report',
43
+ 'data_mapper' => $dataMapper,
44
+ 'column_widths' => ['A' => 10, 'B' => 20, 'C' => 30, 'D' => 15]
45
+ ]);
46
+
47
+ return $export->download();
48
+ ```
49
+
50
+ ### 2. MultiSheetExport Template
51
+
52
+ Template untuk export multiple sheets dengan data mapping.
53
+
54
+ **Usage:**
55
+ ```php
56
+ use App\Exports\Templates\MultiSheetExport;
57
+
58
+ $sheets = [
59
+ [
60
+ 'title' => 'Users',
61
+ 'headers' => ['ID', 'Name', 'Status'],
62
+ 'data' => $usersData,
63
+ 'data_mapper' => [
64
+ 'id',
65
+ 'name',
66
+ function ($item) {
67
+ return $item['active'] ? 'Active' : 'Inactive';
68
+ }
69
+ ]
70
+ ],
71
+ [
72
+ 'title' => 'Posts',
73
+ 'headers' => ['ID', 'Title', 'Author'],
74
+ 'data' => $postsData,
75
+ 'data_mapper' => ['id', 'title', 'author_name']
76
+ ]
77
+ ];
78
+
79
+ $export = MultiSheetExport::create($sheets, [
80
+ 'report_title' => 'Complete Report',
81
+ 'include_summary' => true
82
+ ]);
83
+
84
+ return $export->download();
85
+ ```
86
+
87
+ ### 3. MultiTableExport Template
88
+
89
+ Template untuk export multiple tables dalam satu sheet.
90
+
91
+ **Usage:**
92
+ ```php
93
+ use App\Exports\Templates\MultiTableExport;
94
+
95
+ $tables = [
96
+ [
97
+ 'title' => 'Summary Statistics',
98
+ 'headers' => ['Metric', 'Value'],
99
+ 'data' => $summaryData,
100
+ 'data_mapper' => [
101
+ 'metric',
102
+ function ($item) {
103
+ return number_format($item['value']);
104
+ }
105
+ ]
106
+ ],
107
+ [
108
+ 'title' => 'Top Users',
109
+ 'headers' => ['Name', 'Score'],
110
+ 'data' => $topUsersData,
111
+ 'data_mapper' => ['name', 'score']
112
+ ]
113
+ ];
114
+
115
+ $export = MultiTableExport::create($tables, 'Dashboard');
116
+ return $export->download();
117
+ ```
118
+
119
+ ## 📡 API Endpoints
120
+
121
+ ### Template Endpoints (Tanpa Model Dependency)
122
+
123
+ ```bash
124
+ # Flexible single sheet template
125
+ GET /api/v1/export/flexible-template
126
+
127
+ # Multi-sheet template
128
+ GET /api/v1/export/multi-sheet-template
129
+
130
+ # Multi-table template
131
+ GET /api/v1/export/multi-table-template
132
+
133
+ # External API data template
134
+ GET /api/v1/export/external-data-template
135
+
136
+ # Custom styling template
137
+ GET /api/v1/export/custom-style-template
138
+ ```
139
+
140
+ ### Model-Based Endpoints (Dengan Mapping)
141
+
142
+ ```bash
143
+ # Users export dengan mapping
144
+ GET /api/v1/export/users-with-mapping?search=john&verified=true
145
+ ```
146
+
147
+ ## 🔧 Data Mapping Examples
148
+
149
+ ### 1. Simple Field Mapping
150
+
151
+ ```php
152
+ $dataMapper = [
153
+ 'id', // ambil field 'id' langsung
154
+ 'name', // ambil field 'name' langsung
155
+ 'email' // ambil field 'email' langsung
156
+ ];
157
+ ```
158
+
159
+ ### 2. Callback Function Mapping
160
+
161
+ ```php
162
+ $dataMapper = [
163
+ 'id',
164
+ function ($item) {
165
+ return strtoupper($item['name']); // format nama jadi uppercase
166
+ },
167
+ function ($item) {
168
+ return $item['is_active'] ? 'Active ✓' : 'Inactive ✗';
169
+ },
170
+ function ($item) {
171
+ return date('d/m/Y H:i', strtotime($item['created_at']));
172
+ }
173
+ ];
174
+ ```
175
+
176
+ ### 3. Nested Data Mapping (Dot Notation)
177
+
178
+ ```php
179
+ // Data structure:
180
+ $data = [
181
+ 'user_id' => 1,
182
+ 'profile' => [
183
+ 'first_name' => 'John',
184
+ 'contact' => [
185
+ 'email' => 'john@example.com'
186
+ ]
187
+ ]
188
+ ];
189
+
190
+ $dataMapper = [
191
+ 'user_id',
192
+ 'profile.first_name', // dot notation untuk nested
193
+ 'profile.contact.email', // nested level 2
194
+ function ($item) {
195
+ return $item['profile']['first_name'] . ' ' . $item['profile']['last_name'];
196
+ }
197
+ ];
198
+ ```
199
+
200
+ ### 4. Complex API Data Mapping
201
+
202
+ ```php
203
+ // Data dari external API
204
+ $apiData = [
205
+ 'id' => 'ext_001',
206
+ 'user_info' => [
207
+ 'name' => 'John Doe',
208
+ 'stats' => [
209
+ 'orders' => 25,
210
+ 'total_spent' => 1250.75
211
+ ]
212
+ ],
213
+ 'created' => '2024-01-15T10:30:00Z'
214
+ ];
215
+
216
+ $dataMapper = [
217
+ 'id',
218
+ 'user_info.name',
219
+ function ($item) {
220
+ return number_format($item['user_info']['stats']['orders']) . ' orders';
221
+ },
222
+ function ($item) {
223
+ return '$' . number_format($item['user_info']['stats']['total_spent'], 2);
224
+ },
225
+ function ($item) {
226
+ return date('d/m/Y', strtotime($item['created']));
227
+ }
228
+ ];
229
+ ```
230
+
231
+ ## 🎨 Custom Styling Examples
232
+
233
+ ### 1. Header Styling
234
+
235
+ ```php
236
+ $customHeaderStyle = [
237
+ 'font' => [
238
+ 'bold' => true,
239
+ 'size' => 14,
240
+ 'color' => ['rgb' => 'FFFFFF'],
241
+ ],
242
+ 'fill' => [
243
+ 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
244
+ 'startColor' => ['rgb' => '4CAF50'],
245
+ ],
246
+ 'borders' => [
247
+ 'allBorders' => [
248
+ 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
249
+ 'color' => ['rgb' => '000000'],
250
+ ],
251
+ ],
252
+ ];
253
+ ```
254
+
255
+ ### 2. Body Styling
256
+
257
+ ```php
258
+ $customBodyStyle = [
259
+ 'font' => ['size' => 11, 'name' => 'Calibri'],
260
+ 'borders' => [
261
+ 'allBorders' => [
262
+ 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
263
+ 'color' => ['rgb' => 'CCCCCC'],
264
+ ],
265
+ ],
266
+ 'alignment' => [
267
+ 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
268
+ ],
269
+ ];
270
+ ```
271
+
272
+ ## 🚀 Implementation Examples
273
+
274
+ ### 1. Export Data dari Database Query
275
+
276
+ ```php
277
+ // Controller method
278
+ public function exportCustomData(Request $request)
279
+ {
280
+ // Query database
281
+ $users = DB::table('users')
282
+ ->join('profiles', 'users.id', '=', 'profiles.user_id')
283
+ ->select('users.*', 'profiles.phone', 'profiles.address')
284
+ ->where('users.active', true)
285
+ ->get()
286
+ ->toArray();
287
+
288
+ $headers = ['ID', 'Name', 'Email', 'Phone', 'Address', 'Status'];
289
+
290
+ $dataMapper = [
291
+ 'id',
292
+ 'name',
293
+ 'email',
294
+ 'phone',
295
+ 'address',
296
+ function ($item) {
297
+ return 'Active ✓';
298
+ }
299
+ ];
300
+
301
+ $export = FlexibleExport::create($users, $headers, [
302
+ 'sheet_title' => 'Active Users',
303
+ 'data_mapper' => $dataMapper
304
+ ]);
305
+
306
+ return $export->download();
307
+ }
308
+ ```
309
+
310
+ ### 2. Export Data dari External API
311
+
312
+ ```php
313
+ public function exportExternalData(Request $request)
314
+ {
315
+ // Fetch dari API
316
+ $response = Http::get('https://api.example.com/users');
317
+ $apiData = $response->json()['data'];
318
+
319
+ $headers = ['ID', 'Name', 'Email', 'Country', 'Total Orders'];
320
+
321
+ $dataMapper = [
322
+ 'user_id',
323
+ 'profile.full_name',
324
+ 'contact.email',
325
+ 'location.country',
326
+ function ($item) {
327
+ return number_format($item['stats']['total_orders']);
328
+ }
329
+ ];
330
+
331
+ $export = FlexibleExport::create($apiData, $headers, [
332
+ 'sheet_title' => 'API Users',
333
+ 'report_title' => 'External Users Report',
334
+ 'data_mapper' => $dataMapper
335
+ ]);
336
+
337
+ return $export->download();
338
+ }
339
+ ```
340
+
341
+ ### 3. Export Multiple Data Sources
342
+
343
+ ```php
344
+ public function exportCombinedData(Request $request)
345
+ {
346
+ // Data dari berbagai sumber
347
+ $usersData = User::all()->toArray();
348
+ $ordersData = Order::with('user')->get()->toArray();
349
+ $statsData = [
350
+ ['metric' => 'Total Users', 'value' => count($usersData)],
351
+ ['metric' => 'Total Orders', 'value' => count($ordersData)],
352
+ ];
353
+
354
+ $sheets = [
355
+ [
356
+ 'title' => 'Users',
357
+ 'headers' => ['ID', 'Name', 'Email', 'Join Date'],
358
+ 'data' => $usersData,
359
+ 'data_mapper' => [
360
+ 'id', 'name', 'email',
361
+ function ($item) {
362
+ return date('d/m/Y', strtotime($item['created_at']));
363
+ }
364
+ ]
365
+ ],
366
+ [
367
+ 'title' => 'Orders',
368
+ 'headers' => ['Order ID', 'Customer', 'Total', 'Date'],
369
+ 'data' => $ordersData,
370
+ 'data_mapper' => [
371
+ 'id',
372
+ 'user.name',
373
+ function ($item) {
374
+ return '$' . number_format($item['total'], 2);
375
+ },
376
+ function ($item) {
377
+ return date('d/m/Y', strtotime($item['created_at']));
378
+ }
379
+ ]
380
+ ]
381
+ ];
382
+
383
+ $export = MultiSheetExport::create($sheets, [
384
+ 'report_title' => 'Business Report',
385
+ 'include_summary' => true
386
+ ]);
387
+
388
+ return $export->download();
389
+ }
390
+ ```
391
+
392
+ ## âš¡ Performance Tips
393
+
394
+ 1. **Use Chunking untuk Data Besar**
395
+ ```php
396
+ // Service akan otomatis chunk per 1000 records
397
+ // Bisa diatur di config/export_info.php
398
+ ```
399
+
400
+ 2. **Selective Field Selection**
401
+ ```php
402
+ // Hanya ambil field yang dibutuhkan
403
+ $data = User::select(['id', 'name', 'email'])->get()->toArray();
404
+ ```
405
+
406
+ 3. **Memory Management**
407
+ ```php
408
+ // Set di .env
409
+ EXPORT_MEMORY_LIMIT=1024M
410
+ EXPORT_MAX_EXECUTION_TIME=600
411
+ ```
412
+
413
+ ## 🔥 Advanced Usage
414
+
415
+ ### Custom Template Class
416
+
417
+ ```php
418
+ <?php
419
+
420
+ namespace App\Exports\Custom;
421
+
422
+ use App\Exports\Templates\FlexibleExport;
423
+
424
+ class SalesReportExport extends FlexibleExport
425
+ {
426
+ public function __construct(array $salesData, string $period)
427
+ {
428
+ $headers = ['Date', 'Product', 'Quantity', 'Revenue', 'Profit Margin'];
429
+
430
+ $dataMapper = [
431
+ function ($item) {
432
+ return date('d/m/Y', strtotime($item['sale_date']));
433
+ },
434
+ 'product_name',
435
+ 'quantity',
436
+ function ($item) {
437
+ return '$' . number_format($item['revenue'], 2);
438
+ },
439
+ function ($item) {
440
+ $margin = (($item['revenue'] - $item['cost']) / $item['revenue']) * 100;
441
+ return number_format($margin, 1) . '%';
442
+ }
443
+ ];
444
+
445
+ parent::__construct([
446
+ 'data' => $salesData,
447
+ 'headers' => $headers,
448
+ 'sheet_title' => 'Sales Report',
449
+ 'report_title' => "Sales Report - {$period}",
450
+ 'data_mapper' => $dataMapper,
451
+ 'column_widths' => ['A' => 12, 'B' => 25, 'C' => 10, 'D' => 15, 'E' => 15]
452
+ ]);
453
+ }
454
+ }
455
+
456
+ // Usage
457
+ $export = new SalesReportExport($salesData, 'January 2024');
458
+ return $export->download();
459
+ ```