Completed
Pull Request — develop (#186)
by
unknown
07:55
created

AbstractDecoratorWriter   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 326
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.28%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 26
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 326
ccs 113
cts 128
cp 0.8828
rs 10

14 Methods

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