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