PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 421 lines | 245 code | 42 blank | 134 comment | 18 complexity | cd1d1f1720f38ab6f2e2c1343e2e1026 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 = '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. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  260. */
  261. public function __clone() {
  262. $vars = get_object_vars($this);
  263. foreach ($vars as $key => $value) {
  264. if (is_object($value)) {
  265. $this->$key = clone $value;
  266. } else {
  267. $this->$key = $value;
  268. }
  269. }
  270. }
  271. /**
  272. * Convert a value in a pre-defined format to a PHP string
  273. *
  274. * @param mixed $value Value to format
  275. * @param string $format Format code
  276. * @return string Formatted string
  277. */
  278. public static function toFormattedString($value = '', $format = '') {
  279. if (preg_match("/^[hmsdy]/i", $format)) { // custom datetime format
  280. // dvc: convert Excel formats to PHP date formats
  281. // first remove escapes related to non-format characters
  282. $format = str_replace('\\', '', $format);
  283. // 4-digit year
  284. $format = str_replace('yyyy', 'Y', $format);
  285. // 2-digit year
  286. $format = str_replace('yy', 'y', $format);
  287. // first letter of month - no php equivalent
  288. $format = str_replace('mmmmm', 'M', $format);
  289. // full month name
  290. $format = str_replace('mmmm', 'F', $format);
  291. // short month name
  292. $format = str_replace('mmm', 'M', $format);
  293. // mm is minutes if time or month w/leading zero
  294. $format = str_replace(':mm', ':i', $format);
  295. // tmp place holder
  296. $format = str_replace('mm', 'x', $format);
  297. // month no leading zero
  298. $format = str_replace('m', 'n', $format);
  299. // month leading zero
  300. $format = str_replace('x', 'm', $format);
  301. // 12-hour suffix
  302. $format = str_replace('AM/PM', 'A', $format);
  303. // tmp place holder
  304. $format = str_replace('dd', 'x', $format);
  305. // days no leading zero
  306. $format = str_replace('d', 'j', $format);
  307. // days leading zero
  308. $format = str_replace('x', 'd', $format);
  309. // seconds
  310. $format = str_replace('ss', 's', $format);
  311. // fractional seconds - no php equivalent
  312. $format = str_replace('.S', '', $format);
  313. if (!strpos($format,'A')) { // 24-hour format
  314. $format = str_replace('h', 'H', $format);
  315. }
  316. // user defined flag symbol????
  317. $format = str_replace(';@', '', $format);
  318. return date($format, (1 * $value));
  319. } else if (preg_match('/%$/', $format)) { // % number format
  320. if (preg_match('/\.[#0]+/i',$format,$m)) {
  321. $s = substr($m[0],0,1).(strlen($m[0])-1);
  322. $format = str_replace($m[0],$s,$format);
  323. }
  324. if (preg_match('/^[#0]+/',$format,$m)) {
  325. $format = str_replace($m[0],strlen($m[0]),$format);
  326. }
  327. $format = '%' . str_replace('%',"f%%",$format);
  328. return sprintf($format, $value);
  329. } else {
  330. if (preg_match ("/^([0-9.,-]+)$/", $value)) {
  331. switch ($format) {
  332. case self::FORMAT_NUMBER:
  333. return sprintf('%1.0f', $value);
  334. case self::FORMAT_NUMBER_00:
  335. return sprintf('%1.2f', $value);
  336. case self::FORMAT_NUMBER_COMMA_SEPARATED1:
  337. case self::FORMAT_NUMBER_COMMA_SEPARATED2:
  338. return number_format($value, 2, ',', '.');
  339. case self::FORMAT_PERCENTAGE:
  340. return round( (100 * $value), 0) . '%';
  341. case self::FORMAT_PERCENTAGE_00:
  342. return round( (100 * $value), 2) . '%';
  343. case self::FORMAT_DATE_YYYYMMDD:
  344. return date('Y-m-d', (1 * $value));
  345. case self::FORMAT_DATE_DDMMYYYY:
  346. return date('d/m/Y', (1 * $value));
  347. case 'yyyy/mm/dd;@':
  348. return date('Y/m/d', (1 * $value));
  349. case self::FORMAT_CURRENCY_USD_SIMPLE:
  350. return '$' . number_format($value, 2);
  351. case self::FORMAT_CURRENCY_USD:
  352. return '$' . number_format($value);
  353. case self::FORMAT_CURRENCY_EUR_SIMPLE:
  354. return 'EUR ' . sprintf('%1.2f', $value);
  355. }
  356. }
  357. return $value;
  358. }
  359. }
  360. }