Completed
Push — develop ( 8fcf27...f63b7f )
by Franck
10s
created

AbstractDecoratorWriter   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 294
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 87.22%

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 27
c 9
b 0
f 1
lcom 1
cbo 7
dl 0
loc 294
ccs 116
cts 133
cp 0.8722
rs 10

9 Methods

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