Completed
Pull Request — develop (#207)
by Franck
16:14 queued 07:46
created

AbstractDecoratorWriter   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 87.88%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 25
c 6
b 0
f 0
lcom 1
cbo 7
dl 0
loc 298
ccs 116
cts 132
cp 0.8788
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B writeRelationship() 0 22 5
A writeSolidFill() 0 12 1
B writeGradientFill() 0 39 1
B writePatternFill() 0 27 1
A writeOutline() 0 22 2
B writeBorder() 0 66 6
B writeFill() 0 22 5
A absoluteZipPath() 0 21 4
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 94
            }
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 4
            }
39
40 94
            $objWriter->endElement();
41 94
        } 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, $pBorder, $pElementName = 'L')
55
    {
56
        if (!($pBorder instanceof Border)) {
57 35
            return;
58 35
        }
59 9
60 9
        if ($pBorder->getLineStyle() == Border::LINE_NONE && $pElementName == '') {
61
            return;
62
        }
63 35
64
        // Line style
65
        $lineStyle = $pBorder->getLineStyle();
66 35
        if ($lineStyle == Border::LINE_NONE) {
67 35
            $lineStyle = Border::LINE_SINGLE;
68 35
        }
69 35
70 35
        // Line width
71
        $lineWidth = 12700 * $pBorder->getLineWidth();
72
73 35
        // a:ln $pElementName
74
        $objWriter->startElement('a:ln' . $pElementName);
75 9
        $objWriter->writeAttribute('w', $lineWidth);
76 9
        $objWriter->writeAttribute('cap', 'flat');
77
        $objWriter->writeAttribute('cmpd', $lineStyle);
78 35
        $objWriter->writeAttribute('algn', 'ctr');
79
80
        // Fill?
81 35
        if ($pBorder->getLineStyle() == Border::LINE_NONE) {
82 35
            // a:noFill
83 35
            $objWriter->writeElement('a:noFill', null);
84
        } else {
85 35
            // a:solidFill
86
            $objWriter->startElement('a:solidFill');
87
88
            // a:srgbClr
89 35
            $objWriter->startElement('a:srgbClr');
90 35
            $objWriter->writeAttribute('val', $pBorder->getColor()->getRGB());
91 35
            $objWriter->endElement();
92
93
            $objWriter->endElement();
94 35
        }
95
96
        // Dash
97 35
        $objWriter->startElement('a:prstDash');
98 35
        $objWriter->writeAttribute('val', $pBorder->getDashStyle());
99 35
        $objWriter->endElement();
100 35
101 35
        // a:round
102
        $objWriter->writeElement('a:round', null);
103
104 35
        // a:headEnd
105 35
        $objWriter->startElement('a:headEnd');
106 35
        $objWriter->writeAttribute('type', 'none');
107 35
        $objWriter->writeAttribute('w', 'med');
108 35
        $objWriter->writeAttribute('len', 'med');
109
        $objWriter->endElement();
110 35
111 35
        // a:tailEnd
112
        $objWriter->startElement('a:tailEnd');
113
        $objWriter->writeAttribute('type', 'none');
114
        $objWriter->writeAttribute('w', 'med');
115
        $objWriter->writeAttribute('len', 'med');
116
        $objWriter->endElement();
117
118
        $objWriter->endElement();
119
    }
120 54
121
    /**
122
     * Write Fill
123 54
     *
124 47
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
125
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
126
     * @throws \Exception
127
     */
128 14
    protected function writeFill(XMLWriter $objWriter, Fill $pFill)
129 9
    {
130 9
        // Is it a fill?
131
        if ($pFill->getFillType() == Fill::FILL_NONE) {
132
            return;
133
        }
134 5
135
        // Is it a solid fill?
136 4
        if ($pFill->getFillType() == Fill::FILL_SOLID) {
137 4
            $this->writeSolidFill($objWriter, $pFill);
138
            return;
139 1
        }
140
141 5
        // Check if this is a pattern type or gradient type
142
        if ($pFill->getFillType() == Fill::FILL_GRADIENT_LINEAR || $pFill->getFillType() == Fill::FILL_GRADIENT_PATH) {
143
            // Gradient fill
144
            $this->writeGradientFill($objWriter, $pFill);
145
        } else {
146
            // Pattern fill
147
            $this->writePatternFill($objWriter, $pFill);
148
        }
149
    }
150 9
151
    /**
152
     * Write Solid Fill
153 9
     *
154
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
155
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
156 9
     * @throws \Exception
157 9
     */
158 9
    protected function writeSolidFill(XMLWriter $objWriter, Fill $pFill)
