Completed
Pull Request — develop (#322)
by Thomas
08:13 queued 03:00
created

AbstractDecoratorWriter   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
lcom 1
cbo 7
dl 0
loc 298
ccs 117
cts 135
cp 0.8667
rs 10
c 1
b 0
f 0

9 Methods

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