Completed
Push — develop ( c1ddc2...097d50 )
by Franck
10:30 queued 08:06
created

AbstractDecoratorWriter   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 7
dl 0
loc 299
ccs 112
cts 128
cp 0.875
rs 10
c 0
b 0
f 0

9 Methods

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