Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
48 / 48
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Shape
100.00% covered (success)
100.00%
48 / 48
100.00% covered (success)
100.00%
7 / 7
15
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
6
 writeArc
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 writeCurve
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 writeLine
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 writePolyline
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 writeRoundRect
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPoints
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Writer\Word2007\Element;
19
20use PhpOffice\PhpWord\Element\Shape as ShapeElement;
21use PhpOffice\PhpWord\Shared\XMLWriter;
22use PhpOffice\PhpWord\Style\Shape as ShapeStyle;
23use PhpOffice\PhpWord\Writer\Word2007\Style\Shape as ShapeStyleWriter;
24
25/**
26 * Shape element writer.
27 *
28 * @since 0.12.0
29 *
30 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
31 */
32class Shape extends AbstractElement
33{
34    /**
35     * Write element.
36     */
37    public function write(): void
38    {
39        $xmlWriter = $this->getXmlWriter();
40        $element = $this->getElement();
41        if (!$element instanceof ShapeElement) {
42            return;
43        }
44
45        $style = $element->getStyle();
46        $styleWriter = new ShapeStyleWriter($xmlWriter, $style);
47
48        $type = $element->getType();
49        if ($type == 'rect' && $style->getRoundness() !== null) {
50            $type = 'roundrect';
51        }
52        $method = "write{$type}";
53
54        if (!$this->withoutP) {
55            $xmlWriter->startElement('w:p');
56        }
57        $this->writeCommentRangeStart();
58
59        $xmlWriter->startElement('w:r');
60        $xmlWriter->startElement('w:pict');
61        $xmlWriter->startElement("v:{$type}");
62
63        // Element style
64        if (method_exists($this, $method)) {
65            $this->$method($xmlWriter, $style);
66        }
67
68        // Child style
69        $styleWriter->write();
70
71        $xmlWriter->endElement(); // v:$type
72        $xmlWriter->endElement(); // w:pict
73        $xmlWriter->endElement(); // w:r
74
75        $this->endElementP(); // w:p
76    }
77
78    /**
79     * Write arc.
80     */
81    private function writeArc(XMLWriter $xmlWriter, ShapeStyle $style): void
82    {
83        $points = $this->getPoints('arc', $style->getPoints());
84
85        $xmlWriter->writeAttributeIf($points['start'] !== null, 'startAngle', $points['start']);
86        $xmlWriter->writeAttributeIf($points['end'] !== null, 'endAngle', $points['end']);
87    }
88
89    /**
90     * Write curve.
91     */
92    private function writeCurve(XMLWriter $xmlWriter, ShapeStyle $style): void
93    {
94        $points = $this->getPoints('curve', $style->getPoints());
95
96        $this->writeLine($xmlWriter, $style);
97        $xmlWriter->writeAttributeIf($points['point1'] !== null, 'control1', $points['point1']);
98        $xmlWriter->writeAttributeIf($points['point2'] !== null, 'control2', $points['point2']);
99    }
100
101    /**
102     * Write line.
103     */
104    private function writeLine(XMLWriter $xmlWriter, ShapeStyle $style): void
105    {
106        $points = $this->getPoints('line', $style->getPoints());
107
108        $xmlWriter->writeAttributeIf($points['start'] !== null, 'from', $points['start']);
109        $xmlWriter->writeAttributeIf($points['end'] !== null, 'to', $points['end']);
110    }
111
112    /**
113     * Write polyline.
114     */
115    private function writePolyline(XMLWriter $xmlWriter, ShapeStyle $style): void
116    {
117        $xmlWriter->writeAttributeIf($style->getPoints() !== null, 'points', $style->getPoints());
118    }
119
120    /**
121     * Write rectangle.
122     */
123    private function writeRoundRect(XMLWriter $xmlWriter, ShapeStyle $style): void
124    {
125        $xmlWriter->writeAttribute('arcsize', $style->getRoundness());
126    }
127
128    /**
129     * Set points.
130     *
131     * @param string $type
132     * @param string $value
133     *
134     * @return array
135     */
136    private function getPoints($type, $value)
137    {
138        $points = [];
139
140        switch ($type) {
141            case 'arc':
142            case 'line':
143                $points = explode(' ', $value);
144                [$start, $end] = array_pad($points, 2, null);
145                $points = ['start' => $start, 'end' => $end];
146
147                break;
148            case 'curve':
149                $points = explode(' ', $value);
150                [$start, $end, $point1, $point2] = array_pad($points, 4, null);
151                $points = ['start' => $start, 'end' => $end, 'point1' => $point1, 'point2' => $point2];
152
153                break;
154        }
155
156        return $points;
157    }
158}