Completed
Pull Request — develop (#455)
by
unknown
10:09
created

AbstractDecoratorWriter::absoluteZipPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 1
crap 20
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\Drawing as CommonDrawing;
6
use PhpOffice\Common\XMLWriter;
7
use PhpOffice\PhpPresentation\Style\Border;
8
use PhpOffice\PhpPresentation\Style\Color;
9
use PhpOffice\PhpPresentation\Style\Fill;
10
use PhpOffice\PhpPresentation\Style\Outline;
11
12
abstract class AbstractDecoratorWriter extends \PhpOffice\PhpPresentation\Writer\AbstractDecoratorWriter
13
{
14
    /**
15
     * Write relationship
16
     *
17
     * @param  \PhpOffice\Common\XMLWriter $objWriter   XML Writer
18
     * @param  int                            $pId         Relationship ID. rId will be prepended!
19
     * @param  string                         $pType       Relationship type
20
     * @param  string                         $pTarget     Relationship target
21
     * @param  string                         $pTargetMode Relationship target mode
22
     * @throws \Exception
23
     */
24 115
    protected function writeRelationship(XMLWriter $objWriter, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
25
    {
26 115
        if ($pType == '' || $pTarget == '') {
27
            throw new \Exception("Invalid parameters passed.");
28
        }
29 115
        if (strpos($pId, 'rId') === false) {
30 115
            $pId = 'rId' . $pId;
31 115
        }
32
33
        // Write relationship
34 115
        $objWriter->startElement('Relationship');
35 115
        $objWriter->writeAttribute('Id', $pId);
36 115
        $objWriter->writeAttribute('Type', $pType);
37 115
        $objWriter->writeAttribute('Target', $pTarget);
38
39 115
        if ($pTargetMode != '') {
40 5
            $objWriter->writeAttribute('TargetMode', $pTargetMode);
41 5
        }
42
43 115
        $objWriter->endElement();
44 115
    }
45
46
    /**
47
     * Write Border
48
     *
49
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
50
     * @param  \PhpOffice\PhpPresentation\Style\Border     $pBorder      Border
51
     * @param  string                         $pElementName Element name
52
     * @throws \Exception
53
     */
54 80
    protected function writeBorder(XMLWriter $objWriter, $pBorder, $pElementName = 'L')
55
    {
56 80
        if (!($pBorder instanceof Border)) {
57
            return;
58
        }
59
60 80
        if ($pBorder->getLineStyle() == Border::LINE_NONE && $pElementName == '') {
61 34
            return;
62
        }
63
64
        // Line style
65 47
        $lineStyle = $pBorder->getLineStyle();
66 47
        if ($lineStyle == Border::LINE_NONE) {
67 10
            $lineStyle = Border::LINE_SINGLE;
68 10
        }
69
70
        // Line width
71 47
        $lineWidth = 12700 * $pBorder->getLineWidth();
72
73
        // a:ln $pElementName
74 47
        $objWriter->startElement('a:ln' . $pElementName);
75 47
        $objWriter->writeAttribute('w', $lineWidth);
76 47
        $objWriter->writeAttribute('cap', 'flat');
77 47
        $objWriter->writeAttribute('cmpd', $lineStyle);
78 47
        $objWriter->writeAttribute('algn', 'ctr');
79
80
        // Fill?
81 47
        if ($pBorder->getLineStyle() == Border::LINE_NONE) {
82
            // a:noFill
83 10
            $objWriter->writeElement('a:noFill', null);
84 10
        } else {
85
            // a:solidFill
86 47
            $objWriter->startElement('a:solidFill');
87 47
            $this->writeColor($objWriter, $pBorder->getColor());
88 47
            $objWriter->endElement();
89
        }
90
91
        // Dash
92 47
        $objWriter->startElement('a:prstDash');
93 47
        $objWriter->writeAttribute('val', $pBorder->getDashStyle());
94 47
        $objWriter->endElement();
95
96
        // a:round
97 47
        $objWriter->writeElement('a:round', null);
98
99
        // a:headEnd
100 47
        $objWriter->startElement('a:headEnd');
101 47
        $objWriter->writeAttribute('type', 'none');
102 47
        $objWriter->writeAttribute('w', 'med');
103 47
        $objWriter->writeAttribute('len', 'med');
104 47
        $objWriter->endElement();
105
106
        // a:tailEnd
107 47
        $objWriter->startElement('a:tailEnd');
108 47
        $objWriter->writeAttribute('type', 'none');
109 47
        $objWriter->writeAttribute('w', 'med');
110 47
        $objWriter->writeAttribute('len', 'med');
111 47
        $objWriter->endElement();
112
113 47
        $objWriter->endElement();
114 47
    }
115
116
    /**
117
     * @param XMLWriter $objWriter
118
     * @param Color $color
119
     * @param int|null $alpha
120
     */
121 54
    protected function writeColor(XMLWriter $objWriter, Color $color, $alpha = null)
122
    {
123 54
        if (is_null($alpha)) {
124 52
            $alpha = $color->getAlpha();
125 52
        }
126
127
        // a:srgbClr
128 54
        $objWriter->startElement('a:srgbClr');
129 54
        $objWriter->writeAttribute('val', $color->getRGB());
130
131
        // a:alpha
132 54
        if ($alpha != "100") {
133 4
            $objWriter->startElement('a:alpha');
134 4
            $objWriter->writeAttribute('val', $alpha . '%');
135 4
            $objWriter->endElement();
136 4
        }
137
138 54
        $objWriter->endElement();
139 54
    }
140
141
    /**
142
     * Write Fill
143
     *
144
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
145
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
146
     * @throws \Exception
147
     */
148 79
    protected function writeFill(XMLWriter $objWriter, $pFill)
149
    {
150 79
        if (! $pFill instanceof Fill) {
151
            return;
152
        }
153
154
        // Is it a fill?
155 79
        if ($pFill->getFillType() == Fill::FILL_NONE) {
156 71
            $objWriter->writeElement('a:noFill');
157 71
            return;
158
        }
159
160
        // Is it a solid fill?
161 18
        if ($pFill->getFillType() == Fill::FILL_SOLID) {
162 13
            $this->writeSolidFill($objWriter, $pFill);
163 13
            return;
164
        }
165
166
        // Check if this is a pattern type or gradient type
167 5
        if ($pFill->getFillType() == Fill::FILL_GRADIENT_LINEAR || $pFill->getFillType() == Fill::FILL_GRADIENT_PATH) {
168
            // Gradient fill
169 4
            $this->writeGradientFill($objWriter, $pFill);
170 4
        } else {
171
            // Pattern fill
172 1
            $this->writePatternFill($objWriter, $pFill);
173
        }
174 5
    }
175
176
    /**
177
     * Write Solid Fill
178
     *
179
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
180
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
181
     * @throws \Exception
182
     */
183 13
    protected function writeSolidFill(XMLWriter $objWriter, Fill $pFill)
184
    {
185
        // a:gradFill
186 13
        $objWriter->startElement('a:solidFill');
187 13
        $this->writeColor($objWriter, $pFill->getStartColor());
188 13
        $objWriter->endElement();
189 13
    }
190
191
    /**
192
     * Write Gradient Fill
193
     *
194
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
195
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
196
     * @throws \Exception
197
     */
198 4
    protected function writeGradientFill(XMLWriter $objWriter, Fill $pFill)
199
    {
200
        // a:gradFill
201 4
        $objWriter->startElement('a:gradFill');
202
203
        // a:gsLst
204 4
        $objWriter->startElement('a:gsLst');
205
        // a:gs
206 4
        $objWriter->startElement('a:gs');
207 4
        $objWriter->writeAttribute('pos', '0%');
208 4
        $this->writeColor($objWriter, $pFill->getStartColor());
209 4
        $objWriter->endElement();
210
211
        // a:gs
212 4
        $objWriter->startElement('a:gs');
213 4
        $objWriter->writeAttribute('pos', '100%');
214 4
        $this->writeColor($objWriter, $pFill->getEndColor());
215 4
        $objWriter->endElement();
216
217 4
        $objWriter->endElement();
218
219
        // a:lin
220 4
        $objWriter->startElement('a:lin');
221 4
        $objWriter->writeAttribute('ang', CommonDrawing::degreesToAngle($pFill->getRotation()));
222 4
        $objWriter->writeAttribute('scaled', '0');
223 4
        $objWriter->endElement();
224
225 4
        $objWriter->endElement();
226 4
    }
227
228
    /**
229
     * Write Pattern Fill
230
     *
231
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
232
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
233
     * @throws \Exception
234
     */
235 1
    protected function writePatternFill(XMLWriter $objWriter, Fill $pFill)
236
    {
237
        // a:pattFill
238 1
        $objWriter->startElement('a:pattFill');
239
240
        // fgClr
241 1
        $objWriter->startElement('a:fgClr');
242
243 1
        $this->writeColor($objWriter, $pFill->getStartColor());
244
245 1
        $objWriter->endElement();
246
247
        // bgClr
248 1
        $objWriter->startElement('a:bgClr');
249
250 1
        $this->writeColor($objWriter, $pFill->getEndColor());
251
252 1
        $objWriter->endElement();
253
254 1
        $objWriter->endElement();
255 1
    }
256
257
    /**
258
     * Write Outline
259
     * @param XMLWriter $objWriter
260
     * @param Outline $oOutline
261
     */
262 28
    protected function writeOutline(XMLWriter $objWriter, $oOutline)
263
    {
264 28
        if (!$oOutline instanceof Outline) {
265 16
            return;
266
        }
267
        // Width : pts
268 28
        $width = $oOutline->getWidth();
269
        // Width : pts => px
270 28
        $width = CommonDrawing::pointsToPixels($width);
271
        // Width : px => emu
272 28
        $width = CommonDrawing::pixelsToEmu($width);
273
274
        // a:ln
275 28
        $objWriter->startElement('a:ln');
276 28
        $objWriter->writeAttribute('w', $width);
277
278
        // Fill
279 28
        $this->writeFill($objWriter, $oOutline->getFill());
280
281
        // > a:ln
282 28
        $objWriter->endElement();
283 28
    }
284
285
    /**
286
     * Determine absolute zip path
287
     *
288
     * @param  string $path
289
     * @return string
290
     */
291
    protected function absoluteZipPath($path)
292
    {
293
        $path      = str_replace(array(
294
            '/',
295
            '\\'
296
        ), DIRECTORY_SEPARATOR, $path);
297
        $parts     = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
298
        $absolutes = array();
299
        foreach ($parts as $part) {
300
            if ('.' == $part) {
301
                continue;
302
            }
303
            if ('..' == $part) {
304
                array_pop($absolutes);
305
            } else {
306
                $absolutes[] = $part;
307
            }
308
        }
309
310
        return implode('/', $absolutes);
311
    }
312
}
313