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