1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MisterIcy\ExcelWriter\Handlers; |
5
|
|
|
|
6
|
|
|
use MisterIcy\ExcelWriter\Generator\AbstractGenerator; |
7
|
|
|
use MisterIcy\ExcelWriter\Generator\GeneratorInterface; |
8
|
|
|
use MisterIcy\ExcelWriter\Properties\AbstractProperty; |
9
|
|
|
use MisterIcy\ExcelWriter\Properties\PropertyCollection; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DataHandler - Reference Implementation |
15
|
|
|
* @package MisterIcy\ExcelWriter\Handlers |
16
|
|
|
*/ |
17
|
|
|
final class DataHandler extends AbstractHandler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param AbstractGenerator $generator |
21
|
|
|
* @return HandlerInterface |
22
|
|
|
*/ |
23
|
|
|
public function handle(GeneratorInterface $generator) |
24
|
|
|
{ |
25
|
|
|
/** @scrutinizer ignore-call */ |
26
|
|
|
$spreadsheet = $generator->getSpreadsheet(); |
27
|
|
|
/** @scrutinizer ignore-call */ |
28
|
|
|
$properties = $generator->getProperties(); |
29
|
|
|
/** @scrutinizer ignore-call */ |
30
|
|
|
$data = $generator->getData(); |
31
|
|
|
|
32
|
|
|
$row = 2; |
33
|
|
|
$column = 1; |
34
|
|
|
|
35
|
|
|
foreach ($data as $object) { |
36
|
|
|
/** @var AbstractProperty $property */ |
37
|
|
|
foreach ($properties->getProperties() as $property) { |
38
|
|
|
$value = $property->renderProperty($object); |
39
|
|
|
if ($property->isFormula()) { |
40
|
|
|
$value = str_replace("{row}", strval($row), $value); |
41
|
|
|
$value = str_replace("{col}", Coordinate::stringFromColumnIndex($column), $value); |
42
|
|
|
|
43
|
|
|
$value = $this->replaceProperties($properties, $value); |
44
|
|
|
} |
45
|
|
|
$spreadsheet->getActiveSheet() |
46
|
|
|
->setCellValueByColumnAndRow($column, $row, $value); |
47
|
|
|
|
48
|
|
|
$spreadsheet |
49
|
|
|
->getActiveSheet() |
50
|
|
|
->getCellByColumnAndRow($column, $row) |
51
|
|
|
->getStyle() |
52
|
|
|
->getNumberFormat() |
53
|
|
|
->setFormatCode($property->getFormatCode()); |
54
|
|
|
$column++; |
55
|
|
|
} |
56
|
|
|
$column = 1; |
57
|
|
|
$row++; |
58
|
|
|
} |
59
|
|
|
return parent::handle($generator); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param PropertyCollection $properties |
64
|
|
|
* @param string $value |
65
|
|
|
* @return string |
66
|
|
|
* @throws \MisterIcy\ExcelWriter\Exceptions\PropertyException |
67
|
|
|
*/ |
68
|
|
|
private function replaceProperties(PropertyCollection $properties, string $value) : string |
69
|
|
|
{ |
70
|
|
|
$matches = []; |
71
|
|
|
preg_match_all("/\[(.*?)\]/", $value, $matches, PREG_UNMATCHED_AS_NULL); |
72
|
|
|
|
73
|
|
|
if (count($matches) > 0) { |
74
|
|
|
if (count($matches[0]) > 0 && count($matches[1]) > 0) { |
75
|
|
|
foreach ($matches[1] as $match) { |
76
|
|
|
$propColumn = $properties->getExcelColumnOfPropertyPath($match); |
77
|
|
|
$value = str_replace("[" . $match . "]", $propColumn, $value); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|