1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\RichText; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\IComparable; |
9
|
|
|
|
10
|
|
|
class RichText implements IComparable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Rich text elements. |
14
|
|
|
* |
15
|
|
|
* @var ITextElement[] |
16
|
|
|
*/ |
17
|
|
|
private $richTextElements; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new RichText instance. |
21
|
|
|
* |
22
|
|
|
* @param Cell $pCell |
23
|
|
|
* |
24
|
|
|
* @throws Exception |
25
|
|
|
*/ |
26
|
45 |
|
public function __construct(Cell $pCell = null) |
27
|
|
|
{ |
28
|
|
|
// Initialise variables |
29
|
45 |
|
$this->richTextElements = []; |
30
|
|
|
|
31
|
|
|
// Rich-Text string attached to cell? |
32
|
45 |
|
if ($pCell !== null) { |
33
|
|
|
// Add cell text and style |
34
|
|
|
if ($pCell->getValue() != '') { |
35
|
|
|
$objRun = new Run($pCell->getValue()); |
36
|
|
|
$objRun->setFont(clone $pCell->getWorksheet()->getStyle($pCell->getCoordinate())->getFont()); |
37
|
|
|
$this->addText($objRun); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Set parent value |
41
|
|
|
$pCell->setValueExplicit($this, DataType::TYPE_STRING); |
42
|
|
|
} |
43
|
45 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add text. |
47
|
|
|
* |
48
|
|
|
* @param ITextElement $pText Rich text element |
49
|
|
|
* |
50
|
|
|
* @return RichText |
51
|
|
|
*/ |
52
|
45 |
|
public function addText(ITextElement $pText) |
53
|
|
|
{ |
54
|
45 |
|
$this->richTextElements[] = $pText; |
55
|
|
|
|
56
|
45 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create text. |
61
|
|
|
* |
62
|
|
|
* @param string $pText Text |
63
|
|
|
* |
64
|
|
|
* @throws Exception |
65
|
|
|
* |
66
|
|
|
* @return TextElement |
67
|
|
|
*/ |
68
|
25 |
|
public function createText($pText) |
69
|
|
|
{ |
70
|
25 |
|
$objText = new TextElement($pText); |
71
|
25 |
|
$this->addText($objText); |
72
|
|
|
|
73
|
25 |
|
return $objText; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create text run. |
78
|
|
|
* |
79
|
|
|
* @param string $pText Text |
80
|
|
|
* |
81
|
|
|
* @throws Exception |
82
|
|
|
* |
83
|
|
|
* @return Run |
84
|
|
|
*/ |
85
|
35 |
|
public function createTextRun($pText) |
86
|
|
|
{ |
87
|
35 |
|
$objText = new Run($pText); |
88
|
35 |
|
$this->addText($objText); |
89
|
|
|
|
90
|
35 |
|
return $objText; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get plain text. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
20 |
|
public function getPlainText() |
99
|
|
|
{ |
100
|
|
|
// Return value |
101
|
20 |
|
$returnValue = ''; |
102
|
|
|
|
103
|
|
|
// Loop through all ITextElements |
104
|
20 |
|
foreach ($this->richTextElements as $text) { |
105
|
20 |
|
$returnValue .= $text->getText(); |
106
|
|
|
} |
107
|
|
|
|
108
|
20 |
|
return $returnValue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Convert to string. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
2 |
|
public function __toString() |
117
|
|
|
{ |
118
|
2 |
|
return $this->getPlainText(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get Rich Text elements. |
123
|
|
|
* |
124
|
|
|
* @return ITextElement[] |
125
|
|
|
*/ |
126
|
31 |
|
public function getRichTextElements() |
127
|
|
|
{ |
128
|
31 |
|
return $this->richTextElements; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set Rich Text elements. |
133
|
|
|
* |
134
|
|
|
* @param ITextElement[] $textElements Array of elements |
135
|
|
|
* |
136
|
|
|
* @return RichText |
137
|
|
|
*/ |
138
|
|
|
public function setRichTextElements(array $textElements) |
139
|
|
|
{ |
140
|
|
|
$this->richTextElements = $textElements; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get hash code. |
147
|
|
|
* |
148
|
|
|
* @return string Hash code |
149
|
|
|
*/ |
150
|
10 |
|
public function getHashCode() |
151
|
|
|
{ |
152
|
10 |
|
$hashElements = ''; |
153
|
10 |
|
foreach ($this->richTextElements as $element) { |
154
|
10 |
|
$hashElements .= $element->getHashCode(); |
155
|
|
|
} |
156
|
|
|
|
157
|
10 |
|
return md5( |
158
|
|
|
$hashElements . |
159
|
10 |
|
__CLASS__ |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
165
|
|
|
*/ |
166
|
1 |
|
public function __clone() |
167
|
|
|
{ |
168
|
1 |
|
$vars = get_object_vars($this); |
169
|
1 |
|
foreach ($vars as $key => $value) { |
170
|
1 |
|
if (is_object($value)) { |
171
|
|
|
$this->$key = clone $value; |
172
|
|
|
} else { |
173
|
1 |
|
$this->$key = $value; |
174
|
|
|
} |
175
|
|
|
} |
176
|
1 |
|
} |
177
|
|
|
} |
178
|
|
|
|