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

AbstractDecoratorWriter::absoluteZipPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 15.664

Importance

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