89 lines
2.8 KiB
PHP
89 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Sofascore;
|
|
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
trait GrappingToExcel
|
|
{
|
|
public static $max = 2000;
|
|
|
|
public function import_to_excel(string $folder, $title = '')
|
|
{
|
|
ini_set('memory_limit', -1);
|
|
|
|
if ($this->debug) {
|
|
Notice::note("import $folder to excel", 'g');
|
|
}
|
|
|
|
if (! $title) {
|
|
$titel = ucfirst($folder).'_'.Carbon::now()->format('Y-m-d');
|
|
}
|
|
|
|
$files = $this->get_tree("$this->data_dir/$folder");
|
|
|
|
$files = array_chunk($this->filter($files), self::$max);
|
|
|
|
foreach ($files as $key => $value) {
|
|
|
|
$has = count($value);
|
|
if (! $key && $has < self::$max) {
|
|
$count_from = 1;
|
|
$count_to = $has;
|
|
} elseif (! $key) {
|
|
$count_from = 1;
|
|
$key++;
|
|
$count_to = $key * self::$max;
|
|
} elseif ($has == self::$max) {
|
|
$count_from = $key * self::$max + 1;
|
|
$key++;
|
|
$count_to = $key * self::$max;
|
|
} else {
|
|
$count_from = $key * self::$max + 1;
|
|
$count_to = $key * self::$max + $has;
|
|
}
|
|
|
|
$spreadsheet = new Spreadsheet;
|
|
$spreadsheet->getProperties()->setTitle("$title-$count_from-$count_to");
|
|
$activeWorksheet = $spreadsheet->getActiveSheet();
|
|
|
|
$column = 1;
|
|
foreach ($value as $file) {
|
|
$data = file_get_contents($file);
|
|
$data = json_decode($data, true);
|
|
$json_string = json_encode($data, JSON_PRETTY_PRINT);
|
|
|
|
$arr = explode(PHP_EOL, $json_string);
|
|
$row = 0;
|
|
foreach ($arr as $string) {
|
|
$row++;
|
|
$replacedString = preg_replace('/\\\\u([0-9abcdef]{4})/', '&#x$1;', $string);
|
|
$utf8string = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');
|
|
$activeWorksheet->setCellValue([$column, $row], $utf8string);
|
|
}
|
|
$column++;
|
|
}
|
|
|
|
$spreadsheet->setActiveSheetIndex(0);
|
|
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
|
$writer->save("$this->data_dir/$folder/$titel-$count_from-$count_to.xlsx");
|
|
}
|
|
}
|
|
|
|
protected function filter(array $files, array $types = ['json'])
|
|
{
|
|
$filtered = [];
|
|
foreach ($files as $file) {
|
|
$path_parts = pathinfo($file);
|
|
if (isset($path_parts['extension']) && in_array($path_parts['extension'], $types)) {
|
|
$filtered[] = $file;
|
|
}
|
|
}
|
|
|
|
return $filtered;
|
|
}
|
|
}
|