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