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

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