66use Botble \DataSynchronize \Contracts \Importer \WithMapping ;
77use Botble \DataSynchronize \DataTransferObjects \ChunkImportResponse ;
88use Botble \DataSynchronize \DataTransferObjects \ChunkValidateResponse ;
9- use Botble \DataSynchronize \Exporter \ExportColumn ;
10- use Botble \DataSynchronize \ Exporter \ Exporter ;
9+ use Botble \DataSynchronize \Exporter \ExampleExporter ;
10+ use Botble \Media \ Facades \ RvMedia ;
1111use Illuminate \Contracts \Filesystem \FileNotFoundException ;
1212use Illuminate \Contracts \Filesystem \Filesystem ;
1313use Illuminate \Contracts \View \View ;
14- use Illuminate \Support \Collection ;
14+ use Illuminate \Support \Facades \ Log ;
1515use Illuminate \Support \Facades \Storage ;
1616use Illuminate \Support \Facades \Validator ;
1717use Illuminate \Support \LazyCollection ;
18+ use Illuminate \Support \Str ;
1819use Spatie \SimpleExcel \SimpleExcelReader ;
20+ use Symfony \Component \HttpFoundation \BinaryFileResponse ;
1921
2022abstract class Importer
2123{
24+ protected bool $ isValidating = false ;
25+
2226 abstract public function columns (): array ;
2327
2428 abstract public function getValidateUrl (): string ;
@@ -112,6 +116,8 @@ public function render(): View
112116
113117 public function validate (string $ fileName , int $ offset = 0 , int $ limit = 100 ): ChunkValidateResponse
114118 {
119+ $ this ->isValidating = true ;
120+
115121 $ rows = $ this ->transformRows ($ this ->getRowsByOffset ($ fileName , $ offset , $ limit ));
116122
117123 $ total = request ()->integer ('total ' ) ?: $ this ->getRows ($ fileName )->count ();
@@ -176,7 +182,7 @@ public function getDoneMessage(int $count): string
176182 {
177183 return trans ('packages/data-synchronize::data-synchronize.import.done_message ' , [
178184 'count ' => number_format ($ count ),
179- 'label ' => $ this ->getLabel (),
185+ 'label ' => strtolower ( $ this ->getLabel () ),
180186 ]);
181187 }
182188
@@ -214,13 +220,11 @@ public function transformRows(array $rows): array
214220 ->mapWithKeys (function (ImportColumn $ column ) use ($ row ) {
215221 $ value = $ row [$ column ->getName ()] ?? null ;
216222
217- if ($ column ->isNullable () && empty ($ value )) {
218- return [$ column ->getName () => null ];
219- }
220-
221- if ($ column ->isBoolean () && is_string ($ value )) {
222- $ value = $ value === $ column ->getTrueValue () ? 1 : 0 ;
223- }
223+ $ value = match (true ) {
224+ $ column ->isNullable () && empty ($ value ) => null ,
225+ $ column ->isBoolean () && is_string ($ value ) => $ value === $ column ->getTrueValue () ? 1 : 0 ,
226+ default => $ value ,
227+ };
224228
225229 return [$ column ->getName () => $ value ];
226230 })
@@ -244,36 +248,31 @@ public function filesystem(): Filesystem
244248 return Storage::disk (config ('packages.data-synchronize.data-synchronize.storage.disk ' ));
245249 }
246250
247- public function downloadExample (string $ format )
251+ public function downloadExample (string $ format ): BinaryFileResponse
248252 {
249- $ examples = $ this ->getExamples ();
250253 $ columns = $ this ->getColumns ();
251- $ label = $ this ->getLabel ();
252-
253- $ exporter = new class ($ examples , $ columns , $ label ) extends Exporter {
254- public function __construct (protected array $ examples , protected array $ columns , protected string $ label )
255- {
256- }
257-
258- public function columns (): array
259- {
260- return array_map (fn (ImportColumn $ item ) => ExportColumn::make ($ item ->getName ())->label ($ item ->getLabel ()), $ this ->columns );
261- }
262-
263- public function getExportFileName (): string
264- {
265- return sprintf ('%s-example ' , str ($ this ->label )->trim ()->replace (' ' , '- ' ));
266- }
267-
268- public function collection (): Collection
269- {
270- return collect ($ this ->examples )->map (fn ($ item ) => (object ) $ item );
271- }
272- };
254+ $ exporter = (new ExampleExporter ($ this ->getExamples (), $ columns , $ this ->getLabel ()));
273255
274256 return $ exporter
275257 ->format ($ format )
276258 ->acceptedColumns (array_map (fn (ImportColumn $ column ) => $ column ->getName (), $ columns ))
277259 ->export ();
278260 }
261+
262+ protected function resolveMediaImage (string $ url , ?string $ directory = null ): string
263+ {
264+ if (! Str::startsWith ($ url , ['http:// ' , 'https:// ' ])) {
265+ return $ url ;
266+ }
267+
268+ $ result = RvMedia::uploadFromUrl ($ url , 0 , $ directory );
269+
270+ if ($ result ['error ' ]) {
271+ Log::error ($ result ['message ' ]);
272+
273+ return $ url ;
274+ }
275+
276+ return $ result ['data ' ]->url ;
277+ }
279278}
0 commit comments