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