22
33namespace Yajra \DataTables \Services ;
44
5+ use Closure ;
56use Generator ;
67use Illuminate \Contracts \Support \Renderable ;
78use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
@@ -223,6 +224,7 @@ public function ajax(): JsonResponse
223224 {
224225 $ query = null ;
225226 if (method_exists ($ this , 'query ' )) {
227+ /** @var EloquentBuilder|QueryBuilder $query */
226228 $ query = app ()->call ([$ this , 'query ' ]);
227229 $ query = $ this ->applyScopes ($ query );
228230 }
@@ -332,7 +334,7 @@ public function builder(): Builder
332334 }
333335
334336 if (method_exists ($ this , 'htmlBuilder ' )) {
335- return $ this ->htmlBuilder = app ()-> call ([ $ this , ' htmlBuilder ' ] );
337+ return $ this ->htmlBuilder = $ this -> htmlBuilder ( );
336338 }
337339
338340 return $ this ->htmlBuilder = app ('datatables.html ' );
@@ -366,7 +368,10 @@ protected function getAjaxResponseData(): array
366368 'length ' => -1 ,
367369 ]);
368370
371+ /** @var JsonResponse $response */
369372 $ response = app ()->call ([$ this , 'ajax ' ]);
373+
374+ /** @var array{data: array} $data */
370375 $ data = $ response ->getData (true );
371376
372377 return $ data ['data ' ];
@@ -434,18 +439,24 @@ public function excel()
434439 {
435440 set_time_limit (3600 );
436441
437- $ ext = '. ' .strtolower ($ this ->excelWriter );
438- $ callback = $ this ->fastExcel ?
439- ($ this ->fastExcelCallback ? $ this ->fastExcelCallback () : null )
440- : $ this ->excelWriter ;
442+ $ path = $ this ->getFilename ().'. ' .strtolower ($ this ->excelWriter );
443+
444+ $ excelFile = $ this ->buildExcelFile ();
445+
446+ if ($ excelFile instanceof FastExcel) {
447+ $ callback = $ this ->fastExcelCallback ? $ this ->fastExcelCallback () : null ;
448+
449+ return $ excelFile ->download ($ path , $ callback );
450+ }
441451
442- return $ this ->buildExcelFile ()->download ($ this ->getFilename ().$ ext , $ callback );
452+ // @phpstan-ignore-next-line
453+ return $ excelFile ->download ($ path , $ this ->excelWriter );
443454 }
444455
445456 /**
446457 * Build Excel file and prepare for export.
447458 *
448- * @return mixed|\Rap2hpoutre\FastExcel\ FastExcel
459+ * @return mixed|FastExcel
449460 * @throws \Exception
450461 */
451462 protected function buildExcelFile ()
@@ -517,7 +528,7 @@ protected function getDataForExport(): array
517528 /**
518529 * Get export columns definition.
519530 *
520- * @return Collection
531+ * @return Collection<int, Column>
521532 */
522533 protected function exportColumns (): Collection
523534 {
@@ -552,24 +563,30 @@ private function toColumnsCollection(array $columns): Collection
552563 /**
553564 * Export results to CSV file.
554565 *
555- * @return string|\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\ StreamedResponse
566+ * @return string|\Symfony\Component\HttpFoundation\StreamedResponse
556567 * @throws \Exception
557568 */
558569 public function csv ()
559570 {
560571 set_time_limit (3600 );
561- $ ext = '. ' .strtolower ($ this ->csvWriter );
562- $ callback = $ this ->fastExcel ?
563- ($ this ->fastExcelCallback ? $ this ->fastExcelCallback () : null )
564- : $ this ->csvWriter ;
572+ $ path = $ this ->getFilename ().'. ' .strtolower ($ this ->excelWriter );
573+
574+ $ excelFile = $ this ->buildExcelFile ();
565575
566- return $ this ->buildExcelFile ()->download ($ this ->getFilename ().$ ext , $ callback );
576+ if ($ excelFile instanceof FastExcel) {
577+ $ callback = $ this ->fastExcelCallback ? $ this ->fastExcelCallback () : null ;
578+
579+ return $ excelFile ->download ($ path , $ callback );
580+ }
581+
582+ // @phpstan-ignore-next-line
583+ return $ this ->buildExcelFile ()->download ($ path , $ this ->csvWriter );
567584 }
568585
569586 /**
570587 * Export results to PDF file.
571588 *
572- * @return \Illuminate\Http\Response|string|\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\ StreamedResponse
589+ * @return \Illuminate\Http\Response|string|\Symfony\Component\HttpFoundation\StreamedResponse
573590 * @throws \Box\Spout\Common\Exception\IOException
574591 * @throws \Box\Spout\Common\Exception\InvalidArgumentException
575592 * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
@@ -582,6 +599,7 @@ public function pdf()
582599 return $ this ->snappyPdf ();
583600 }
584601
602+ // @phpstan-ignore-next-line
585603 return $ this ->buildExcelFile ()->download ($ this ->getFilename ().'.pdf ' , $ this ->pdfWriter );
586604 }
587605
@@ -594,7 +612,9 @@ public function snappyPdf(): Response
594612 {
595613 /** @var \Barryvdh\Snappy\PdfWrapper $snappy */
596614 $ snappy = app ('snappy.pdf.wrapper ' );
597- $ options = config ('datatables-buttons.snappy.options ' );
615+ $ options = (array ) config ('datatables-buttons.snappy.options ' );
616+
617+ /** @var string $orientation */
598618 $ orientation = config ('datatables-buttons.snappy.orientation ' );
599619
600620 $ snappy ->setOptions ($ options )->setOrientation ($ orientation );
@@ -721,15 +741,16 @@ protected function convertToLazyCollection(array|Collection $collection): LazyCo
721741 /**
722742 * @return \Closure
723743 */
724- public function fastExcelCallback (): \ Closure
744+ public function fastExcelCallback (): Closure
725745 {
726746 return function ($ row ) {
727747 $ mapped = [];
728- foreach ($ this ->exportColumns () as $ column ) {
748+
749+ $ this ->exportColumns ()->each (function (Column $ column ) use (&$ mapped , $ row ) {
729750 if ($ column ['exportable ' ]) {
730751 $ mapped [$ column ['title ' ]] = $ row [$ column ['data ' ]];
731752 }
732- }
753+ });
733754
734755 return $ mapped ;
735756 };
@@ -742,6 +763,7 @@ protected function buildFastExcelFile(): FastExcel
742763 {
743764 $ query = null ;
744765 if (method_exists ($ this , 'query ' )) {
766+ /** @var EloquentBuilder|QueryBuilder $query */
745767 $ query = app ()->call ([$ this , 'query ' ]);
746768 $ query = $ this ->applyScopes ($ query );
747769 }
0 commit comments