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 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.2, 2010-01-11
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../');
  35. }
  36.  
  37. /** PHPExcel_IComparable */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/IComparable.php';
  39.  
  40. /** PHPExcel_Cell */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Cell.php';
  42.  
  43. /** PHPExcel_Cell_DataType */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Cell/DataType.php';
  45.  
  46. /** PHPExcel_RichText_ITextElement */
  47. require_once PHPEXCEL_ROOT 'PHPExcel/RichText/ITextElement.php';
  48.  
  49. /** PHPExcel_RichText_TextElement */
  50. require_once PHPEXCEL_ROOT 'PHPExcel/RichText/TextElement.php';
  51.  
  52. /** PHPExcel_RichText_Run */
  53. require_once PHPEXCEL_ROOT 'PHPExcel/RichText/Run.php';
  54.  
  55. /** PHPExcel_Style_Font */
  56. require_once PHPEXCEL_ROOT 'PHPExcel/Style/Font.php';
  57.  
  58. /**
  59.  * PHPExcel_RichText
  60.  *
  61.  * @category   PHPExcel
  62.  * @package    PHPExcel_RichText
  63.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  64.  */
  65. class PHPExcel_RichText implements PHPExcel_IComparable
  66. {
  67.     /**
  68.      * Rich text elements
  69.      *
  70.      * @var PHPExcel_RichText_ITextElement[] 
  71.      */
  72.     private $_richTextElements;
  73.     
  74.     /**
  75.      * Parent cell
  76.      *
  77.      * @var PHPExcel_Cell 
  78.      */
  79.     private $_parent;
  80.        
  81.     /**
  82.      * Create a new PHPExcel_RichText instance
  83.      *
  84.      * @param     PHPExcel_Cell    $pParent 
  85.      * @throws    Exception
  86.      */
  87.     public function __construct(PHPExcel_Cell $pCell null)
  88.     {
  89.         // Initialise variables
  90.         $this->_richTextElements = array();
  91.         
  92.         // Set parent?
  93.         if (!is_null($pCell)) {
  94.             // Set parent cell
  95.             $this->_parent = $pCell;
  96.                 
  97.             // Add cell text and style
  98.             if ($this->_parent->getValue(!= ""{
  99.                 $objRun new PHPExcel_RichText_Run($this->_parent->getValue());
  100.                 $objRun->setFont(clone $this->_parent->getParent()->getStyle($this->_parent->getCoordinate())->getFont());
  101.                 $this->addText($objRun);
  102.             }
  103.                 
  104.             // Set parent value
  105.             $this->_parent->setValueExplicit($thisPHPExcel_Cell_DataType::TYPE_STRING);
  106.         }
  107.     }
  108.     
  109.     /**
  110.      * Add text
  111.      *
  112.      * @param     PHPExcel_RichText_ITextElement        $pText        Rich text element
  113.      * @throws     Exception
  114.      * @return PHPExcel_RichText 
  115.      */
  116.     public function addText(PHPExcel_RichText_ITextElement $pText null)
  117.     {
  118.         $this->_richTextElements[$pText;
  119.         return $this;
  120.     }
  121.     
  122.     /**
  123.      * Create text
  124.      *
  125.      * @param     string    $pText    Text
  126.      * @return    PHPExcel_RichText_TextElement 
  127.      * @throws     Exception
  128.      */
  129.     public function createText($pText '')
  130.     {
  131.         $objText new PHPExcel_RichText_TextElement($pText);
  132.         $this->addText($objText);
  133.         return $objText;
  134.     }
  135.     
  136.     /**
  137.      * Create text run
  138.      *
  139.      * @param     string    $pText    Text
  140.      * @return    PHPExcel_RichText_Run 
  141.      * @throws     Exception
  142.      */
  143.     public function createTextRun($pText '')
  144.     {
  145.         $objText new PHPExcel_RichText_Run($pText);
  146.         $this->addText($objText);
  147.         return $objText;
  148.     }
  149.     
  150.     /**
  151.      * Get plain text
  152.      *
  153.      * @return string 
  154.      */
  155.     public function getPlainText()
  156.     {
  157.         // Return value
  158.         $returnValue '';
  159.         
  160.         // Loop through all PHPExcel_RichText_ITextElement
  161.         foreach ($this->_richTextElements as $text{
  162.             $returnValue .= $text->getText();
  163.         }
  164.         
  165.         // Return
  166.         return $returnValue;
  167.     }
  168.     
  169.     /**
  170.      * Convert to string
  171.      *
  172.      * @return string 
  173.      */
  174.     public function __toString({
  175.         return $this->getPlainText();
  176.     }
  177.     
  178.     /**
  179.      * Get Rich Text elements
  180.      *
  181.      * @return PHPExcel_RichText_ITextElement[] 
  182.      */
  183.     public function getRichTextElements()
  184.     {
  185.         return $this->_richTextElements;
  186.     }
  187.     
  188.     /**
  189.      * Set Rich Text elements
  190.      *
  191.      * @param     PHPExcel_RichText_ITextElement[]    $pElements        Array of elements
  192.      * @throws     Exception
  193.      * @return PHPExcel_RichText 
  194.      */
  195.     public function setRichTextElements($pElements null)
  196.     {
  197.         if (is_array($pElements)) {
  198.             $this->_richTextElements = $pElements;
  199.         else {
  200.             throw new Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  201.         }
  202.         return $this;
  203.     }
  204.  
  205.     /**
  206.      * Get parent
  207.      *
  208.      * @return PHPExcel_Cell 
  209.      */
  210.     public function getParent({
  211.         return $this->_parent;
  212.     }
  213.     
  214.     /**
  215.      * Set parent
  216.      *
  217.      * @param PHPExcel_Cell    $value 
  218.      * @return PHPExcel_RichText 
  219.      */
  220.     public function setParent(PHPExcel_Cell $value{
  221.         // Set parent
  222.         $this->_parent = $value;
  223.         
  224.         // Set parent value
  225.         $this->_parent->setValueExplicit($thisPHPExcel_Cell_DataType::TYPE_STRING);
  226.         
  227.         // Verify style information
  228.  
  229.         $sheet $this->_parent->getParent();
  230.         $cellFont $sheet->getStyle($this->_parent->getCoordinate())->getFont()->getSharedComponent();
  231.         foreach ($this->getRichTextElements(as $element{
  232.             if (!($element instanceof PHPExcel_RichText_Run)) continue;
  233.             
  234.             if ($element->getFont()->getHashCode(== $sheet->getDefaultStyle()->getFont()->getHashCode()) {
  235.                 if ($element->getFont()->getHashCode(!= $cellFont->getHashCode()) {
  236.                     $element->setFont(clone $cellFont);
  237.                 }
  238.             }
  239.         }
  240.         return $this;
  241.     }
  242.     
  243.     /**
  244.      * Get hash code
  245.      *
  246.      * @return string    Hash code
  247.      */    
  248.     public function getHashCode({
  249.         $hashElements '';
  250.         foreach ($this->_richTextElements as $element{
  251.             $hashElements .= $element->getHashCode();
  252.         }
  253.         
  254.         return md5(
  255.               $hashElements
  256.             . __CLASS__
  257.         );
  258.     }
  259.     
  260.     /**
  261.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  262.      */
  263.     public function __clone({
  264.         $vars get_object_vars($this);
  265.         foreach ($vars as $key => $value{
  266.             if ($key == '_parent'continue;
  267.             
  268.             if (is_object($value)) {
  269.                 $this->$key clone $value;
  270.             else {
  271.                 $this->$key $value;
  272.             }
  273.         }
  274.     }
  275. }

Documentation generated on Mon, 11 Jan 2010 08:13:18 +0100 by phpDocumentor 1.4.1