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.3, 2008-08-25
  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                = 'yyyy-mm-dd';
  54.     const FORMAT_DATE_DDMMYYYY                = 'dd/mm/yyyy';
  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:i';
  60.     const FORMAT_DATE_TIME1                    = 'h:i a';
  61.     const FORMAT_DATE_TIME2                    = 'h:i:s a';
  62.     const FORMAT_DATE_TIME3                    = 'H:i';
  63.     const FORMAT_DATE_TIME4                    = 'H:i:s';
  64.     const FORMAT_DATE_TIME5                    = 'i:s';
  65.     const FORMAT_DATE_TIME6                    = 'H:i:s';
  66.     const FORMAT_DATE_TIME7                    = 'i:s.S';
  67.     const FORMAT_DATE_TIME8                    = 'h:mm:ss;@';
  68.     const FORMAT_DATE_YYYYMMDDSLASH            = 'yyyy/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($pIndex, self::$_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 ("/^([0-9.,-]+)$/", $value)) {
  315.             switch ($format) {
  316.                 case self::FORMAT_NUMBER:
  317.                     return sprintf('%1.0f', $value);
  318.                 case self::FORMAT_NUMBER_00:
  319.                     return sprintf('%1.2f', $value);
  320.                     
  321.                 case self::FORMAT_NUMBER_COMMA_SEPARATED1:
  322.                 case self::FORMAT_NUMBER_COMMA_SEPARATED2:
  323.                     return number_format($value, 2, ',', '.');
  324.                     
  325.                 case self::FORMAT_PERCENTAGE:
  326.                     return round( (100 * $value), 0) . '%';
  327.                 case self::FORMAT_PERCENTAGE_00:
  328.                     return round( (100 * $value), 2) . '%';
  329.                     
  330.                 case self::FORMAT_DATE_YYYYMMDD:
  331.                     return date('Y-m-d', (1 * $value));
  332.                 case self::FORMAT_DATE_DDMMYYYY:
  333.                     return date('d/m/Y', (1 * $value));
  334.                 case 'yyyy/mm/dd;@':
  335.                     return date('Y/m/d', (1 * $value));
  336.                     
  337.                 case self::FORMAT_CURRENCY_USD_SIMPLE:
  338.                     return '$' . number_format($value, 2);
  339.                 case self::FORMAT_CURRENCY_USD:
  340.                     return '$' . number_format($value);
  341.                  case self::FORMAT_CURRENCY_EUR_SIMPLE:
  342.                      return 'EUR ' . sprintf('%1.2f', $value);
  343.             }
  344.         }
  345.         
  346.         return $value;
  347.     }
  348. }

Documentation generated on Mon, 25 Aug 2008 13:44:30 +0200 by phpDocumentor 1.4.1