Passed
Push — master ( 59d671...e19d13 )
by Alexandros
01:54
created

DataHandler::formatCell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 PhpOffice\PhpSpreadsheet\Cell\Cell;
10
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
11
12
/**
13
 * Class DataHandler - Reference Implementation
14
 * @package MisterIcy\ExcelWriter\Handlers
15
 */
16
final class DataHandler extends AbstractHandler
17
{
18
    /**
19
     * @param AbstractGenerator $generator
20
     * @return HandlerInterface
21
     */
22
    public function handle(GeneratorInterface $generator)
23
    {
24
        $spreadsheet = $generator->getSpreadsheet();
0 ignored issues
show
Bug introduced by
The method getSpreadsheet() does not exist on MisterIcy\ExcelWriter\Generator\GeneratorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to MisterIcy\ExcelWriter\Generator\GeneratorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        $spreadsheet = $generator->getSpreadsheet();
Loading history...
25
        $properties = $generator->getProperties();
0 ignored issues
show
Bug introduced by
The method getProperties() does not exist on MisterIcy\ExcelWriter\Generator\GeneratorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to MisterIcy\ExcelWriter\Generator\GeneratorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        /** @scrutinizer ignore-call */ 
26
        $properties = $generator->getProperties();
Loading history...
26
        $data = $generator->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on MisterIcy\ExcelWriter\Generator\GeneratorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to MisterIcy\ExcelWriter\Generator\GeneratorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        /** @scrutinizer ignore-call */ 
27
        $data = $generator->getData();
Loading history...
27
28
        $row = 2;
29
        $column = 1;
30
31
        foreach ($data as $object) {
32
            /** @var AbstractProperty $property */
33
            foreach ($properties->getProperties() as $property) {
34
35
                $value = $property->renderProperty($object);
36
37
                if ($property->isFormula()) {
38
                    $value = str_replace("{row}", strval($row), $value);
39
                    $value = str_replace("{col}", Coordinate::stringFromColumnIndex($column), $value);
40
41
                    $matches = [];
42
                    preg_match_all("/\[(.*?)\]/", $value, $matches, PREG_UNMATCHED_AS_NULL);
43
44
45
                    if (count($matches) > 0) {
46
                        if (count($matches[0]) > 0 && count($matches[1]) > 0) {
47
                            foreach ($matches[1] as $match) {
48
                                $propColumn = $properties->getExcelColumnOfPropertyPath($match);
49
                                $value = str_replace("[" . $match . "]", $propColumn, $value);
50
                            }
51
                        }
52
                    }
53
                }
54
                $spreadsheet->getActiveSheet()
55
                    ->setCellValueByColumnAndRow($column, $row, $value);
56
57
58
                $spreadsheet
59
                    ->getActiveSheet()
60
                    ->getCellByColumnAndRow($column, $row)
61
                    ->getStyle()
62
                    ->getNumberFormat()
63
                    ->setFormatCode($property->getFormatCode());
64
                $column++;
65
            }
66
            $column = 1;
67
            $row++;
68
        }
69
        return parent::handle($generator);
70
    }
71
}
72