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