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 array |
14
|
|
|
*/ |
15
|
|
|
protected $context; |
16
|
|
|
/** |
17
|
|
|
* @var \Twig_Environment |
18
|
|
|
*/ |
19
|
|
|
protected $environment; |
20
|
|
|
/** |
21
|
|
|
* @var SheetWrapper |
22
|
|
|
*/ |
23
|
|
|
protected $sheetWrapper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Cell|null |
27
|
|
|
*/ |
28
|
|
|
protected $object; |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $attributes; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $mappings; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* CellWrapper constructor. |
40
|
|
|
* |
41
|
|
|
* @param array $context |
42
|
|
|
* @param \Twig_Environment $environment |
43
|
|
|
* @param SheetWrapper $sheetWrapper |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper) |
46
|
|
|
{ |
47
|
|
|
$this->context = $context; |
48
|
|
|
$this->environment = $environment; |
49
|
|
|
$this->sheetWrapper = $sheetWrapper; |
50
|
|
|
|
51
|
|
|
$this->object = null; |
52
|
|
|
$this->attributes = []; |
53
|
|
|
$this->mappings = []; |
54
|
|
|
|
55
|
|
|
$this->initializeMappings(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int|null $index |
60
|
|
|
* @param mixed|null $value |
61
|
|
|
* @param array $properties |
62
|
|
|
* |
63
|
|
|
* @throws \InvalidArgumentException |
64
|
|
|
* @throws \LogicException |
65
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
*/ |
68
|
|
|
public function start(int $index = null, $value = null, array $properties = []) |
69
|
|
|
{ |
70
|
|
|
if ($this->sheetWrapper->getObject() === null) { |
71
|
|
|
throw new \LogicException(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($index === null) { |
75
|
|
|
$this->sheetWrapper->increaseColumn(); |
76
|
|
|
} else { |
77
|
|
|
$this->sheetWrapper->setColumn($index); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->object = $this->sheetWrapper->getObject()->getCellByColumnAndRow($this->sheetWrapper->getColumn(), |
81
|
|
|
$this->sheetWrapper->getRow()); |
82
|
|
|
|
83
|
|
|
if ($value !== null) { |
84
|
|
|
if (isset($properties['dataType'])) { |
85
|
|
|
$this->object->setValueExplicit($value, $properties['dataType']); |
86
|
|
|
} else { |
87
|
|
|
$this->object->setValue($value); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->attributes['value'] = $value; |
92
|
|
|
$this->attributes['properties'] = $properties; |
93
|
|
|
|
94
|
|
|
$this->setProperties($properties, $this->mappings); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function end() |
98
|
|
|
{ |
99
|
|
|
$this->object = null; |
100
|
|
|
$this->attributes = []; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Cell|null |
105
|
|
|
*/ |
106
|
|
|
public function getObject() |
107
|
|
|
{ |
108
|
|
|
return $this->object; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Cell|null $object |
113
|
|
|
*/ |
114
|
|
|
public function setObject(Cell $object = null) |
115
|
|
|
{ |
116
|
|
|
$this->object = $object; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getAttributes(): array |
123
|
|
|
{ |
124
|
|
|
return $this->attributes; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $attributes |
129
|
|
|
*/ |
130
|
|
|
public function setAttributes(array $attributes) |
131
|
|
|
{ |
132
|
|
|
$this->attributes = $attributes; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getMappings(): array |
139
|
|
|
{ |
140
|
|
|
return $this->mappings; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $mappings |
145
|
|
|
*/ |
146
|
|
|
public function setMappings(array $mappings) |
147
|
|
|
{ |
148
|
|
|
$this->mappings = $mappings; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function initializeMappings() |
152
|
|
|
{ |
153
|
|
|
$this->mappings['break'] = function ($value) { |
154
|
|
|
$this->sheetWrapper->getObject()->setBreak($this->object->getCoordinate(), $value); |
155
|
|
|
}; |
156
|
|
|
$this->mappings['dataType'] = function ($value) { |
157
|
|
|
$this->object->setDataType($value); |
158
|
|
|
}; |
159
|
|
|
$this->mappings['dataValidation']['allowBlank'] = function ($value) { |
160
|
|
|
$this->object->getDataValidation()->setAllowBlank($value); |
161
|
|
|
}; |
162
|
|
|
$this->mappings['dataValidation']['error'] = function ($value) { |
163
|
|
|
$this->object->getDataValidation()->setError($value); |
164
|
|
|
}; |
165
|
|
|
$this->mappings['dataValidation']['errorStyle'] = function ($value) { |
166
|
|
|
$this->object->getDataValidation()->setErrorStyle($value); |
167
|
|
|
}; |
168
|
|
|
$this->mappings['dataValidation']['errorTitle'] = function ($value) { |
169
|
|
|
$this->object->getDataValidation()->setErrorTitle($value); |
170
|
|
|
}; |
171
|
|
|
$this->mappings['dataValidation']['formula1'] = function ($value) { |
172
|
|
|
$this->object->getDataValidation()->setFormula1($value); |
173
|
|
|
}; |
174
|
|
|
$this->mappings['dataValidation']['formula2'] = function ($value) { |
175
|
|
|
$this->object->getDataValidation()->setFormula2($value); |
176
|
|
|
}; |
177
|
|
|
$this->mappings['dataValidation']['operator'] = function ($value) { |
178
|
|
|
$this->object->getDataValidation()->setOperator($value); |
179
|
|
|
}; |
180
|
|
|
$this->mappings['dataValidation']['prompt'] = function ($value) { |
181
|
|
|
$this->object->getDataValidation()->setPrompt($value); |
182
|
|
|
}; |
183
|
|
|
$this->mappings['dataValidation']['promptTitle'] = function ($value) { |
184
|
|
|
$this->object->getDataValidation()->setPromptTitle($value); |
185
|
|
|
}; |
186
|
|
|
$this->mappings['dataValidation']['showDropDown'] = function ($value) { |
187
|
|
|
$this->object->getDataValidation()->setShowDropDown($value); |
188
|
|
|
}; |
189
|
|
|
$this->mappings['dataValidation']['showErrorMessage'] = function ($value) { |
190
|
|
|
$this->object->getDataValidation()->setShowErrorMessage($value); |
191
|
|
|
}; |
192
|
|
|
$this->mappings['dataValidation']['showInputMessage'] = function ($value) { |
193
|
|
|
$this->object->getDataValidation()->setShowInputMessage($value); |
194
|
|
|
}; |
195
|
|
|
$this->mappings['dataValidation']['type'] = function ($value) { |
196
|
|
|
$this->object->getDataValidation()->setType($value); |
197
|
|
|
}; |
198
|
|
|
$this->mappings['merge'] = function ($value) { |
199
|
|
|
if (is_int($value)) { |
200
|
|
|
$value = Cell::stringFromColumnIndex($value).$this->sheetWrapper->getRow(); |
201
|
|
|
} |
202
|
|
|
$this->sheetWrapper->getObject()->mergeCells(sprintf('%s:%s', $this->object->getCoordinate(), $value)); |
203
|
|
|
}; |
204
|
|
|
$this->mappings['style'] = function ($value) { |
205
|
|
|
$this->sheetWrapper->getObject()->getStyle($this->object->getCoordinate())->applyFromArray($value); |
206
|
|
|
}; |
207
|
|
|
$this->mappings['url'] = function ($value) { |
208
|
|
|
$this->object->getHyperlink()->setUrl($value); |
209
|
|
|
}; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|