PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.6.2/Classes/PHPExcel/Style/NumberFormat.php

#
PHP | 272 lines | 124 code | 32 blank | 116 comment | 13 complexity | fcdc6c7fa78bb277f374ea44bc544aaf MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  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 ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_IComparable */
  28. require_once 'PHPExcel/IComparable.php';
  29. /**
  30. * PHPExcel_Style_NumberFormat
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Style
  34. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
  37. {
  38. /* Pre-defined formats */
  39. const FORMAT_GENERAL = 'General';
  40. const FORMAT_NUMBER = '0';
  41. const FORMAT_NUMBER_00 = '0.00';
  42. const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00';
  43. const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-';
  44. const FORMAT_PERCENTAGE = '0%';
  45. const FORMAT_PERCENTAGE_00 = '0.00%';
  46. const FORMAT_DATE_YYYYMMDD = 'yyyy-mm-dd';
  47. const FORMAT_DATE_DDMMYYYY = 'dd/mm/yyyy';
  48. const FORMAT_DATE_DMYSLASH = 'd/m/Y';
  49. const FORMAT_DATE_DMYMINUS = 'd-M-Y';
  50. const FORMAT_DATE_DMMINUS = 'd-M';
  51. const FORMAT_DATE_MYMINUS = 'M-Y';
  52. const FORMAT_DATE_DATETIME = 'd/m/Y H:i';
  53. const FORMAT_DATE_TIME1 = 'h:i a';
  54. const FORMAT_DATE_TIME2 = 'h:i:s a';
  55. const FORMAT_DATE_TIME3 = 'H:i';
  56. const FORMAT_DATE_TIME4 = 'H:i:s';
  57. const FORMAT_DATE_TIME5 = 'i:s';
  58. const FORMAT_DATE_TIME6 = 'H:i:s';
  59. const FORMAT_DATE_TIME7 = 'i:s.S';
  60. const FORMAT_DATE_TIME8 = 'h:mm:ss;@';
  61. const FORMAT_DATE_YYYYMMDDSLASH = 'yyyy/mm/dd;@';
  62. const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-';
  63. const FORMAT_CURRENCY_USD = '$#,##0_-';
  64. const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-';
  65. /**
  66. * Format Code
  67. *
  68. * @var string
  69. */
  70. private $_formatCode;
  71. /**
  72. * Parent Style
  73. *
  74. * @var PHPExcel_Style
  75. */
  76. private $_parent;
  77. /**
  78. * Parent Borders
  79. *
  80. * @var _parentPropertyName string
  81. */
  82. private $_parentPropertyName;
  83. /**
  84. * Create a new PHPExcel_Style_NumberFormat
  85. */
  86. public function __construct()
  87. {
  88. // Initialise values
  89. $this->_formatCode = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  90. }
  91. /**
  92. * Property Prepare bind
  93. *
  94. * Configures this object for late binding as a property of a parent object
  95. *
  96. * @param $parent
  97. * @param $parentPropertyName
  98. */
  99. public function propertyPrepareBind($parent, $parentPropertyName)
  100. {
  101. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  102. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  103. $this->_parent = $parent;
  104. $this->_parentPropertyName = $parentPropertyName;
  105. }
  106. /**
  107. * Property Get Bound
  108. *
  109. * Returns the PHPExcel_Style_NumberFormat that is actual bound to PHPExcel_Style
  110. *
  111. * @return PHPExcel_Style_NumberFormat
  112. */
  113. private function propertyGetBound() {
  114. if(!isset($this->_parent))
  115. return $this; // I am bound
  116. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  117. return $this->_parent->getNumberFormat(); // Another one is bound
  118. return $this; // No one is bound yet
  119. }
  120. /**
  121. * Property Begin Bind
  122. *
  123. * If no PHPExcel_Style_NumberFormat has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  124. *
  125. * @return PHPExcel_Style_NumberFormat
  126. */
  127. private function propertyBeginBind() {
  128. if(!isset($this->_parent))
  129. return $this; // I am already bound
  130. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  131. return $this->_parent->getNumberFormat(); // Another one is already bound
  132. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  133. $this->_parent = null;
  134. return $this;
  135. }
  136. /**
  137. * Apply styles from array
  138. *
  139. * <code>
  140. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray(
  141. * array(
  142. * 'code' => PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE
  143. * )
  144. * );
  145. * </code>
  146. *
  147. * @param array $pStyles Array containing style information
  148. * @throws Exception
  149. */
  150. public function applyFromArray($pStyles = null) {
  151. if (is_array($pStyles)) {
  152. if (array_key_exists('code', $pStyles)) {
  153. $this->setFormatCode($pStyles['code']);
  154. }
  155. } else {
  156. throw new Exception("Invalid style array passed.");
  157. }
  158. }
  159. /**
  160. * Get Format Code
  161. *
  162. * @return string
  163. */
  164. public function getFormatCode() {
  165. return $this->propertyGetBound()->_formatCode;
  166. }
  167. /**
  168. * Set Format Code
  169. *
  170. * @param string $pValue
  171. */
  172. public function setFormatCode($pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL) {
  173. if ($pValue == '') {
  174. $pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  175. }
  176. $this->propertyBeginBind()->_formatCode = $pValue;
  177. }
  178. /**
  179. * Get hash code
  180. *
  181. * @return string Hash code
  182. */
  183. public function getHashCode() {
  184. $property = $this->propertyGetBound();
  185. return md5(
  186. $property->_formatCode
  187. . __CLASS__
  188. );
  189. }
  190. /**
  191. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  192. */
  193. public function __clone() {
  194. $vars = get_object_vars($this);
  195. foreach ($vars as $key => $value) {
  196. if (is_object($value)) {
  197. $this->$key = clone $value;
  198. } else {
  199. $this->$key = $value;
  200. }
  201. }
  202. }
  203. /**
  204. * Convert a value in a pre-defined format to a PHP string
  205. *
  206. * @param mixed $value Value to format
  207. * @param string $format Format code
  208. * @return string Formatted string
  209. */
  210. public static function toFormattedString($value = '', $format = '') {
  211. if (preg_match ("/^([0-9.,-]+)$/", $value)) {
  212. switch ($format) {
  213. case self::FORMAT_NUMBER:
  214. return sprintf('%1.0f', $value);
  215. case self::FORMAT_NUMBER_00:
  216. return sprintf('%1.2f', $value);
  217. case self::FORMAT_NUMBER_COMMA_SEPARATED1:
  218. case self::FORMAT_NUMBER_COMMA_SEPARATED2:
  219. return number_fromat($value, 2, ',', '.');
  220. case self::FORMAT_PERCENTAGE:
  221. return round( (100 * $value), 0) . '%';
  222. case self::FORMAT_PERCENTAGE_00:
  223. return round( (100 * $value), 2) . '%';
  224. case self::FORMAT_DATE_YYYYMMDD:
  225. return date('Y-m-d', (1 * $value));
  226. case self::FORMAT_DATE_DDMMYYYY:
  227. return date('d/m/Y', (1 * $value));
  228. case 'yyyy/mm/dd;@':
  229. return date('Y/m/d', (1 * $value));
  230. case self::FORMAT_CURRENCY_USD_SIMPLE:
  231. return '$' . number_format($value, 2);
  232. case self::FORMAT_CURRENCY_USD:
  233. return '$' . number_format($value);
  234. case self::FORMAT_CURRENCY_EUR_SIMPLE:
  235. return 'EUR ' . sprintf('%1.2f', $value);
  236. }
  237. }
  238. return $value;
  239. }
  240. }