159
    {
160 9
        // a:gradFill
161 9
        $objWriter->startElement('a:solidFill');
162
163
        // srgbClr
164
        $objWriter->startElement('a:srgbClr');
165
        $objWriter->writeAttribute('val', $pFill->getStartColor()->getRGB());
166
        $objWriter->endElement();
167
168
        $objWriter->endElement();
169
    }
170 4
171
    /**
172
     * Write Gradient Fill
173 4
     *
174
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
175
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
176 4
     * @throws \Exception
177
     */
178 4
    protected function writeGradientFill(XMLWriter $objWriter, Fill $pFill)
179 4
    {
180
        // a:gradFill
181
        $objWriter->startElement('a:gradFill');
182 4
183 4
        // a:gsLst
184 4
        $objWriter->startElement('a:gsLst');
185
        // a:gs
186 4
        $objWriter->startElement('a:gs');
187
        $objWriter->writeAttribute('pos', '0');
188
189 4
        // srgbClr
190 4
        $objWriter->startElement('a:srgbClr');
191
        $objWriter->writeAttribute('val', $pFill->getStartColor()->getRGB());
192
        $objWriter->endElement();
193 4
194 4
        $objWriter->endElement();
195 4
196
        // a:gs
197 4
        $objWriter->startElement('a:gs');
198
        $objWriter->writeAttribute('pos', '100000');
199 4
200
        // srgbClr
201
        $objWriter->startElement('a:srgbClr');
202 4
        $objWriter->writeAttribute('val', $pFill->getEndColor()->getRGB());
203 4
        $objWriter->endElement();
204 4
205 4
        $objWriter->endElement();
206
207 4
        $objWriter->endElement();
208 4
209
        // a:lin
210
        $objWriter->startElement('a:lin');
211
        $objWriter->writeAttribute('ang', CommonDrawing::degreesToAngle($pFill->getRotation()));
212
        $objWriter->writeAttribute('scaled', '0');
213
        $objWriter->endElement();
214
215
        $objWriter->endElement();
216
    }
217 1
218
    /**
219
     * Write Pattern Fill
220 1
     *
221
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
222
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
223 1
     * @throws \Exception
224
     */
225
    protected function writePatternFill(XMLWriter $objWriter, Fill $pFill)
226 1
    {
227 1
        // a:pattFill
228 1
        $objWriter->startElement('a:pattFill');
229
230 1
        // fgClr
231
        $objWriter->startElement('a:fgClr');
232
233 1
        // srgbClr
234
        $objWriter->startElement('a:srgbClr');
235
        $objWriter->writeAttribute('val', $pFill->getStartColor()->getRGB());
236 1
        $objWriter->endElement();
237 1
238 1
        $objWriter->endElement();
239
240 1
        // bgClr
241
        $objWriter->startElement('a:bgClr');
242 1
243 1
        // srgbClr
244
        $objWriter->startElement('a:srgbClr');
245
        $objWriter->writeAttribute('val', $pFill->getEndColor()->getRGB());
246
        $objWriter->endElement();
247
248
        $objWriter->endElement();
249
250 11
        $objWriter->endElement();
251
    }
252 11
253 11
    /**
254
     * Write Outline
255
     * @param XMLWriter $objWriter
256 3
     * @param Outline $oOutline
257
     */
258 3
    protected function writeOutline(XMLWriter $objWriter, $oOutline)
259
    {
260 3
        if (!$oOutline instanceof Outline) {
261
            return;
262
        }
263 3
        // Width : pts
264 3
        $width = $oOutline->getWidth();
265
        // Width : pts => px
266
        $width = CommonDrawing::pointsToPixels($width);
267 3
        // Width : px => emu
268
        $width = CommonDrawing::pixelsToEmu($width);
269
270 3
        // a:ln
271 3
        $objWriter->startElement('a:ln');
272
        $objWriter->writeAttribute('w', $width);
273
274
        // Fill
275
        $this->writeFill($objWriter, $oOutline->getFill());
276
277
        // > a:ln
278
        $objWriter->endElement();
279
    }
280
281
    /**
282
     * Determine absolute zip path
283
     *
284
     * @param  string $path
285
     * @return string
286
     */
287
    protected function absoluteZipPath($path)
288
    {
289
        $path      = str_replace(array(
290
            '/',
291
            '\\'
292
        ), DIRECTORY_SEPARATOR, $path);
293
        $parts     = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
294
        $absolutes = array();
295
        foreach ($parts as $part) {
296
            if ('.' == $part) {
297
                continue;
298
            }
299
            if ('..' == $part) {
300
                array_pop($absolutes);
301 1
            } else {
302
                $absolutes[] = $part;
303
            }
304
        }
305
306
        return implode('/', $absolutes);
307
    }
308
}
309