PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/wi6857-memory/Classes/PHPExcel/Style/NumberFormat.php

#
PHP | 497 lines | 277 code | 48 blank | 172 comment | 41 complexity | 3e42c5fd4f382a2d7be2dc884bf6fbe5 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 Borders
  88. *
  89. * @var _parentPropertyName string
  90. */
  91. private $_parentPropertyName;
  92. /**
  93. * Supervisor?
  94. *
  95. * @var boolean
  96. */
  97. private $_isSupervisor;
  98. /**
  99. * Parent. Only used for supervisor
  100. *
  101. * @var PHPExcel_Style
  102. */
  103. private $_parent;
  104. /**
  105. * Create a new PHPExcel_Style_NumberFormat
  106. */
  107. public function __construct($isSupervisor = false)
  108. {
  109. // Supervisor?
  110. $this->_isSupervisor = $isSupervisor;
  111. // Initialise values
  112. $this->_formatCode = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  113. }
  114. /**
  115. * Bind parent. Only used for supervisor
  116. *
  117. * @param PHPExcel_Style $parent
  118. */
  119. public function bindParent($parent)
  120. {
  121. $this->_parent = $parent;
  122. }
  123. /**
  124. * Is this a supervisor or a real style component?
  125. *
  126. * @return boolean
  127. */
  128. public function getIsSupervisor()
  129. {
  130. return $this->_isSupervisor;
  131. }
  132. /**
  133. * Get the shared style component for the currently active cell in currently active sheet.
  134. * Only used for style supervisor
  135. *
  136. * @return PHPExcel_Style_NumberFormat
  137. */
  138. public function getSharedComponent()
  139. {
  140. return $this->_parent->getSharedComponent()->getNumberFormat();
  141. }
  142. /**
  143. * Get the currently active sheet. Only used for supervisor
  144. *
  145. * @return PHPExcel_Worksheet
  146. */
  147. public function getActiveSheet()
  148. {
  149. return $this->_parent->getActiveSheet();
  150. }
  151. /**
  152. * Get the currently active cell coordinate in currently active sheet.
  153. * Only used for supervisor
  154. *
  155. * @return string E.g. 'A1'
  156. */
  157. public function getSelectedCell()
  158. {
  159. return $this->getActiveSheet()->getSelectedCell();
  160. }
  161. /**
  162. * Build style array from subcomponents
  163. *
  164. * @param array $array
  165. * @return array
  166. */
  167. public function getStyleArray($array)
  168. {
  169. return array('numberformat' => $array);
  170. }
  171. /**
  172. * Apply styles from array
  173. *
  174. * <code>
  175. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray(
  176. * array(
  177. * 'code' => PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE
  178. * )
  179. * );
  180. * </code>
  181. *
  182. * @param array $pStyles Array containing style information
  183. * @throws Exception
  184. */
  185. public function applyFromArray($pStyles = null) {
  186. if (is_array($pStyles)) {
  187. if (array_key_exists('code', $pStyles)) {
  188. $this->setFormatCode($pStyles['code']);
  189. }
  190. } else {
  191. throw new Exception("Invalid style array passed.");
  192. }
  193. }
  194. /**
  195. * Get Format Code
  196. *
  197. * @return string
  198. */
  199. public function getFormatCode() {
  200. if ($this->_isSupervisor) {
  201. return $this->getSharedComponent()->getFormatCode();
  202. }
  203. return $this->_formatCode;
  204. }
  205. /**
  206. * Set Format Code
  207. *
  208. * @param string $pValue
  209. */
  210. public function setFormatCode($pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL) {
  211. if ($pValue == '') {
  212. $pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  213. }
  214. if ($this->_isSupervisor) {
  215. $styleArray = $this->getStyleArray(array('code' => $pValue));
  216. $this->getActiveSheet()->duplicateStyleArray($styleArray, $this->getSelectedCell());
  217. } else {
  218. $this->_formatCode = $pValue;
  219. }
  220. }
  221. /**
  222. * Get built-in format code
  223. *
  224. * @param int $pIndex
  225. * @return string
  226. */
  227. public static function builtInFormatCode($pIndex) {
  228. // Clean parameter
  229. $pIndex = intval($pIndex);
  230. // Built-in format codes
  231. if (is_null(self::$_builtInFormats)) {
  232. self::$_builtInFormats = array();
  233. // General
  234. self::$_builtInFormats[0] = 'General';
  235. self::$_builtInFormats[1] = '0';
  236. self::$_builtInFormats[2] = '0.00';
  237. self::$_builtInFormats[3] = '#,##0';
  238. self::$_builtInFormats[4] = '#,##0.00';
  239. self::$_builtInFormats[9] = '0%';
  240. self::$_builtInFormats[10] = '0.00%';
  241. self::$_builtInFormats[11] = '0.00E+00';
  242. self::$_builtInFormats[12] = '# ?/?';
  243. self::$_builtInFormats[13] = '# ??/??';
  244. self::$_builtInFormats[14] = 'mm-dd-yy';
  245. self::$_builtInFormats[15] = 'd-mmm-yy';
  246. self::$_builtInFormats[16] = 'd-mmm';
  247. self::$_builtInFormats[17] = 'mmm-yy';
  248. self::$_builtInFormats[18] = 'h:mm AM/PM';
  249. self::$_builtInFormats[19] = 'h:mm:ss AM/PM';
  250. self::$_builtInFormats[20] = 'h:mm';
  251. self::$_builtInFormats[21] = 'h:mm:ss';
  252. self::$_builtInFormats[22] = 'm/d/yy h:mm';
  253. self::$_builtInFormats[37] = '#,##0 ;(#,##0)';
  254. self::$_builtInFormats[38] = '#,##0 ;[Red](#,##0)';
  255. self::$_builtInFormats[39] = '#,##0.00;(#,##0.00)';
  256. self::$_builtInFormats[40] = '#,##0.00;[Red](#,##0.00)';
  257. self::$_builtInFormats[45] = 'mm:ss';
  258. self::$_builtInFormats[46] = '[h]:mm:ss';
  259. self::$_builtInFormats[47] = 'mmss.0';
  260. self::$_builtInFormats[48] = '##0.0E+0';
  261. self::$_builtInFormats[49] = '@';
  262. // CHT
  263. self::$_builtInFormats[27] = '[$-404]e/m/d';
  264. self::$_builtInFormats[30] = 'm/d/yy';
  265. self::$_builtInFormats[36] = '[$-404]e/m/d';
  266. self::$_builtInFormats[50] = '[$-404]e/m/d';
  267. self::$_builtInFormats[57] = '[$-404]e/m/d';
  268. // THA
  269. self::$_builtInFormats[59] = 't0';
  270. self::$_builtInFormats[60] = 't0.00';
  271. self::$_builtInFormats[61] = 't#,##0';
  272. self::$_builtInFormats[62] = 't#,##0.00';
  273. self::$_builtInFormats[67] = 't0%';
  274. self::$_builtInFormats[68] = 't0.00%';
  275. self::$_builtInFormats[69] = 't# ?/?';
  276. self::$_builtInFormats[70] = 't# ??/??';
  277. }
  278. if (array_key_exists($pIndex, self::$_builtInFormats)) {
  279. return self::$_builtInFormats[$pIndex];
  280. }
  281. return '';
  282. }
  283. /**
  284. * Get hash code
  285. *
  286. * @return string Hash code
  287. */
  288. public function getHashCode() {
  289. if ($this->_isSupervisor) {
  290. return $this->getSharedComponent()->getHashCode();
  291. }
  292. return md5(
  293. $this->_formatCode
  294. . __CLASS__
  295. );
  296. }
  297. /**
  298. * Hash index
  299. *
  300. * @var string
  301. */
  302. private $_hashIndex;
  303. /**
  304. * Get hash index
  305. *
  306. * Note that this index may vary during script execution! Only reliable moment is
  307. * while doing a write of a workbook and when changes are not allowed.
  308. *
  309. * @return string Hash index
  310. */
  311. public function getHashIndex() {
  312. return $this->_hashIndex;
  313. }
  314. /**
  315. * Set hash index
  316. *
  317. * Note that this index may vary during script execution! Only reliable moment is
  318. * while doing a write of a workbook and when changes are not allowed.
  319. *
  320. * @param string $value Hash index
  321. */
  322. public function setHashIndex($value) {
  323. $this->_hashIndex = $value;
  324. }
  325. /**
  326. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  327. */
  328. public function __clone() {
  329. $vars = get_object_vars($this);
  330. foreach ($vars as $key => $value) {
  331. if (is_object($value)) {
  332. $this->$key = clone $value;
  333. } else {
  334. $this->$key = $value;
  335. }
  336. }
  337. }
  338. /**
  339. * Convert a value in a pre-defined format to a PHP string
  340. *
  341. * @param mixed $value Value to format
  342. * @param string $format Format code
  343. * @return string Formatted string
  344. */
  345. public static function toFormattedString($value = '', $format = '') {
  346. if (!is_numeric($value)) return $value;
  347. if (preg_match("/^[hmsdy]/i", $format)) { // custom datetime format
  348. // dvc: convert Excel formats to PHP date formats
  349. // first remove escapes related to non-format characters
  350. // OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case
  351. $format = strtolower($format);
  352. $format = str_replace('\\', '', $format);
  353. // 4-digit year
  354. $format = str_replace('yyyy', 'Y', $format);
  355. // 2-digit year
  356. $format = str_replace('yy', 'y', $format);
  357. // first letter of month - no php equivalent
  358. $format = str_replace('mmmmm', 'M', $format);
  359. // full month name
  360. $format = str_replace('mmmm', 'F', $format);
  361. // short month name
  362. $format = str_replace('mmm', 'M', $format);
  363. // mm is minutes if time or month w/leading zero
  364. $format = str_replace(':mm', ':i', $format);
  365. // tmp place holder
  366. $format = str_replace('mm', 'x', $format);
  367. // month no leading zero
  368. $format = str_replace('m', 'n', $format);
  369. // month leading zero
  370. $format = str_replace('x', 'm', $format);
  371. // 12-hour suffix
  372. $format = str_replace('am/pm', 'A', $format);
  373. // tmp place holder
  374. $format = str_replace('dd', 'x', $format);
  375. // days no leading zero
  376. $format = str_replace('d', 'j', $format);
  377. // days leading zero
  378. $format = str_replace('x', 'd', $format);
  379. // seconds
  380. $format = str_replace('ss', 's', $format);
  381. // fractional seconds - no php equivalent
  382. $format = str_replace('.s', '', $format);
  383. if (!strpos($format,'A')) { // 24-hour format
  384. $format = str_replace('h', 'H', $format);
  385. }
  386. // user defined flag symbol????
  387. $format = str_replace(';@', '', $format);
  388. if (is_float($value)) {
  389. return date($format, PHPExcel_Shared_Date::ExceltoPHP($value));
  390. }
  391. return date($format, $value);
  392. } else if (preg_match('/%$/', $format)) { // % number format
  393. if (preg_match('/\.[#0]+/i',$format,$m)) {
  394. $s = substr($m[0],0,1).(strlen($m[0])-1);
  395. $format = str_replace($m[0],$s,$format);
  396. }
  397. if (preg_match('/^[#0]+/',$format,$m)) {
  398. $format = str_replace($m[0],strlen($m[0]),$format);
  399. }
  400. $format = '%' . str_replace('%',"f%%",$format);
  401. return sprintf($format, $value);
  402. } else {
  403. if (preg_match ("/^([0-9.,-]+)$/", $value)) {
  404. if ($format === self::FORMAT_NUMBER)
  405. return sprintf('%1.0f', $value);
  406. else if ($format === self::FORMAT_NUMBER_00)
  407. return sprintf('%1.2f', $value);
  408. else if ($format === self::FORMAT_NUMBER_COMMA_SEPARATED1 || $format === self::FORMAT_NUMBER_COMMA_SEPARATED2)
  409. return number_format($value, 2, ',', '.');
  410. else if ($format === self::FORMAT_PERCENTAGE)
  411. return round( (100 * $value), 0) . '%';
  412. else if ($format === self::FORMAT_PERCENTAGE_00)
  413. return round( (100 * $value), 2) . '%';
  414. else if ($format === self::FORMAT_DATE_YYYYMMDD || $format === self::FORMAT_DATE_YYYYMMDD2)
  415. return date('Y-m-d', (1 * $value));
  416. else if ($format === self::FORMAT_DATE_DDMMYYYY)
  417. return date('d/m/Y', (1 * $value));
  418. else if ($format === 'yyyy/mm/dd;@')
  419. return date('Y/m/d', (1 * $value));
  420. else if ($format === self::FORMAT_CURRENCY_USD_SIMPLE)
  421. return '$' . number_format($value, 2);
  422. else if ($format === self::FORMAT_CURRENCY_USD)
  423. return '$' . number_format($value);
  424. else if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE)
  425. return 'EUR ' . sprintf('%1.2f', $value);
  426. }
  427. return $value;
  428. }
  429. }
  430. }