|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Wrapper; |
|
4
|
|
|
|
|
5
|
|
|
use MewesK\TwigSpreadsheetBundle\Helper\Filesystem; |
|
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class DrawingWrapper. |
|
11
|
|
|
*/ |
|
12
|
|
|
class DrawingWrapper extends BaseWrapper |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var SheetWrapper |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $sheetWrapper; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var HeaderFooterWrapper |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $headerFooterWrapper; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Drawing|HeaderFooterDrawing|null |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $object; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $attributes; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* DrawingWrapper constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $context |
|
36
|
|
|
* @param \Twig_Environment $environment |
|
37
|
|
|
* @param SheetWrapper $sheetWrapper |
|
38
|
|
|
* @param HeaderFooterWrapper $headerFooterWrapper |
|
39
|
|
|
* @param array $attributes |
|
40
|
|
|
*/ |
|
41
|
112 |
|
public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper, HeaderFooterWrapper $headerFooterWrapper, array $attributes = []) |
|
42
|
|
|
{ |
|
43
|
112 |
|
parent::__construct($context, $environment); |
|
44
|
|
|
|
|
45
|
112 |
|
$this->sheetWrapper = $sheetWrapper; |
|
46
|
112 |
|
$this->headerFooterWrapper = $headerFooterWrapper; |
|
47
|
|
|
|
|
48
|
112 |
|
$this->object = null; |
|
49
|
112 |
|
$this->attributes = $attributes; |
|
50
|
112 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $path |
|
54
|
|
|
* @param array $properties |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
57
|
|
|
* @throws \LogicException |
|
58
|
|
|
* @throws \InvalidArgumentException |
|
59
|
|
|
* @throws \RuntimeException |
|
60
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function start(string $path, array $properties = []) |
|
63
|
|
|
{ |
|
64
|
6 |
|
if ($this->sheetWrapper->getObject() === null) { |
|
65
|
|
|
throw new \LogicException(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// create local copy of the asset |
|
69
|
6 |
|
$tempPath = $this->createTempCopy($path); |
|
70
|
|
|
|
|
71
|
|
|
// add to header/footer |
|
72
|
6 |
|
if ($this->headerFooterWrapper->getObject()) { |
|
73
|
1 |
|
$headerFooterParameters = $this->headerFooterWrapper->getParameters(); |
|
74
|
1 |
|
$alignment = $this->headerFooterWrapper->getAlignmentParameters()['type']; |
|
75
|
1 |
|
$location = ''; |
|
76
|
|
|
|
|
77
|
|
|
switch ($alignment) { |
|
78
|
1 |
|
case HeaderFooterWrapper::ALIGNMENT_CENTER: |
|
79
|
|
|
$location .= 'C'; |
|
80
|
|
|
$headerFooterParameters['value'][HeaderFooterWrapper::ALIGNMENT_CENTER] .= '&G'; |
|
81
|
|
|
break; |
|
82
|
1 |
|
case HeaderFooterWrapper::ALIGNMENT_LEFT: |
|
83
|
1 |
|
$location .= 'L'; |
|
84
|
1 |
|
$headerFooterParameters['value'][HeaderFooterWrapper::ALIGNMENT_LEFT] .= '&G'; |
|
85
|
1 |
|
break; |
|
86
|
1 |
|
case HeaderFooterWrapper::ALIGNMENT_RIGHT: |
|
87
|
1 |
|
$location .= 'R'; |
|
88
|
1 |
|
$headerFooterParameters['value'][HeaderFooterWrapper::ALIGNMENT_RIGHT] .= '&G'; |
|
89
|
1 |
|
break; |
|
90
|
|
|
default: |
|
91
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $alignment)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$location .= $headerFooterParameters['baseType'] === HeaderFooterWrapper::BASETYPE_HEADER ? 'H' : 'F'; |
|
95
|
|
|
|
|
96
|
1 |
|
$this->object = new HeaderFooterDrawing(); |
|
97
|
1 |
|
$this->object->setPath($tempPath); |
|
98
|
1 |
|
$this->headerFooterWrapper->getObject()->addImage($this->object, $location); |
|
99
|
1 |
|
$this->headerFooterWrapper->setParameters($headerFooterParameters); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// add to worksheet |
|
103
|
|
|
else { |
|
104
|
5 |
|
$this->object = new Drawing(); |
|
105
|
5 |
|
$this->object->setWorksheet($this->sheetWrapper->getObject()); |
|
106
|
5 |
|
$this->object->setPath($tempPath); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
6 |
|
$this->setProperties($properties); |
|
110
|
6 |
|
} |
|
111
|
|
|
|
|
112
|
6 |
|
public function end() |
|
113
|
|
|
{ |
|
114
|
6 |
|
$this->object = null; |
|
115
|
6 |
|
$this->parameters = []; |
|
116
|
6 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return Drawing |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getObject(): Drawing |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->object; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param Drawing $object |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setObject(Drawing $object) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->object = $object; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function configureMappings(): array |
|
138
|
|
|
{ |
|
139
|
|
|
return [ |
|
140
|
|
|
'coordinates' => function ($value) { $this->object->setCoordinates($value); }, |
|
141
|
|
|
'description' => function ($value) { $this->object->setDescription($value); }, |
|
142
|
|
|
'height' => function ($value) { $this->object->setHeight($value); }, |
|
143
|
|
|
'name' => function ($value) { $this->object->setName($value); }, |
|
144
|
|
|
'offsetX' => function ($value) { $this->object->setOffsetX($value); }, |
|
145
|
|
|
'offsetY' => function ($value) { $this->object->setOffsetY($value); }, |
|
146
|
|
|
'resizeProportional' => function ($value) { $this->object->setResizeProportional($value); }, |
|
147
|
|
|
'rotation' => function ($value) { $this->object->setRotation($value); }, |
|
148
|
|
|
'shadow' => [ |
|
149
|
|
|
'alignment' => function ($value) { $this->object->getShadow()->setAlignment($value); }, |
|
150
|
|
|
'alpha' => function ($value) { $this->object->getShadow()->setAlpha($value); }, |
|
151
|
|
|
'blurRadius' => function ($value) { $this->object->getShadow()->setBlurRadius($value); }, |
|
152
|
|
|
'color' => function ($value) { $this->object->getShadow()->getColor()->setRGB($value); }, |
|
153
|
|
|
'direction' => function ($value) { $this->object->getShadow()->setDirection($value); }, |
|
154
|
|
|
'distance' => function ($value) { $this->object->getShadow()->setDistance($value); }, |
|
155
|
|
|
'visible' => function ($value) { $this->object->getShadow()->setVisible($value); }, |
|
156
|
|
|
], |
|
157
|
|
|
'width' => function ($value) { $this->object->setWidth($value); }, |
|
158
|
|
|
]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $path |
|
163
|
|
|
* |
|
164
|
|
|
* @throws \InvalidArgumentException |
|
165
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
6 |
|
private function createTempCopy(string $path): string |
|
170
|
|
|
{ |
|
171
|
|
|
// create temp path |
|
172
|
6 |
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
|
173
|
6 |
|
$tempPath = sprintf('%s/tsb_%s%s', $this->attributes['cache']['bitmap'], md5($path), $extension ? '.'.$extension : ''); |
|
174
|
|
|
|
|
175
|
|
|
// create local copy |
|
176
|
6 |
|
if (!Filesystem::exists($tempPath)) { |
|
177
|
2 |
|
$data = file_get_contents($path); |
|
178
|
2 |
|
if ($data === false) { |
|
179
|
|
|
throw new \InvalidArgumentException($path.' does not exist.'); |
|
180
|
|
|
} |
|
181
|
2 |
|
Filesystem::dumpFile($tempPath, $data); |
|
182
|
2 |
|
unset($data); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
6 |
|
return $tempPath; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|