Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.91% |
50 / 55 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
Paragraph | |
90.91% |
50 / 55 |
|
50.00% |
2 / 4 |
16.19 | |
0.00% |
0 / 1 |
write | |
100.00% |
44 / 44 |
|
100.00% |
1 / 1 |
9 | |||
writeIndentation | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
4.12 | |||
writeTabs | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 | |||
setNestedLevel | |
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\RTF\Style; |
19 | |
20 | use PhpOffice\PhpWord\SimpleType\Jc; |
21 | |
22 | /** |
23 | * RTF paragraph style writer. |
24 | * |
25 | * @since 0.11.0 |
26 | */ |
27 | class Paragraph extends AbstractStyle |
28 | { |
29 | /** |
30 | * Depth of table container nested level; Primarily used for RTF writer/reader. |
31 | * |
32 | * 0 = Not in a table; 1 = in a table; 2 = in a table inside another table, etc. |
33 | * |
34 | * @var int |
35 | */ |
36 | private $nestedLevel = 0; |
37 | |
38 | private const LEFT = Jc::LEFT; |
39 | private const RIGHT = Jc::RIGHT; |
40 | private const JUSTIFY = Jc::JUSTIFY; |
41 | |
42 | /** |
43 | * Write style. |
44 | * |
45 | * @return string |
46 | */ |
47 | public function write() |
48 | { |
49 | $style = $this->getStyle(); |
50 | if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) { |
51 | return ''; |
52 | } |
53 | |
54 | $alignments = [ |
55 | Jc::START => '\ql', |
56 | Jc::END => '\qr', |
57 | Jc::CENTER => '\qc', |
58 | Jc::BOTH => '\qj', |
59 | self::LEFT => '\ql', |
60 | self::RIGHT => '\qr', |
61 | self::JUSTIFY => '\qj', |
62 | ]; |
63 | $bidiAlignments = [ |
64 | Jc::START => '\qr', |
65 | Jc::END => '\ql', |
66 | Jc::CENTER => '\qc', |
67 | Jc::BOTH => '\qj', |
68 | self::LEFT => '\ql', |
69 | self::RIGHT => '\qr', |
70 | self::JUSTIFY => '\qj', |
71 | ]; |
72 | |
73 | $spaceAfter = $style->getSpaceAfter(); |
74 | $spaceBefore = $style->getSpaceBefore(); |
75 | |
76 | $content = ''; |
77 | if ($this->nestedLevel == 0) { |
78 | $content .= '\pard\nowidctlpar '; |
79 | } |
80 | $alignment = $style->getAlignment(); |
81 | $bidi = $style->isBidi(); |
82 | if ($alignment === '' && $bidi !== null) { |
83 | $alignment = Jc::START; |
84 | } |
85 | if (isset($alignments[$alignment])) { |
86 | $content .= $bidi ? $bidiAlignments[$alignment] : $alignments[$alignment]; |
87 | } |
88 | $content .= $this->writeIndentation($style->getIndentation()); |
89 | $content .= $this->getValueIf($spaceBefore !== null, '\sb' . round($spaceBefore ?? 0)); |
90 | $content .= $this->getValueIf($spaceAfter !== null, '\sa' . round($spaceAfter ?? 0)); |
91 | $lineHeight = $style->getLineHeight(); |
92 | if ($lineHeight) { |
93 | $lineHeightAdjusted = (int) ($lineHeight * 240); |
94 | $content .= "\\sl$lineHeightAdjusted\\slmult1"; |
95 | } |
96 | if ($style->hasPageBreakBefore()) { |
97 | $content .= '\\page'; |
98 | } |
99 | |
100 | $styles = $style->getStyleValues(); |
101 | $content .= $this->writeTabs($styles['tabs']); |
102 | |
103 | return $content; |
104 | } |
105 | |
106 | /** |
107 | * Writes an \PhpOffice\PhpWord\Style\Indentation. |
108 | * |
109 | * @param null|\PhpOffice\PhpWord\Style\Indentation $indent |
110 | * |
111 | * @return string |
112 | */ |
113 | private function writeIndentation($indent = null) |
114 | { |
115 | if (isset($indent) && $indent instanceof \PhpOffice\PhpWord\Style\Indentation) { |
116 | $writer = new Indentation($indent); |
117 | |
118 | return $writer->write(); |
119 | } |
120 | |
121 | return ''; |
122 | } |
123 | |
124 | /** |
125 | * Writes tabs. |
126 | * |
127 | * @param \PhpOffice\PhpWord\Style\Tab[] $tabs |
128 | * |
129 | * @return string |
130 | */ |
131 | private function writeTabs($tabs = null) |
132 | { |
133 | $content = ''; |
134 | if (!empty($tabs)) { |
135 | foreach ($tabs as $tab) { |
136 | $styleWriter = new Tab($tab); |
137 | $content .= $styleWriter->write(); |
138 | } |
139 | } |
140 | |
141 | return $content; |
142 | } |
143 | |
144 | /** |
145 | * Set nested level. |
146 | * |
147 | * @param int $value |
148 | */ |
149 | public function setNestedLevel($value): void |
150 | { |
151 | $this->nestedLevel = $value; |
152 | } |
153 | } |