PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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