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

Source for file NumberFormat.php

Documentation is available at NumberFormat.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_Style
  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.  
  33. /**
  34.  * PHPExcel_Style_NumberFormat
  35.  *
  36.  * @category   PHPExcel
  37.  * @package    PHPExcel_Style
  38.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  39.  */
  40. class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
  41. {    
  42.     /* Pre-defined formats */
  43.     const FORMAT_GENERAL                    'General';
  44.     
  45.     const FORMAT_NUMBER                        '0';
  46.     const FORMAT_NUMBER_00                    '0.00';
  47.     const FORMAT_NUMBER_COMMA_SEPARATED1    '#,##0.00';
  48.     const FORMAT_NUMBER_COMMA_SEPARATED2    '#,##0.00_-';
  49.     
  50.     const FORMAT_PERCENTAGE                    '0%';
  51.     const FORMAT_PERCENTAGE_00                '0.00%';
  52.     
  53.     const FORMAT_DATE_YYYYMMDD                'yy-mm-dd';
  54.     const FORMAT_DATE_DDMMYYYY                'dd/mm/yy';
  55.     const FORMAT_DATE_DMYSLASH                'd/m/y';
  56.     const FORMAT_DATE_DMYMINUS                'd-m-y';
  57.     const FORMAT_DATE_DMMINUS                'd-m';
  58.     const FORMAT_DATE_MYMINUS                'm-y';
  59.     const FORMAT_DATE_DATETIME                'd/m/y h:mm';
  60.     const FORMAT_DATE_TIME1                    'h:mm AM/PM';
  61.     const FORMAT_DATE_TIME2                    'h:mm:ss AM/PM';
  62.     const FORMAT_DATE_TIME3                    'h:mm';
  63.     const FORMAT_DATE_TIME4                    'h:mm:ss';
  64.     const FORMAT_DATE_TIME5                    'mm:ss';
  65.     const FORMAT_DATE_TIME6                    'h:mm:ss';
  66.     const FORMAT_DATE_TIME7                    'i:s.S';
  67.     const FORMAT_DATE_TIME8                    'h:mm:ss;@';
  68.     const FORMAT_DATE_YYYYMMDDSLASH            'yy/mm/dd;@';
  69.     
  70.     const FORMAT_CURRENCY_USD_SIMPLE        '"$"#,##0.00_-';
  71.     const FORMAT_CURRENCY_USD                '$#,##0_-';
  72.     const FORMAT_CURRENCY_EUR_SIMPLE        '[$EUR ]#,##0.00_-';
  73.     
  74.     /**
  75.      * Excel built-in number formats
  76.      *
  77.      * @var array 
  78.      */
  79.     private static $_builtInFormats;
  80.     
  81.     /**
  82.      * Format Code
  83.      *
  84.      * @var string 
  85.      */
  86.     private $_formatCode;
  87.  
  88.     /**
  89.      * Parent Style
  90.      *
  91.      * @var PHPExcel_Style 
  92.      */
  93.      
  94.     private $_parent;
  95.     
  96.     /**
  97.      * Parent Borders
  98.      *
  99.      * @var _parentPropertyName string
  100.      */
  101.     private $_parentPropertyName;
  102.         
  103.     /**
  104.      * Create a new PHPExcel_Style_NumberFormat
  105.      */
  106.     public function __construct()
  107.     {
  108.         // Initialise values
  109.         $this->_formatCode            = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  110.     }
  111.  
  112.     /**
  113.      * Property Prepare bind
  114.      *
  115.      * Configures this object for late binding as a property of a parent object
  116.      *     
  117.      * @param $parent 
  118.      * @param $parentPropertyName 
  119.      */
  120.     public function propertyPrepareBind($parent$parentPropertyName)
  121.     {
  122.         // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  123.         // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  124.         $this->_parent                 = $parent;
  125.         $this->_parentPropertyName    = $parentPropertyName;
  126.     }
  127.     
  128.     /**
  129.      * Property Get Bound
  130.      *
  131.      * Returns the PHPExcel_Style_NumberFormat that is actual bound to PHPExcel_Style
  132.      *
  133.      * @return PHPExcel_Style_NumberFormat 
  134.      */
  135.     private function propertyGetBound({
  136.         if(!isset($this->_parent))
  137.             return $this;                                                                // I am bound
  138.  
  139.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  140.             return $this->_parent->getNumberFormat();                                    // Another one is bound
  141.  
  142.         return $this;                                                                    // No one is bound yet
  143.     }
  144.     
  145.     /**
  146.      * Property Begin Bind
  147.      *
  148.      * If no PHPExcel_Style_NumberFormat has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  149.      *
  150.      * @return PHPExcel_Style_NumberFormat 
  151.      */
  152.     private function propertyBeginBind({
  153.         if(!isset($this->_parent))
  154.             return $this;                                                                // I am already bound
  155.  
  156.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  157.             return $this->_parent->getNumberFormat();                                    // Another one is already bound
  158.             
  159.         $this->_parent->propertyCompleteBind($this$this->_parentPropertyName);        // Bind myself
  160.         $this->_parent = null;
  161.         
  162.         return $this;
  163.     }
  164.     
  165.     /**
  166.      * Apply styles from array
  167.      * 
  168.      * <code>
  169.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray(
  170.      *         array(
  171.      *             'code' => PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE
  172.      *         )
  173.      * );
  174.      * </code>
  175.      * 
  176.      * @param    array    $pStyles    Array containing style information
  177.      * @throws    Exception
  178.      */
  179.     public function applyFromArray($pStyles null{
  180.         if (is_array($pStyles)) {
  181.             if (array_key_exists('code'$pStyles)) {
  182.                 $this->setFormatCode($pStyles['code']);
  183.             }
  184.         else {
  185.             throw new Exception("Invalid style array passed.");
  186.         }
  187.     }
  188.     
  189.     /**
  190.      * Get Format Code
  191.      *
  192.      * @return string 
  193.      */
  194.     public function getFormatCode({
  195.         return $this->propertyGetBound()->_formatCode;
  196.     }
  197.     
  198.     /**
  199.      * Set Format Code
  200.      *
  201.      * @param string $pValue 
  202.      */
  203.     public function setFormatCode($pValue PHPExcel_Style_NumberFormat::FORMAT_GENERAL{
  204.         if ($pValue == ''{
  205.             $pValue PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  206.         }
  207.         $this->propertyBeginBind()->_formatCode $pValue;
  208.     }
  209.     
  210.     /**
  211.      * Get built-in format code
  212.      * 
  213.      * @param    int        $pIndex 
  214.      * @return    string 
  215.      */
  216.     public static function builtInFormatCode($pIndex{
  217.         // Clean parameter
  218.         $pIndex intval($pIndex);
  219.         
  220.         // Built-in format codes
  221.         if (is_null(self::$_builtInFormats)) {
  222.             self::$_builtInFormats array();
  223.             
  224.             // General
  225.             self::$_builtInFormats[0'General';
  226.             self::$_builtInFormats[1'0';
  227.             self::$_builtInFormats[2'0.00';
  228.             self::$_builtInFormats[3'#,##0';
  229.             self::$_builtInFormats[4'#,##0.00';
  230.             self::$_builtInFormats[9'0%';
  231.             self::$_builtInFormats[10'0.00%';
  232.             self::$_builtInFormats[11'0.00E+00';
  233.             self::$_builtInFormats[12'# ?/?';
  234.             self::$_builtInFormats[13'# ??/??';
  235.             self::$_builtInFormats[14'mm-dd-yy';
  236.             self::$_builtInFormats[15'd-mmm-yy';
  237.             self::$_builtInFormats[16'd-mmm';
  238.             self::$_builtInFormats[17'mmm-yy';
  239.             self::$_builtInFormats[18'h:mm AM/PM';
  240.             self::$_builtInFormats[19'h:mm:ss AM/PM';
  241.             self::$_builtInFormats[20'h:mm';
  242.             self::$_builtInFormats[21'h:mm:ss';
  243.             self::$_builtInFormats[22'm/d/yy h:mm';
  244.             self::$_builtInFormats[37'#,##0 ;(#,##0)';
  245.             self::$_builtInFormats[38'#,##0 ;[Red](#,##0)';
  246.             self::$_builtInFormats[39'#,##0.00;(#,##0.00)';
  247.             self::$_builtInFormats[40'#,##0.00;[Red](#,##0.00)';
  248.             self::$_builtInFormats[45'mm:ss';
  249.             self::$_builtInFormats[46'[h]:mm:ss';
  250.             self::$_builtInFormats[47'mmss.0';
  251.             self::$_builtInFormats[48'##0.0E+0';
  252.             self::$_builtInFormats[49'@';
  253.  
  254.             // CHT
  255.             self::$_builtInFormats[27'[$-404]e/m/d';
  256.             self::$_builtInFormats[30'm/d/yy';
  257.             self::$_builtInFormats[36'[$-404]e/m/d';
  258.             self::$_builtInFormats[50'[$-404]e/m/d';
  259.             self::$_builtInFormats[57'[$-404]e/m/d';
  260.             
  261.             // THA
  262.             self::$_builtInFormats[59't0';
  263.             self::$_builtInFormats[60't0.00';
  264.             self::$_builtInFormats[61't#,##0';
  265.             self::$_builtInFormats[62't#,##0.00';
  266.             self::$_builtInFormats[67't0%';
  267.             self::$_builtInFormats[68't0.00%';
  268.             self::$_builtInFormats[69't# ?/?';
  269.             self::$_builtInFormats[70't# ??/??';
  270.         }
  271.         
  272.         if (array_key_exists($pIndexself::$_builtInFormats)) {
  273.             return self::$_builtInFormats[$pIndex];
  274.         }
  275.         
  276.         return '';
  277.     }
  278.  
  279.     /**
  280.      * Get hash code
  281.      *
  282.      * @return string    Hash code
  283.      */    
  284.     public function getHashCode({
  285.         $property $this->propertyGetBound();
  286.         return md5(
  287.               $property->_formatCode
  288.             . __CLASS__
  289.         );
  290.     }
  291.     
  292.     /**
  293.      * Hash index
  294.      *
  295.      * @var string 
  296.      */
  297.     private $_hashIndex;
  298.     
  299.     /**
  300.      * Get hash index
  301.      * 
  302.      * Note that this index may vary during script execution! Only reliable moment is
  303.      * while doing a write of a workbook and when changes are not allowed.
  304.      *
  305.      * @return string    Hash index
  306.      */
  307.     public function getHashIndex({
  308.         return $this->_hashIndex;
  309.     }
  310.     
  311.     /**
  312.      * Set hash index
  313.      * 
  314.      * Note that this index may vary during script execution! Only reliable moment is
  315.      * while doing a write of a workbook and when changes are not allowed.
  316.      *
  317.      * @param string    $value    Hash index
  318.      */
  319.     public function setHashIndex($value{
  320.         $this->_hashIndex = $value;
  321.     }
  322.         
  323.     /**
  324.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  325.      */
  326.     public function __clone({
  327.         $vars get_object_vars($this);
  328.         foreach ($vars as $key => $value{
  329.             if (is_object($value)) {
  330.                 $this->$key clone $value;
  331.             else {
  332.                 $this->$key $value;
  333.             }
  334.         }
  335.     }
  336.     
  337.     /**
  338.      * Convert a value in a pre-defined format to a PHP string
  339.      *
  340.      * @param mixed     $value        Value to format
  341.      * @param string     $format        Format code
  342.      * @return string    Formatted string
  343.      */
  344.     public static function toFormattedString($value ''$format ''{
  345.         if (preg_match("/^[hmsdy]/i"$format)) // custom datetime format
  346.             // dvc: convert Excel formats to PHP date formats
  347.             // first remove escapes related to non-format characters
  348.             $format str_replace('\\'''$format);
  349.  
  350.             // 4-digit year
  351.             $format str_replace('yyyy''Y'$format);
  352.  
  353.             // 2-digit year
  354.             $format str_replace('yy''y'$format);
  355.  
  356.             // first letter of month - no php equivalent
  357.             $format str_replace('mmmmm''M'$format);
  358.  
  359.             // full month name
  360.             $format str_replace('mmmm''F'$format);
  361.  
  362.             // short month name
  363.             $format str_replace('mmm''M'$format);
  364.  
  365.             // mm is minutes if time or month w/leading zero
  366.             $format str_replace(':mm'':i'$format);
  367.  
  368.             // tmp place holder
  369.             $format str_replace('mm''x'$format);
  370.  
  371.             // month no leading zero
  372.             $format str_replace('m''n'$format);
  373.  
  374.             // month leading zero
  375.             $format str_replace('x''m'$format);
  376.  
  377.             // 12-hour suffix
  378.             $format str_replace('AM/PM''A'$format);
  379.  
  380.             // tmp place holder
  381.             $format str_replace('dd''x'$format);
  382.  
  383.             // days no leading zero
  384.             $format str_replace('d''j'$format);
  385.  
  386.             // days leading zero
  387.             $format str_replace('x''d'$format);
  388.  
  389.             // seconds
  390.             $format str_replace('ss''s'$format);
  391.  
  392.             // fractional seconds - no php equivalent
  393.             $format str_replace('.S'''$format);
  394.  
  395.             if (!strpos($format,'A')) // 24-hour format
  396.                 $format str_replace('h''H'$format);
  397.             }
  398.             
  399.             // user defined flag symbol????
  400.             $format str_replace(';@'''$format);
  401.             
  402.             return date($format($value));
  403.             
  404.         else if (preg_match('/%$/'$format)) // % number format
  405.             if (preg_match('/\.[#0]+/i',$format,$m)) {
  406.                 $s substr($m[0],0,1).(strlen($m[0])-1);
  407.                 $format str_replace($m[0],$s,$format);
  408.             }
  409.             if (preg_match('/^[#0]+/',$format,$m)) {
  410.                 $format str_replace($m[0],strlen($m[0]),$format);
  411.             }
  412.             $format '%' str_replace('%',"f%%",$format);
  413.             
  414.             return sprintf($format$value);
  415.             
  416.         else {
  417.             if (preg_match ("/^([0-9.,-]+)$/"$value)) {
  418.                 switch ($format{
  419.                     case self::FORMAT_NUMBER:
  420.                         return sprintf('%1.0f'$value);
  421.                     case self::FORMAT_NUMBER_00:
  422.                         return sprintf('%1.2f'$value);
  423.                         
  424.                     case self::FORMAT_NUMBER_COMMA_SEPARATED1:
  425.                     case self::FORMAT_NUMBER_COMMA_SEPARATED2:
  426.                         return number_format($value2',''.');
  427.                         
  428.                     case self::FORMAT_PERCENTAGE:
  429.                         return round( (100 $value)0'%';
  430.                     case self::FORMAT_PERCENTAGE_00:
  431.                         return round( (100 $value)2'%';
  432.                         
  433.                     case self::FORMAT_DATE_YYYYMMDD:
  434.                         return date('Y-m-d'($value));
  435.                     case self::FORMAT_DATE_DDMMYYYY:
  436.                         return date('d/m/Y'($value));
  437.                     case 'yyyy/mm/dd;@':
  438.                         return date('Y/m/d'($value));
  439.                         
  440.                     case self::FORMAT_CURRENCY_USD_SIMPLE:
  441.                         return '$' number_format($value2);
  442.                     case self::FORMAT_CURRENCY_USD:
  443.                         return '$' number_format($value);
  444.                      case self::FORMAT_CURRENCY_EUR_SIMPLE:
  445.                          return 'EUR ' sprintf('%1.2f'$value);
  446.                 }
  447.             }
  448.         
  449.             return $value;
  450.         }
  451.     }
  452. }

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