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.7, 2009-04-22
  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.      * Convert to string
  158.      *
  159.      * @return string 
  160.      */
  161.     public function __toString({
  162.         return $this->getPlainText();
  163.     }
  164.     
  165.     /**
  166.      * Get Rich Text elements
  167.      *
  168.      * @return PHPExcel_RichText_ITextElement[] 
  169.      */
  170.     public function getRichTextElements()
  171.     {
  172.         return $this->_richTextElements;
  173.     }
  174.     
  175.     /**
  176.      * Set Rich Text elements
  177.      *
  178.      * @param     PHPExcel_RichText_ITextElement[]    $pElements        Array of elements
  179.      * @throws     Exception
  180.      */
  181.     public function setRichTextElements($pElements null)
  182.     {
  183.         if (is_array($pElements)) {
  184.             $this->_richTextElements = $pElements;
  185.         else {
  186.             throw new Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  187.         }
  188.     }
  189.  
  190.     /**
  191.      * Get parent
  192.      *
  193.      * @return PHPExcel_Cell 
  194.      */
  195.     public function getParent({
  196.         return $this->_parent;
  197.     }
  198.     
  199.     /**
  200.      * Set parent
  201.      *
  202.      * @param PHPExcel_Cell    $value 
  203.      */
  204.     public function setParent(PHPExcel_Cell $value{
  205.         // Set parent
  206.         $this->_parent = $value;
  207.         
  208.         // Set parent value
  209.         $this->_parent->setValue($this);
  210.         
  211.         // Verify style information
  212.  
  213.         $sheet $this->_parent->getParent();
  214.         $cellFont $sheet->getStyle($this->_parent->getCoordinate())->getFont();
  215.         foreach ($this->getRichTextElements(as $element{
  216.             if (!($element instanceof PHPExcel_RichText_Run)) continue;
  217.             
  218.             if ($element->getFont()->getHashCode(== $sheet->getDefaultStyle()->getFont()->getHashCode()) {
  219.                 if ($element->getFont()->getHashCode(!= $cellFont->getHashCode()) {
  220.                     $element->setFont(clone $cellFont);
  221.                 }
  222.             }
  223.         }
  224.     }
  225.     
  226.     /**
  227.      * Get hash code
  228.      *
  229.      * @return string    Hash code
  230.      */    
  231.     public function getHashCode({
  232.         $hashElements '';
  233.         foreach ($this->_richTextElements as $element{
  234.             $hashElements .= $element->getHashCode();
  235.         }
  236.         
  237.         return md5(
  238.               $hashElements
  239.             . __CLASS__
  240.         );
  241.     }
  242.     
  243.     /**
  244.      * Hash index
  245.      *
  246.      * @var string 
  247.      */
  248.     private $_hashIndex;
  249.     
  250.     /**
  251.      * Get hash index
  252.      * 
  253.      * Note that this index may vary during script execution! Only reliable moment is
  254.      * while doing a write of a workbook and when changes are not allowed.
  255.      *
  256.      * @return string    Hash index
  257.      */
  258.     public function getHashIndex({
  259.         return $this->_hashIndex;
  260.     }
  261.     
  262.     /**
  263.      * Set hash index
  264.      * 
  265.      * Note that this index may vary during script execution! Only reliable moment is
  266.      * while doing a write of a workbook and when changes are not allowed.
  267.      *
  268.      * @param string    $value    Hash index
  269.      */
  270.     public function setHashIndex($value{
  271.         $this->_hashIndex = $value;
  272.     }
  273.     
  274.     /**
  275.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  276.      */
  277.     public function __clone({
  278.         $vars get_object_vars($this);
  279.         foreach ($vars as $key => $value{
  280.             if ($key == '_parent'continue;
  281.             
  282.             if (is_object($value)) {
  283.                 $this->$key clone $value;
  284.             else {
  285.                 $this->$key $value;
  286.             }
  287.         }
  288.     }
  289. }

Documentation generated on Wed, 22 Apr 2009 09:01:49 +0200 by phpDocumentor 1.4.1