-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
MappedReader.php
51 lines (43 loc) 路 1.39 KB
/
MappedReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Maatwebsite\Excel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithFormatData;
use Maatwebsite\Excel\Concerns\WithMappedCells;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class MappedReader
{
/**
* @param WithMappedCells $import
* @param Worksheet $worksheet
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function map(WithMappedCells $import, Worksheet $worksheet)
{
$mapped = $import->mapping();
array_walk_recursive($mapped, function (&$coordinate) use ($import, $worksheet) {
$cell = Cell::make($worksheet, $coordinate);
$coordinate = $cell->getValue(
null,
$import instanceof WithCalculatedFormulas,
$import instanceof WithFormatData
);
});
if ($import instanceof ToModel) {
$model = $import->model($mapped);
if ($model) {
$model->saveOrFail();
}
}
if ($import instanceof ToCollection) {
$import->collection(new Collection($mapped));
}
if ($import instanceof ToArray) {
$import->array($mapped);
}
}
}