Passed
Push — master ( abd7ac...a09b7d )
by Alexandros
01:55
created

DataHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 33
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceProperties() 0 14 5
A handle() 0 37 4
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