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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.4, 2008-10-27
  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 - 2008 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.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  294.      */
  295.     public function __clone({
  296.         $vars get_object_vars($this);
  297.         foreach ($vars as $key => $value{
  298.             if (is_object($value)) {
  299.                 $this->$key clone $value;
  300.             else {
  301.                 $this->$key $value;
  302.             }
  303.         }
  304.     }
  305.     
  306.     /**
  307.      * Convert a value in a pre-defined format to a PHP string
  308.      *
  309.      * @param mixed     $value        Value to format
  310.      * @param string     $format        Format code
  311.      * @return string    Formatted string
  312.      */
  313.     public static function toFormattedString($value ''$format ''{
  314.         if (preg_match("/^[hmsdy]/i"$format)) // custom datetime format
  315.             // dvc: convert Excel formats to PHP date formats
  316.             // first remove escapes related to non-format characters
  317.             $format str_replace('\\'''$format);
  318.  
  319.             // 4-digit year
  320.             $format str_replace('yyyy''Y'$format);
  321.  
  322.             // 2-digit year
  323.             $format str_replace('yy''y'$format);
  324.  
  325.             // first letter of month - no php equivalent
  326.             $format str_replace('mmmmm''M'$format);
  327.  
  328.             // full month name
  329.             $format str_replace('mmmm''F'$format);
  330.  
  331.             // short month name
  332.             $format str_replace('mmm''M'$format);
  333.  
  334.             // mm is minutes if time or month w/leading zero
  335.             $format str_replace(':mm'':i'$format);
  336.  
  337.             // tmp place holder
  338.             $format str_replace('mm''x'$format);
  339.  
  340.             // month no leading zero
  341.             $format str_replace('m''n'$format);
  342.  
  343.             // month leading zero
  344.             $format str_replace('x''m'$format);
  345.  
  346.             // 12-hour suffix
  347.             $format str_replace('AM/PM''A'$format);
  348.  
  349.             // tmp place holder
  350.             $format str_replace('dd''x'$format);
  351.  
  352.             // days no leading zero
  353.             $format str_replace('d''j'$format);
  354.  
  355.             // days leading zero
  356.             $format str_replace('x''d'$format);
  357.  
  358.             // seconds
  359.             $format str_replace('ss''s'$format);
  360.  
  361.             // fractional seconds - no php equivalent
  362.             $format str_replace('.S'''$format);
  363.  
  364.             if (!strpos($format,'A')) // 24-hour format
  365.                 $format str_replace('h''H'$format);
  366.             }
  367.             
  368.             // user defined flag symbol????
  369.             $format str_replace(';@'''$format);
  370.             
  371.             return date($format($value));
  372.             
  373.         else if (preg_match('/%$/'$format)) // % number format
  374.             if (preg_match('/\.[#0]+/i',$format,$m)) {
  375.                 $s substr($m[0],0,1).(strlen($m[0])-1);
  376.                 $format str_replace($m[0],$s,$format);
  377.             }
  378.             if (preg_match('/^[#0]+/',$format,$m)) {
  379.                 $format str_replace($m[0],strlen($m[0]),$format);
  380.             }
  381.             $format '%' str_replace('%',"f%%",$format);
  382.             
  383.             return sprintf($format$value);
  384.             
  385.         else {
  386.             if (preg_match ("/^([0-9.,-]+)$/"$value)) {
  387.                 switch ($format{
  388.                     case self::FORMAT_NUMBER:
  389.                         return sprintf('%1.0f'$value);
  390.                     case self::FORMAT_NUMBER_00:
  391.                         return sprintf('%1.2f'$value);
  392.                         
  393.                     case self::FORMAT_NUMBER_COMMA_SEPARATED1:
  394.                     case self::FORMAT_NUMBER_COMMA_SEPARATED2:
  395.                         return number_format($value2',''.');
  396.                         
  397.                     case self::FORMAT_PERCENTAGE:
  398.                         return round( (100 $value)0'%';
  399.                     case self::FORMAT_PERCENTAGE_00:
  400.                         return round( (100 $value)2'%';
  401.                         
  402.                     case self::FORMAT_DATE_YYYYMMDD:
  403.                         return date('Y-m-d'($value));
  404.                     case self::FORMAT_DATE_DDMMYYYY:
  405.                         return date('d/m/Y'($value));
  406.                     case 'yyyy/mm/dd;@':
  407.                         return date('Y/m/d'($value));
  408.                         
  409.                     case self::FORMAT_CURRENCY_USD_SIMPLE:
  410.                         return '$' number_format($value2);
  411.                     case self::FORMAT_CURRENCY_USD:
  412.                         return '$' number_format($value);
  413.                      case self::FORMAT_CURRENCY_EUR_SIMPLE:
  414.                          return 'EUR ' sprintf('%1.2f'$value);
  415.                 }
  416.             }
  417.         
  418.             return $value;
  419.         }
  420.     }
  421. }

Documentation generated on Mon, 27 Oct 2008 08:40:23 +0100 by phpDocumentor 1.4.1