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