|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Wrapper; |
|
4
|
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CellWrapper. |
|
9
|
|
|
*/ |
|
10
|
|
|
class CellWrapper extends BaseWrapper |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var SheetWrapper |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $sheetWrapper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Cell|null |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $object; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* CellWrapper constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $context |
|
26
|
|
|
* @param \Twig_Environment $environment |
|
27
|
|
|
* @param SheetWrapper $sheetWrapper |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($context, $environment); |
|
32
|
|
|
|
|
33
|
|
|
$this->sheetWrapper = $sheetWrapper; |
|
34
|
|
|
|
|
35
|
|
|
$this->object = null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param int|null $index |
|
40
|
|
|
* @param mixed|null $value |
|
41
|
|
|
* @param array $properties |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \InvalidArgumentException |
|
44
|
|
|
* @throws \LogicException |
|
45
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
|
46
|
|
|
* @throws \RuntimeException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function start(int $index = null, $value = null, array $properties = []) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->sheetWrapper->getObject() === null) { |
|
51
|
|
|
throw new \LogicException(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($index === null) { |
|
55
|
|
|
$this->sheetWrapper->increaseColumn(); |
|
56
|
|
|
} else { |
|
57
|
|
|
$this->sheetWrapper->setColumn($index); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->object = $this->sheetWrapper->getObject()->getCellByColumnAndRow($this->sheetWrapper->getColumn(), |
|
61
|
|
|
$this->sheetWrapper->getRow()); |
|
62
|
|
|
|
|
63
|
|
|
if ($value !== null) { |
|
64
|
|
|
if (isset($properties['dataType'])) { |
|
65
|
|
|
$this->object->setValueExplicit($value, $properties['dataType']); |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->object->setValue($value); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->attributes['value'] = $value; |
|
72
|
|
|
$this->attributes['properties'] = $properties; |
|
73
|
|
|
|
|
74
|
|
|
$this->setProperties($properties, $this->mappings); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function end() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->object = null; |
|
80
|
|
|
$this->attributes = []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return Cell|null |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getObject() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->object; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param Cell|null $object |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setObject(Cell $object = null) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->object = $object; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function configureMappings(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
'break' => function ($value) { $this->sheetWrapper->getObject()->setBreak($this->object->getCoordinate(), $value); }, |
|
106
|
|
|
'dataType' => function ($value) { $this->object->setDataType($value); }, |
|
107
|
|
|
'dataValidation' => [ |
|
108
|
|
|
'allowBlank' => function ($value) { $this->object->getDataValidation()->setAllowBlank($value); }, |
|
109
|
|
|
'error' => function ($value) { $this->object->getDataValidation()->setError($value); }, |
|
110
|
|
|
'errorStyle' => function ($value) { $this->object->getDataValidation()->setErrorStyle($value); }, |
|
111
|
|
|
'errorTitle' => function ($value) { $this->object->getDataValidation()->setErrorTitle($value); }, |
|
112
|
|
|
'formula1' => function ($value) { $this->object->getDataValidation()->setFormula1($value); }, |
|
113
|
|
|
'formula2' => function ($value) { $this->object->getDataValidation()->setFormula2($value); }, |
|
114
|
|
|
'operator' => function ($value) { $this->object->getDataValidation()->setOperator($value); }, |
|
115
|
|
|
'prompt' => function ($value) { $this->object->getDataValidation()->setPrompt($value); }, |
|
116
|
|
|
'promptTitle' => function ($value) { $this->object->getDataValidation()->setPromptTitle($value); }, |
|
117
|
|
|
'showDropDown' => function ($value) { $this->object->getDataValidation()->setShowDropDown($value); }, |
|
118
|
|
|
'showErrorMessage' => function ($value) { $this->object->getDataValidation()->setShowErrorMessage($value); }, |
|
119
|
|
|
'showInputMessage' => function ($value) { $this->object->getDataValidation()->setShowInputMessage($value); }, |
|
120
|
|
|
'type' => function ($value) { $this->object->getDataValidation()->setType($value); }, |
|
121
|
|
|
], |
|
122
|
|
|
'merge' => function ($value) { |
|
123
|
|
|
if (is_int($value)) { |
|
124
|
|
|
$value = Cell::stringFromColumnIndex($value).$this->sheetWrapper->getRow(); |
|
125
|
|
|
} |
|
126
|
|
|
$this->sheetWrapper->getObject()->mergeCells(sprintf('%s:%s', $this->object->getCoordinate(), $value)); |
|
127
|
|
|
}, |
|
128
|
|
|
'style' => function ($value) { $this->sheetWrapper->getObject()->getStyle($this->object->getCoordinate())->applyFromArray($value); }, |
|
129
|
|
|
'url' => function ($value) { $this->object->getHyperlink()->setUrl($value); }, |
|
130
|
|
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|