Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
PreserveText
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getFontStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParagraphStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getText
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\Element;
20
21use PhpOffice\PhpWord\Shared\Text as SharedText;
22use PhpOffice\PhpWord\Style\Font;
23use PhpOffice\PhpWord\Style\Paragraph;
24
25/**
26 * Preserve text/field element.
27 */
28class PreserveText extends AbstractElement
29{
30    /**
31     * Text content.
32     *
33     * @var null|array|string
34     */
35    private $text;
36
37    /**
38     * Text style.
39     *
40     * @var null|Font|string
41     */
42    private $fontStyle;
43
44    /**
45     * Paragraph style.
46     *
47     * @var null|Paragraph|string
48     */
49    private $paragraphStyle;
50
51    /**
52     * Create a new Preserve Text Element.
53     *
54     * @param string $text
55     * @param mixed $fontStyle
56     * @param mixed $paragraphStyle
57     */
58    public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
59    {
60        $this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
61        $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
62
63        $this->text = SharedText::toUTF8($text);
64        $matches = preg_split('/({.*?})/', $this->text ?? '', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
65        if (isset($matches[0])) {
66            $this->text = $matches;
67        }
68    }
69
70    /**
71     * Get Text style.
72     *
73     * @return null|Font|string
74     */
75    public function getFontStyle()
76    {
77        return $this->fontStyle;
78    }
79
80    /**
81     * Get Paragraph style.
82     *
83     * @return null|Paragraph|string
84     */
85    public function getParagraphStyle()
86    {
87        return $this->paragraphStyle;
88    }
89
90    /**
91     * Get Text content.
92     *
93     * @return null|array|string
94     */
95    public function getText()
96    {
97        return $this->text;
98    }
99}