1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU Lesser General Public |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16
|
|
|
* Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Lesser General Public |
19
|
|
|
* License along with this library; if not, write to the Free Software |
20
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21
|
|
|
* |
22
|
|
|
* @category PhpSpreadsheet |
23
|
|
|
* |
24
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
25
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
26
|
|
|
*/ |
27
|
|
|
class RichText implements IComparable |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Rich text elements. |
31
|
|
|
* |
32
|
|
|
* @var RichText\ITextElement[] |
33
|
|
|
*/ |
34
|
|
|
private $richTextElements; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new RichText instance. |
38
|
|
|
* |
39
|
|
|
* @param Cell $pCell |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
*/ |
43
|
37 |
|
public function __construct(Cell $pCell = null) |
44
|
|
|
{ |
45
|
|
|
// Initialise variables |
46
|
37 |
|
$this->richTextElements = []; |
47
|
|
|
|
48
|
|
|
// Rich-Text string attached to cell? |
49
|
37 |
|
if ($pCell !== null) { |
50
|
|
|
// Add cell text and style |
51
|
|
|
if ($pCell->getValue() != '') { |
52
|
|
|
$objRun = new RichText\Run($pCell->getValue()); |
53
|
|
|
$objRun->setFont(clone $pCell->getWorksheet()->getStyle($pCell->getCoordinate())->getFont()); |
54
|
|
|
$this->addText($objRun); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Set parent value |
58
|
|
|
$pCell->setValueExplicit($this, Cell\DataType::TYPE_STRING); |
59
|
|
|
} |
60
|
37 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add text. |
64
|
|
|
* |
65
|
|
|
* @param RichText\ITextElement $pText Rich text element |
66
|
|
|
* |
67
|
|
|
* @throws Exception |
68
|
|
|
* |
69
|
|
|
* @return RichText |
70
|
|
|
*/ |
71
|
37 |
|
public function addText(RichText\ITextElement $pText) |
72
|
|
|
{ |
73
|
37 |
|
$this->richTextElements[] = $pText; |
74
|
|
|
|
75
|
37 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create text. |
80
|
|
|
* |
81
|
|
|
* @param string $pText Text |
82
|
|
|
* |
83
|
|
|
* @throws Exception |
84
|
|
|
* |
85
|
|
|
* @return RichText\TextElement |
86
|
|
|
*/ |
87
|
23 |
|
public function createText($pText) |
88
|
|
|
{ |
89
|
23 |
|
$objText = new RichText\TextElement($pText); |
90
|
23 |
|
$this->addText($objText); |
91
|
|
|
|
92
|
23 |
|
return $objText; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create text run. |
97
|
|
|
* |
98
|
|
|
* @param string $pText Text |
99
|
|
|
* |
100
|
|
|
* @throws Exception |
101
|
|
|
* |
102
|
|
|
* @return RichText\Run |
103
|
|
|
*/ |
104
|
29 |
|
public function createTextRun($pText) |
105
|
|
|
{ |
106
|
29 |
|
$objText = new RichText\Run($pText); |
107
|
29 |
|
$this->addText($objText); |
108
|
|
|
|
109
|
29 |
|
return $objText; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get plain text. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
13 |
|
public function getPlainText() |
118
|
|
|
{ |
119
|
|
|
// Return value |
120
|
13 |
|
$returnValue = ''; |
121
|
|
|
|
122
|
|
|
// Loop through all RichText\ITextElements |
123
|
13 |
|
foreach ($this->richTextElements as $text) { |
124
|
13 |
|
$returnValue .= $text->getText(); |
125
|
|
|
} |
126
|
|
|
|
127
|
13 |
|
return $returnValue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Convert to string. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
1 |
|
public function __toString() |
136
|
|
|
{ |
137
|
1 |
|
return $this->getPlainText(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get Rich Text elements. |
142
|
|
|
* |
143
|
|
|
* @return RichText\ITextElement[] |
144
|
|
|
*/ |
145
|
31 |
|
public function getRichTextElements() |
146
|
|
|
{ |
147
|
31 |
|
return $this->richTextElements; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set Rich Text elements. |
152
|
|
|
* |
153
|
|
|
* @param RichText\ITextElement[] $textElements Array of elements |
154
|
|
|
* |
155
|
|
|
* @throws Exception |
156
|
|
|
* |
157
|
|
|
* @return RichText |
158
|
|
|
*/ |
159
|
|
|
public function setRichTextElements(array $textElements) |
160
|
|
|
{ |
161
|
|
|
$this->richTextElements = $textElements; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get hash code. |
168
|
|
|
* |
169
|
|
|
* @return string Hash code |
170
|
|
|
*/ |
171
|
10 |
|
public function getHashCode() |
172
|
|
|
{ |
173
|
10 |
|
$hashElements = ''; |
174
|
10 |
|
foreach ($this->richTextElements as $element) { |
175
|
10 |
|
$hashElements .= $element->getHashCode(); |
176
|
|
|
} |
177
|
|
|
|
178
|
10 |
|
return md5( |
179
|
|
|
$hashElements . |
180
|
10 |
|
__CLASS__ |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
186
|
|
|
*/ |
187
|
1 |
View Code Duplication |
public function __clone() |
|
|
|
|
188
|
|
|
{ |
189
|
1 |
|
$vars = get_object_vars($this); |
190
|
1 |
|
foreach ($vars as $key => $value) { |
191
|
1 |
|
if (is_object($value)) { |
192
|
|
|
$this->$key = clone $value; |
193
|
|
|
} else { |
194
|
1 |
|
$this->$key = $value; |
195
|
|
|
} |
196
|
|
|
} |
197
|
1 |
|
} |
198
|
|
|
} |
199
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.