Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Text
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
7 / 7
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setFontStyle
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 getFontStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParagraphStyle
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getParagraphStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setText
100.00% covered (success)
100.00%
2 / 2
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 * 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
18namespace PhpOffice\PhpWord\Element;
19
20use PhpOffice\PhpWord\Shared\Text as SharedText;
21use PhpOffice\PhpWord\Style\Font;
22use PhpOffice\PhpWord\Style\Paragraph;
23
24/**
25 * Text element.
26 */
27class Text extends AbstractElement
28{
29    /**
30     * Text content.
31     *
32     * @var ?string
33     */
34    protected $text;
35
36    /**
37     * Text style.
38     *
39     * @var \PhpOffice\PhpWord\Style\Font|string
40     */
41    protected $fontStyle;
42
43    /**
44     * Paragraph style.
45     *
46     * @var \PhpOffice\PhpWord\Style\Paragraph|string
47     */
48    protected $paragraphStyle;
49
50    /**
51     * Create a new Text Element.
52     *
53     * @param string $text
54     * @param mixed $fontStyle
55     * @param mixed $paragraphStyle
56     */
57    public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
58    {
59        $this->setText($text);
60        $paragraphStyle = $this->setParagraphStyle($paragraphStyle);
61        $this->setFontStyle($fontStyle, $paragraphStyle);
62    }
63
64    /**
65     * Set Text style.
66     *
67     * @param array|\PhpOffice\PhpWord\Style\Font|string $style
68     * @param array|\PhpOffice\PhpWord\Style\Paragraph|string $paragraphStyle
69     *
70     * @return \PhpOffice\PhpWord\Style\Font|string
71     */
72    public function setFontStyle($style = null, $paragraphStyle = null)
73    {
74        if ($style instanceof Font) {
75            $this->fontStyle = $style;
76            $this->setParagraphStyle($paragraphStyle);
77        } elseif (is_array($style)) {
78            $this->fontStyle = new Font('text', $paragraphStyle);
79            $this->fontStyle->setStyleByArray($style);
80        } elseif (null === $style) {
81            $this->fontStyle = new Font('text', $paragraphStyle);
82        } else {
83            $this->fontStyle = $style;
84            $this->setParagraphStyle($paragraphStyle);
85        }
86
87        return $this->fontStyle;
88    }
89
90    /**
91     * Get Text style.
92     *
93     * @return \PhpOffice\PhpWord\Style\Font|string
94     */
95    public function getFontStyle()
96    {
97        return $this->fontStyle;
98    }
99
100    /**
101     * Set Paragraph style.
102     *
103     * @param array|\PhpOffice\PhpWord\Style\Paragraph|string $style
104     *
105     * @return \PhpOffice\PhpWord\Style\Paragraph|string
106     */
107    public function setParagraphStyle($style = null)
108    {
109        if (is_array($style)) {
110            $this->paragraphStyle = new Paragraph();
111            $this->paragraphStyle->setStyleByArray($style);
112        } elseif ($style instanceof Paragraph) {
113            $this->paragraphStyle = $style;
114        } elseif (null === $style) {
115            $this->paragraphStyle = new Paragraph();
116        } else {
117            $this->paragraphStyle = $style;
118        }
119
120        return $this->paragraphStyle;
121    }
122
123    /**
124     * Get Paragraph style.
125     *
126     * @return \PhpOffice\PhpWord\Style\Paragraph|string
127     */
128    public function getParagraphStyle()
129    {
130        return $this->paragraphStyle;
131    }
132
133    /**
134     * Set text content.
135     *
136     * @param string $text
137     *
138     * @return self
139     */
140    public function setText($text)
141    {
142        $this->text = SharedText::toUTF8($text);
143
144        return $this;
145    }
146
147    /**
148     * Get Text content.
149     *
150     * @return ?string
151     */
152    public function getText()
153    {
154        return $this->text;
155    }
156}