|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MewesK\TwigExcelBundle\Wrapper; |
|
4
|
|
|
use Twig_Environment; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class XlsHeaderFooterWrapper |
|
8
|
|
|
* |
|
9
|
|
|
* @package MewesK\TwigExcelBundle\Wrapper |
|
10
|
|
|
*/ |
|
11
|
|
|
class XlsHeaderFooterWrapper extends AbstractWrapper |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $context; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Twig_Environment |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $environment; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var XlsSheetWrapper |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $sheetWrapper; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $alignmentAttributes; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \PHPExcel_Worksheet_HeaderFooter |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $object; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $attributes; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $mappings; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* XlsHeaderFooterWrapper constructor. |
|
46
|
|
|
* @param array $context |
|
47
|
|
|
* @param Twig_Environment $environment |
|
48
|
|
|
* @param XlsSheetWrapper $sheetWrapper |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(array $context, Twig_Environment $environment, XlsSheetWrapper $sheetWrapper) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->context = $context; |
|
53
|
|
|
$this->environment = $environment; |
|
54
|
|
|
$this->sheetWrapper = $sheetWrapper; |
|
55
|
|
|
|
|
56
|
|
|
$this->alignmentAttributes = []; |
|
57
|
|
|
|
|
58
|
|
|
$this->object = null; |
|
59
|
|
|
$this->attributes = []; |
|
60
|
|
|
$this->mappings = []; |
|
61
|
|
|
|
|
62
|
|
|
$this->initializeMappings(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function initializeMappings() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->mappings['scaleWithDocument'] = function ($value) { |
|
68
|
|
|
$this->object->setScaleWithDocument($value); |
|
69
|
|
|
}; |
|
70
|
|
|
$this->mappings['alignWithMargins'] = function ($value) { |
|
71
|
|
|
$this->object->setAlignWithMargins($value); |
|
72
|
|
|
}; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $type |
|
77
|
|
|
* @param null|array $properties |
|
78
|
|
|
* @throws \LogicException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function start($type, array $properties = null) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->sheetWrapper->getObject() === null) { |
|
83
|
|
|
throw new \LogicException(); |
|
84
|
|
|
} |
|
85
|
|
|
if (in_array(strtolower($type), |
|
86
|
|
|
['header', 'oddheader', 'evenheader', 'firstheader', 'footer', 'oddfooter', 'evenfooter', 'firstfooter'], |
|
87
|
|
|
true) === false |
|
88
|
|
|
) { |
|
89
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $type)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->object = $this->sheetWrapper->getObject()->getHeaderFooter(); |
|
93
|
|
|
$this->attributes['value'] = ['left' => null, 'center' => null, 'right' => null]; // will be generated by the alignment tags |
|
94
|
|
|
$this->attributes['type'] = $type; |
|
95
|
|
|
$this->attributes['properties'] = $properties ?: []; |
|
96
|
|
|
|
|
97
|
|
|
if ($properties !== null) { |
|
98
|
|
|
$this->setProperties($properties, $this->mappings); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function end() |
|
103
|
|
|
{ |
|
104
|
|
|
$value = implode('', $this->attributes['value']); |
|
105
|
|
|
|
|
106
|
|
|
switch (strtolower($this->attributes['type'])) { |
|
107
|
|
|
case 'header': |
|
108
|
|
|
$this->object->setOddHeader($value); |
|
109
|
|
|
$this->object->setEvenHeader($value); |
|
110
|
|
|
$this->object->setFirstHeader($value); |
|
111
|
|
|
break; |
|
112
|
|
|
case 'oddheader': |
|
113
|
|
|
$this->object->setDifferentOddEven(true); |
|
114
|
|
|
$this->object->setOddHeader($value); |
|
115
|
|
|
break; |
|
116
|
|
|
case 'evenheader': |
|
117
|
|
|
$this->object->setDifferentOddEven(true); |
|
118
|
|
|
$this->object->setEvenHeader($value); |
|
119
|
|
|
break; |
|
120
|
|
|
case 'firstheader': |
|
121
|
|
|
$this->object->setDifferentFirst(true); |
|
122
|
|
|
$this->object->setFirstHeader($value); |
|
123
|
|
|
break; |
|
124
|
|
|
case 'footer': |
|
125
|
|
|
$this->object->setOddFooter($value); |
|
126
|
|
|
$this->object->setEvenFooter($value); |
|
127
|
|
|
$this->object->setFirstFooter($value); |
|
128
|
|
|
break; |
|
129
|
|
|
case 'oddfooter': |
|
130
|
|
|
$this->object->setDifferentOddEven(true); |
|
131
|
|
|
$this->object->setOddFooter($value); |
|
132
|
|
|
break; |
|
133
|
|
|
case 'evenfooter': |
|
134
|
|
|
$this->object->setDifferentOddEven(true); |
|
135
|
|
|
$this->object->setEvenFooter($value); |
|
136
|
|
|
break; |
|
137
|
|
|
case 'firstfooter': |
|
138
|
|
|
$this->object->setDifferentFirst(true); |
|
139
|
|
|
$this->object->setFirstFooter($value); |
|
140
|
|
|
break; |
|
141
|
|
|
default: |
|
142
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $this->attributes['type'])); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$this->object = null; |
|
146
|
|
|
$this->attributes = []; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param null|string $type |
|
151
|
|
|
* @param null|array $properties |
|
152
|
|
|
* @throws \InvalidArgumentException |
|
153
|
|
|
*/ |
|
154
|
|
|
public function startAlignment($type = null, array $properties = null) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->alignmentAttributes['type'] = $type; |
|
157
|
|
|
$this->alignmentAttributes['properties'] = $properties; |
|
158
|
|
|
|
|
159
|
|
|
switch (strtolower($this->alignmentAttributes['type'])) { |
|
160
|
|
|
case 'left': |
|
161
|
|
|
$this->attributes['value']['left'] = '&L'; |
|
162
|
|
|
break; |
|
163
|
|
|
case 'center': |
|
164
|
|
|
$this->attributes['value']['center'] = '&C'; |
|
165
|
|
|
break; |
|
166
|
|
|
case 'right': |
|
167
|
|
|
$this->attributes['value']['right'] = '&R'; |
|
168
|
|
|
break; |
|
169
|
|
|
default: |
|
170
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $this->alignmentAttributes['type'])); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param null|string $value |
|
176
|
|
|
* @throws \InvalidArgumentException |
|
177
|
|
|
*/ |
|
178
|
|
|
public function endAlignment($value = null) |
|
179
|
|
|
{ |
|
180
|
|
|
switch (strtolower($this->alignmentAttributes['type'])) { |
|
181
|
|
|
case 'left': |
|
182
|
|
|
if (strpos($this->attributes['value']['left'], '&G') === false) { |
|
183
|
|
|
$this->attributes['value']['left'] .= $value; |
|
184
|
|
|
} |
|
185
|
|
|
break; |
|
186
|
|
|
case 'center': |
|
187
|
|
|
if (strpos($this->attributes['value']['center'], '&G') === false) { |
|
188
|
|
|
$this->attributes['value']['center'] .= $value; |
|
189
|
|
|
} |
|
190
|
|
|
break; |
|
191
|
|
|
case 'right': |
|
192
|
|
|
if (strpos($this->attributes['value']['right'], '&G') === false) { |
|
193
|
|
|
$this->attributes['value']['right'] .= $value; |
|
194
|
|
|
} |
|
195
|
|
|
break; |
|
196
|
|
|
default: |
|
197
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $this->alignmentAttributes['type'])); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$this->alignmentAttributes = []; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
// |
|
204
|
|
|
// Getters/Setters |
|
205
|
|
|
// |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return \PHPExcel_Worksheet_HeaderFooter |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getObject() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->object; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param \PHPExcel_Worksheet_HeaderFooter $object |
|
217
|
|
|
*/ |
|
218
|
|
|
public function setObject($object) |
|
219
|
|
|
{ |
|
220
|
|
|
$this->object = $object; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getAttributes() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->attributes; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param array $attributes |
|
233
|
|
|
*/ |
|
234
|
|
|
public function setAttributes($attributes) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->attributes = $attributes; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return array |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getMappings() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->mappings; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param array $mappings |
|
249
|
|
|
*/ |
|
250
|
|
|
public function setMappings($mappings) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->mappings = $mappings; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @return array |
|
257
|
|
|
*/ |
|
258
|
|
|
public function getAlignmentAttributes() |
|
259
|
|
|
{ |
|
260
|
|
|
return $this->alignmentAttributes; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @param array $alignmentAttributes |
|
265
|
|
|
*/ |
|
266
|
|
|
public function setAlignmentAttributes($alignmentAttributes) |
|
267
|
|
|
{ |
|
268
|
|
|
$this->alignmentAttributes = $alignmentAttributes; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|