Completed
Pull Request — develop (#216)
by Franck
07:08
created

AbstractDecoratorWriter::absoluteZipPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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