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