Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
72 / 72
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Paragraph
100.00% covered (success)
100.00%
72 / 72
100.00% covered (success)
100.00%
6 / 6
22
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
 writeStyle
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
8
 writeTabs
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 writeNumbering
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
 setWithoutPPR
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIsInline
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Style;
20
21use PhpOffice\PhpWord\Shared\XMLWriter;
22use PhpOffice\PhpWord\Style;
23use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
24use PhpOffice\PhpWord\Writer\Word2007\Element\ParagraphAlignment;
25
26/**
27 * Paragraph style writer.
28 *
29 * @since 0.10.0
30 */
31class Paragraph extends AbstractStyle
32{
33    /**
34     * Without w:pPr.
35     *
36     * @var bool
37     */
38    private $withoutPPR = false;
39
40    /**
41     * Is inline in element.
42     *
43     * @var bool
44     */
45    private $isInline = false;
46
47    /**
48     * Write style.
49     */
50    public function write(): void
51    {
52        $xmlWriter = $this->getXmlWriter();
53
54        $isStyleName = $this->isInline && null !== $this->style && is_string($this->style);
55        if ($isStyleName) {
56            if (!$this->withoutPPR) {
57                $xmlWriter->startElement('w:pPr');
58            }
59            $xmlWriter->startElement('w:pStyle');
60            $xmlWriter->writeAttribute('w:val', $this->style);
61            $xmlWriter->endElement();
62            if (!$this->withoutPPR) {
63                $xmlWriter->endElement();
64            }
65        } else {
66            $this->writeStyle();
67        }
68    }
69
70    /**
71     * Write full style.
72     */
73    private function writeStyle(): void
74    {
75        $style = $this->getStyle();
76        if (!$style instanceof ParagraphStyle) {
77            return;
78        }
79        $xmlWriter = $this->getXmlWriter();
80        $styles = $style->getStyleValues();
81
82        if (!$this->withoutPPR) {
83            $xmlWriter->startElement('w:pPr');
84        }
85
86        // Style name
87        if ($this->isInline === true) {
88            $xmlWriter->writeElementIf($styles['name'] !== null, 'w:pStyle', 'w:val', $styles['name']);
89        }
90
91        // Pagination
92        $xmlWriter->writeElementIf($styles['pagination']['widowControl'] === false, 'w:widowControl', 'w:val', '0');
93        $xmlWriter->writeElementIf($styles['pagination']['keepNext'] === true, 'w:keepNext', 'w:val', '1');
94        $xmlWriter->writeElementIf($styles['pagination']['keepLines'] === true, 'w:keepLines', 'w:val', '1');
95        $xmlWriter->writeElementIf($styles['pagination']['pageBreak'] === true, 'w:pageBreakBefore', 'w:val', '1');
96
97        // Paragraph alignment
98        if ('' !== $styles['alignment']) {
99            $paragraphAlignment = new ParagraphAlignment($styles['alignment']);
100            $xmlWriter->startElement($paragraphAlignment->getName());
101            foreach ($paragraphAlignment->getAttributes() as $attributeName => $attributeValue) {
102                $xmlWriter->writeAttribute($attributeName, $attributeValue);
103            }
104            $xmlWriter->endElement();
105        }
106
107        //Right to left
108        $xmlWriter->writeElementIf($styles['bidi'] === true, 'w:bidi');
109
110        //Paragraph contextualSpacing
111        $xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');
112
113        //Paragraph textAlignment
114        $xmlWriter->writeElementIf($styles['textAlignment'] !== null, 'w:textAlignment', 'w:val', $styles['textAlignment']);
115
116        // Hyphenation
117        $xmlWriter->writeElementIf($styles['suppressAutoHyphens'] === true, 'w:suppressAutoHyphens');
118
119        // Child style: alignment, indentation, spacing, and shading
120        $this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']);
121        $this->writeChildStyle($xmlWriter, 'Spacing', $styles['spacing']);
122        $this->writeChildStyle($xmlWriter, 'Shading', $styles['shading']);
123
124        // Tabs
125        $this->writeTabs($xmlWriter, $styles['tabs']);
126
127        // Numbering
128        $this->writeNumbering($xmlWriter, $styles['numbering']);
129
130        // Border
131        if ($style->hasBorder()) {
132            $xmlWriter->startElement('w:pBdr');
133
134            $styleWriter = new MarginBorder($xmlWriter);
135            $styleWriter->setSizes($style->getBorderSize());
136            $styleWriter->setStyles($style->getBorderStyle());
137            $styleWriter->setColors($style->getBorderColor());
138            $styleWriter->write();
139
140            $xmlWriter->endElement();
141        }
142
143        if (!$this->withoutPPR) {
144            $xmlWriter->endElement(); // w:pPr
145        }
146    }
147
148    /**
149     * Write tabs.
150     *
151     * @param Style\Tab[] $tabs
152     */
153    private function writeTabs(XMLWriter $xmlWriter, $tabs): void
154    {
155        if (!empty($tabs)) {
156            $xmlWriter->startElement('w:tabs');
157            foreach ($tabs as $tab) {
158                $styleWriter = new Tab($xmlWriter, $tab);
159                $styleWriter->write();
160            }
161            $xmlWriter->endElement();
162        }
163    }
164
165    /**
166     * Write numbering.
167     *
168     * @param array $numbering
169     */
170    private function writeNumbering(XMLWriter $xmlWriter, $numbering): void
171    {
172        $numStyle = $numbering['style'];
173        $numLevel = $numbering['level'];
174
175        /** @var Style\Numbering $numbering */
176        $numbering = Style::getStyle($numStyle);
177        if ($numStyle !== null && $numbering !== null) {
178            $xmlWriter->startElement('w:numPr');
179            $xmlWriter->startElement('w:numId');
180            $xmlWriter->writeAttribute('w:val', $numbering->getIndex());
181            $xmlWriter->endElement(); // w:numId
182            $xmlWriter->startElement('w:ilvl');
183            $xmlWriter->writeAttribute('w:val', $numLevel);
184            $xmlWriter->endElement(); // w:ilvl
185            $xmlWriter->endElement(); // w:numPr
186
187            $xmlWriter->startElement('w:outlineLvl');
188            $xmlWriter->writeAttribute('w:val', $numLevel);
189            $xmlWriter->endElement(); // w:outlineLvl
190        }
191    }
192
193    /**
194     * Set without w:pPr.
195     *
196     * @param bool $value
197     */
198    public function setWithoutPPR($value): void
199    {
200        $this->withoutPPR = $value;
201    }
202
203    /**
204     * Set is inline.
205     *
206     * @param bool $value
207     */
208    public function setIsInline($value): void
209    {
210        $this->isInline = $value;
211    }
212}