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