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