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 SheetWrapper |
14
|
|
|
*/ |
15
|
|
|
protected $sheetWrapper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var null|HeaderFooter |
19
|
|
|
*/ |
20
|
|
|
protected $object; |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $alignmentAttributes; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* HeaderFooterWrapper constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $context |
30
|
|
|
* @param \Twig_Environment $environment |
31
|
|
|
* @param SheetWrapper $sheetWrapper |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($context, $environment); |
36
|
|
|
|
37
|
|
|
$this->sheetWrapper = $sheetWrapper; |
38
|
|
|
|
39
|
|
|
$this->object = null; |
40
|
|
|
$this->alignmentAttributes = []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $type |
45
|
|
|
* @param array $properties |
46
|
|
|
* |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
* @throws \LogicException |
49
|
|
|
* @throws \RuntimeException |
50
|
|
|
*/ |
51
|
|
|
public function start(string $type, array $properties = []) |
52
|
|
|
{ |
53
|
|
|
if ($this->sheetWrapper->getObject() === null) { |
54
|
|
|
throw new \LogicException(); |
55
|
|
|
} |
56
|
|
|
if (in_array(strtolower($type), |
57
|
|
|
['header', 'oddheader', 'evenheader', 'firstheader', 'footer', 'oddfooter', 'evenfooter', 'firstfooter'], |
58
|
|
|
true) === false |
59
|
|
|
) { |
60
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $type)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->object = $this->sheetWrapper->getObject()->getHeaderFooter(); |
64
|
|
|
$this->attributes['value'] = ['left' => null, 'center' => null, 'right' => null]; // will be generated by the alignment tags |
65
|
|
|
$this->attributes['type'] = $type; |
66
|
|
|
$this->attributes['properties'] = $properties; |
67
|
|
|
|
68
|
|
|
$this->setProperties($properties, $this->mappings); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function end() |
72
|
|
|
{ |
73
|
|
|
$value = implode('', $this->attributes['value']); |
74
|
|
|
|
75
|
|
|
switch (strtolower($this->attributes['type'])) { |
76
|
|
|
case 'header': |
77
|
|
|
$this->object->setOddHeader($value); |
78
|
|
|
$this->object->setEvenHeader($value); |
79
|
|
|
$this->object->setFirstHeader($value); |
80
|
|
|
break; |
81
|
|
|
case 'oddheader': |
82
|
|
|
$this->object->setDifferentOddEven(true); |
83
|
|
|
$this->object->setOddHeader($value); |
84
|
|
|
break; |
85
|
|
|
case 'evenheader': |
86
|
|
|
$this->object->setDifferentOddEven(true); |
87
|
|
|
$this->object->setEvenHeader($value); |
88
|
|
|
break; |
89
|
|
|
case 'firstheader': |
90
|
|
|
$this->object->setDifferentFirst(true); |
91
|
|
|
$this->object->setFirstHeader($value); |
92
|
|
|
break; |
93
|
|
|
case 'footer': |
94
|
|
|
$this->object->setOddFooter($value); |
95
|
|
|
$this->object->setEvenFooter($value); |
96
|
|
|
$this->object->setFirstFooter($value); |
97
|
|
|
break; |
98
|
|
|
case 'oddfooter': |
99
|
|
|
$this->object->setDifferentOddEven(true); |
100
|
|
|
$this->object->setOddFooter($value); |
101
|
|
|
break; |
102
|
|
|
case 'evenfooter': |
103
|
|
|
$this->object->setDifferentOddEven(true); |
104
|
|
|
$this->object->setEvenFooter($value); |
105
|
|
|
break; |
106
|
|
|
case 'firstfooter': |
107
|
|
|
$this->object->setDifferentFirst(true); |
108
|
|
|
$this->object->setFirstFooter($value); |
109
|
|
|
break; |
110
|
|
|
default: |
111
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $this->attributes['type'])); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->object = null; |
115
|
|
|
$this->attributes = []; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $type |
120
|
|
|
* @param array $properties |
121
|
|
|
* |
122
|
|
|
* @throws \InvalidArgumentException |
123
|
|
|
*/ |
124
|
|
|
public function startAlignment(string $type, array $properties = []) |
125
|
|
|
{ |
126
|
|
|
$this->alignmentAttributes['type'] = $type; |
127
|
|
|
$this->alignmentAttributes['properties'] = $properties; |
128
|
|
|
|
129
|
|
|
switch (strtolower($type)) { |
130
|
|
|
case 'left': |
131
|
|
|
$this->attributes['value']['left'] = '&L'; |
132
|
|
|
break; |
133
|
|
|
case 'center': |
134
|
|
|
$this->attributes['value']['center'] = '&C'; |
135
|
|
|
break; |
136
|
|
|
case 'right': |
137
|
|
|
$this->attributes['value']['right'] = '&R'; |
138
|
|
|
break; |
139
|
|
|
default: |
140
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $this->alignmentAttributes['type'])); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $value |
146
|
|
|
* |
147
|
|
|
* @throws \InvalidArgumentException |
148
|
|
|
*/ |
149
|
|
|
public function endAlignment($value) |
150
|
|
|
{ |
151
|
|
|
switch (strtolower($this->alignmentAttributes['type'])) { |
152
|
|
View Code Duplication |
case 'left': |
|
|
|
|
153
|
|
|
if (strpos($this->attributes['value']['left'], '&G') === false) { |
154
|
|
|
$this->attributes['value']['left'] .= $value; |
155
|
|
|
} |
156
|
|
|
break; |
157
|
|
View Code Duplication |
case 'center': |
|
|
|
|
158
|
|
|
if (strpos($this->attributes['value']['center'], '&G') === false) { |
159
|
|
|
$this->attributes['value']['center'] .= $value; |
160
|
|
|
} |
161
|
|
|
break; |
162
|
|
View Code Duplication |
case 'right': |
|
|
|
|
163
|
|
|
if (strpos($this->attributes['value']['right'], '&G') === false) { |
164
|
|
|
$this->attributes['value']['right'] .= $value; |
165
|
|
|
} |
166
|
|
|
break; |
167
|
|
|
default: |
168
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $this->alignmentAttributes['type'])); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->alignmentAttributes = []; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return null|HeaderFooter |
176
|
|
|
*/ |
177
|
|
|
public function getObject() |
178
|
|
|
{ |
179
|
|
|
return $this->object; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param null|HeaderFooter $object |
184
|
|
|
*/ |
185
|
|
|
public function setObject(HeaderFooter $object = null) |
186
|
|
|
{ |
187
|
|
|
$this->object = $object; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
public function getAlignmentAttributes(): array |
194
|
|
|
{ |
195
|
|
|
return $this->alignmentAttributes; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param array $alignmentAttributes |
200
|
|
|
*/ |
201
|
|
|
public function setAlignmentAttributes(array $alignmentAttributes) |
202
|
|
|
{ |
203
|
|
|
$this->alignmentAttributes = $alignmentAttributes; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
protected function configureMappings(): array |
210
|
|
|
{ |
211
|
|
|
return [ |
212
|
|
|
'scaleWithDocument' => function ($value) { $this->object->setScaleWithDocument($value); }, |
213
|
|
|
'alignWithMargins' => function ($value) { $this->object->setAlignWithMargins($value); }, |
214
|
|
|
]; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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.