Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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