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