PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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