Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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      public function __construct(Cell $pCell = null)
  27      {
  28          // Initialise variables
  29          $this->richTextElements = [];
  30  
  31          // Rich-Text string attached to cell?
  32          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      }
  44  
  45      /**
  46       * Add text.
  47       *
  48       * @param ITextElement $pText Rich text element
  49       *
  50       * @return RichText
  51       */
  52      public function addText(ITextElement $pText)
  53      {
  54          $this->richTextElements[] = $pText;
  55  
  56          return $this;
  57      }
  58  
  59      /**
  60       * Create text.
  61       *
  62       * @param string $pText Text
  63       *
  64       * @throws Exception
  65       *
  66       * @return TextElement
  67       */
  68      public function createText($pText)
  69      {
  70          $objText = new TextElement($pText);
  71          $this->addText($objText);
  72  
  73          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      public function createTextRun($pText)
  86      {
  87          $objText = new Run($pText);
  88          $this->addText($objText);
  89  
  90          return $objText;
  91      }
  92  
  93      /**
  94       * Get plain text.
  95       *
  96       * @return string
  97       */
  98      public function getPlainText()
  99      {
 100          // Return value
 101          $returnValue = '';
 102  
 103          // Loop through all ITextElements
 104          foreach ($this->richTextElements as $text) {
 105              $returnValue .= $text->getText();
 106          }
 107  
 108          return $returnValue;
 109      }
 110  
 111      /**
 112       * Convert to string.
 113       *
 114       * @return string
 115       */
 116      public function __toString()
 117      {
 118          return $this->getPlainText();
 119      }
 120  
 121      /**
 122       * Get Rich Text elements.
 123       *
 124       * @return ITextElement[]
 125       */
 126      public function getRichTextElements()
 127      {
 128          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      public function getHashCode()
 151      {
 152          $hashElements = '';
 153          foreach ($this->richTextElements as $element) {
 154              $hashElements .= $element->getHashCode();
 155          }
 156  
 157          return md5(
 158              $hashElements .
 159              __CLASS__
 160          );
 161      }
 162  
 163      /**
 164       * Implement PHP __clone to create a deep clone, not just a shallow copy.
 165       */
 166      public function __clone()
 167      {
 168          $vars = get_object_vars($this);
 169          foreach ($vars as $key => $value) {
 170              if (is_object($value)) {
 171                  $this->$key = clone $value;
 172              } else {
 173                  $this->$key = $value;
 174              }
 175          }
 176      }
 177  }