Completed
Push — develop ( f5141e...bd86d7 )
by Franck
18:00
created

AbstractDecoratorWriter::writeRelationship()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

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