PHPExcel_RichText
[ class tree: PHPExcel_RichText ] [ index: PHPExcel_RichText ] [ all elements ]

Source for file RichText.php

Documentation is available at RichText.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_RichText
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  26.  */
  27.  
  28.  
  29. /** PHPExcel_IComparable */
  30. require_once 'PHPExcel/IComparable.php';
  31.  
  32. /** PHPExcel_Cell */
  33. require_once 'PHPExcel/Cell.php';
  34.  
  35. /** PHPExcel_RichText_ITextElement */
  36. require_once 'PHPExcel/RichText/ITextElement.php';
  37.  
  38. /** PHPExcel_RichText_TextElement */
  39. require_once 'PHPExcel/RichText/TextElement.php';
  40.  
  41. /** PHPExcel_RichText_Run */
  42. require_once 'PHPExcel/RichText/Run.php';
  43.  
  44. /** PHPExcel_Style_Font */
  45. require_once 'PHPExcel/Style/Font.php';
  46.  
  47. /**
  48.  * PHPExcel_RichText
  49.  *
  50.  * @category   PHPExcel
  51.  * @package    PHPExcel_RichText
  52.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  53.  */
  54. class PHPExcel_RichText implements PHPExcel_IComparable
  55. {
  56.     /**
  57.      * Rich text elements
  58.      *
  59.      * @var PHPExcel_RichText_ITextElement[] 
  60.      */
  61.     private $_richTextElements;
  62.     
  63.     /**
  64.      * Parent cell
  65.      *
  66.      * @var PHPExcel_Cell 
  67.      */
  68.     private $_parent;
  69.        
  70.     /**
  71.      * Create a new PHPExcel_RichText instance
  72.      *
  73.      * @param     PHPExcel_Cell    $pParent 
  74.      * @throws    Exception
  75.      */
  76.     public function __construct(PHPExcel_Cell $pCell null)
  77.     {
  78.         // Initialise variables
  79.         $this->_richTextElements = array();
  80.         
  81.         // Set parent?
  82.         if (!is_null($pCell)) {
  83.             // Set parent cell
  84.             $this->_parent = $pCell;
  85.                 
  86.             // Add cell text and style
  87.             if ($this->_parent->getValue(!= ""{
  88.                 $objRun new PHPExcel_RichText_Run($this->_parent->getValue());
  89.                 $objRun->setFont(clone $this->_parent->getParent()->getStyle($this->_parent->getCoordinate())->getFont());
  90.                 $this->addText($objRun);
  91.             }
  92.                 
  93.             // Set parent value
  94.             $this->_parent->setValue($this);
  95.         }
  96.     }
  97.     
  98.     /**
  99.      * Add text
  100.      *
  101.      * @param     PHPExcel_RichText_ITextElement        $pText        Rich text element
  102.      * @throws     Exception
  103.      */
  104.     public function addText(PHPExcel_RichText_ITextElement $pText null)
  105.     {
  106.         $this->_richTextElements[$pText;
  107.     }
  108.     
  109.     /**
  110.      * Create text
  111.      *
  112.      * @param     string    $pText    Text
  113.      * @return    PHPExcel_RichText_TextElement 
  114.      * @throws     Exception
  115.      */
  116.     public function createText($pText '')
  117.     {
  118.         $objText new PHPExcel_RichText_TextElement($pText);
  119.         $this->addText($objText);
  120.         return $objText;
  121.     }
  122.     
  123.     /**
  124.      * Create text run
  125.      *
  126.      * @param     string    $pText    Text
  127.      * @return    PHPExcel_RichText_Run 
  128.      * @throws     Exception
  129.      */
  130.     public function createTextRun($pText '')
  131.     {
  132.         $objText new PHPExcel_RichText_Run($pText);
  133.         $this->addText($objText);
  134.         return $objText;
  135.     }
  136.     
  137.     /**
  138.      * Get plain text
  139.      *
  140.      * @return string 
  141.      */
  142.     public function getPlainText()
  143.     {
  144.         // Return value
  145.         $returnValue '';
  146.         
  147.         // Loop trough all PHPExcel_RichText_ITextElement
  148.         foreach ($this->_richTextElements as $text{
  149.             $returnValue .= $text->getText();
  150.         }
  151.         
  152.         // Return
  153.         return $returnValue;
  154.     }
  155.     
  156.     /**
  157.      * Get Rich Text elements
  158.      *
  159.      * @return PHPExcel_RichText_ITextElement[] 
  160.      */
  161.     public function getRichTextElements()
  162.     {
  163.         return $this->_richTextElements;
  164.     }
  165.     
  166.     /**
  167.      * Set Rich Text elements
  168.      *
  169.      * @param     PHPExcel_RichText_ITextElement[]    $pElements        Array of elements
  170.      * @throws     Exception
  171.      */
  172.     public function setRichTextElements($pElements null)
  173.     {
  174.         if (is_array($pElements)) {
  175.             $this->_richTextElements = $pElements;
  176.         else {
  177.             throw new Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  178.         }
  179.     }
  180.  
  181.     /**
  182.      * Get parent
  183.      *
  184.      * @return PHPExcel_Cell 
  185.      */
  186.     public function getParent({
  187.         return $this->_parent;
  188.     }
  189.     
  190.     /**
  191.      * Set parent
  192.      *
  193.      * @param PHPExcel_Cell    $value 
  194.      */
  195.     public function setParent(PHPExcel_Cell $value{
  196.         // Set parent
  197.         $this->_parent = $value;
  198.         
  199.         // Set parent value
  200.         $this->_parent->setValue($this);
  201.         
  202.         // Verify style information
  203.  
  204.         $sheet $this->_parent->getParent();
  205.         $cellFont $sheet->getStyle($this->_parent->getCoordinate())->getFont();
  206.         foreach ($this->getRichTextElements(as $element{
  207.             if (!($element instanceof PHPExcel_RichText_Run)) continue;
  208.             
  209.             if ($element->getFont()->getHashCode(== $sheet->getDefaultStyle()->getFont()->getHashCode()) {
  210.                 if ($element->getFont()->getHashCode(!= $cellFont->getHashCode()) {
  211.                     $element->setFont(clone $cellFont);
  212.                 }
  213.             }
  214.         }
  215.     }
  216.     
  217.     /**
  218.      * Get hash code
  219.      *
  220.      * @return string    Hash code
  221.      */    
  222.     public function getHashCode({
  223.         $hashElements '';
  224.         foreach ($this->_richTextElements as $element{
  225.             $hashElements .= $element->getHashCode();
  226.         }
  227.         
  228.         return md5(
  229.               $hashElements
  230.             . __CLASS__
  231.         );
  232.     }
  233.     
  234.     /**
  235.      * Hash index
  236.      *
  237.      * @var string 
  238.      */
  239.     private $_hashIndex;
  240.     
  241.     /**
  242.      * Get hash index
  243.      * 
  244.      * Note that this index may vary during script execution! Only reliable moment is
  245.      * while doing a write of a workbook and when changes are not allowed.
  246.      *
  247.      * @return string    Hash index
  248.      */
  249.     public function getHashIndex({
  250.         return $this->_hashIndex;
  251.     }
  252.     
  253.     /**
  254.      * Set hash index
  255.      * 
  256.      * Note that this index may vary during script execution! Only reliable moment is
  257.      * while doing a write of a workbook and when changes are not allowed.
  258.      *
  259.      * @param string    $value    Hash index
  260.      */
  261.     public function setHashIndex($value{
  262.         $this->_hashIndex = $value;
  263.     }
  264.     
  265.     /**
  266.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  267.      */
  268.     public function __clone({
  269.         $vars get_object_vars($this);
  270.         foreach ($vars as $key => $value{
  271.             if ($key == '_parent'continue;
  272.             
  273.             if (is_object($value)) {
  274.                 $this->$key clone $value;
  275.             else {
  276.                 $this->$key $value;
  277.             }
  278.         }
  279.     }
  280. }

Documentation generated on Mon, 05 Jan 2009 20:38:27 +0100 by phpDocumentor 1.4.1