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

/branches/v1.3.5/Classes/PHPExcel/Calculation/Functions.php

#
PHP | 503 lines | 196 code | 58 blank | 249 comment | 36 complexity | 7f83e0d70f3b3ed3a2f2a4cb004a9562 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 - 2007 PHPExcel, Maarten Balliauw
  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_Calculation
  23. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Calculation_Functions
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Calculation
  32. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Calculation_Functions {
  35. /**
  36. * List of error codes
  37. *
  38. * @var array
  39. */
  40. private static $_errorCodes = array("#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A");
  41. /**
  42. * DUMMY
  43. *
  44. * @return string #N/A!
  45. */
  46. public static function DUMMY() {
  47. return '#N/A';
  48. }
  49. /**
  50. * NA
  51. *
  52. * @return string #N/A!
  53. */
  54. public static function NA() {
  55. return '#N/A';
  56. }
  57. /**
  58. * LOGICAL_AND
  59. *
  60. * @return boolean
  61. */
  62. public static function LOGICAL_AND() {
  63. // Return value
  64. $returnValue = null;
  65. // Loop trough arguments
  66. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  67. foreach ($aArgs as $arg) {
  68. if (is_null($returnValue)) {
  69. $returnValue = $arg;
  70. } else {
  71. // Is it a boolean value?
  72. if (is_bool($arg)) {
  73. $returnValue = $returnValue && $arg;
  74. }
  75. }
  76. }
  77. // Return
  78. return $returnValue;
  79. }
  80. /**
  81. * LOGICAL_OR
  82. *
  83. * @return boolean
  84. */
  85. public static function LOGICAL_OR() {
  86. // Return value
  87. $returnValue = null;
  88. // Loop trough arguments
  89. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  90. foreach ($aArgs as $arg) {
  91. if (is_null($returnValue)) {
  92. $returnValue = $arg;
  93. } else {
  94. // Is it a boolean value?
  95. if (is_bool($arg)) {
  96. $returnValue = $returnValue || $arg;
  97. }
  98. }
  99. }
  100. // Return
  101. return $returnValue;
  102. }
  103. /**
  104. * SUM
  105. *
  106. * @return int
  107. */
  108. public static function SUM() {
  109. // Return value
  110. $returnValue = null;
  111. // Loop trough arguments
  112. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  113. foreach ($aArgs as $arg) {
  114. // Is it a numeric value?
  115. if (is_numeric($arg)) {
  116. if (is_null($returnValue)) {
  117. $returnValue = $arg;
  118. } else {
  119. $returnValue += $arg;
  120. }
  121. }
  122. }
  123. // Return
  124. return $returnValue;
  125. }
  126. /**
  127. * PRODUCT
  128. *
  129. * @return int
  130. */
  131. public static function PRODUCT() {
  132. // Return value
  133. $returnValue = null;
  134. // Loop trough arguments
  135. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  136. foreach ($aArgs as $arg) {
  137. // Is it a numeric value?
  138. if (is_null($returnValue)) {
  139. $returnValue = $arg;
  140. } else {
  141. $returnValue *= $arg;
  142. }
  143. }
  144. // Return
  145. return $returnValue;
  146. }
  147. /**
  148. * QUOTIENT
  149. *
  150. * @return int
  151. */
  152. public static function QUOTIENT() {
  153. // Return value
  154. $returnValue = null;
  155. // Loop trough arguments
  156. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  157. foreach ($aArgs as $arg) {
  158. // Is it a numeric value?
  159. if (is_null($returnValue)) {
  160. $returnValue = $arg;
  161. } else {
  162. $returnValue /= $arg;
  163. }
  164. }
  165. // Return
  166. return intval($returnValue);
  167. }
  168. /**
  169. * MIN
  170. *
  171. * @return int
  172. */
  173. public static function MIN() {
  174. // Return value
  175. $returnValue = 0;
  176. // Loop trough arguments
  177. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  178. $returnValue = $aArgs[0];
  179. foreach ($aArgs as $arg) {
  180. // Is it a numeric value?
  181. if (is_numeric($arg)) {
  182. if ($arg < $returnValue) {
  183. $returnValue = $arg;
  184. }
  185. }
  186. }
  187. // Return
  188. return $returnValue;
  189. }
  190. /**
  191. * MAX
  192. *
  193. * @return int
  194. */
  195. public static function MAX() {
  196. // Return value
  197. $returnValue = 0;
  198. // Loop trough arguments
  199. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  200. $returnValue = $aArgs[0];
  201. foreach ($aArgs as $arg) {
  202. // Is it a numeric value?
  203. if (is_numeric($arg)) {
  204. if ($arg > $returnValue) {
  205. $returnValue = $arg;
  206. }
  207. }
  208. }
  209. // Return
  210. return $returnValue;
  211. }
  212. /**
  213. * COUNT
  214. *
  215. * @return int
  216. */
  217. public static function COUNT() {
  218. // Return value
  219. $returnValue = 0;
  220. // Loop trough arguments
  221. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  222. $returnValue = $aArgs[0];
  223. foreach ($aArgs as $arg) {
  224. // Is it a numeric value?
  225. if (is_numeric($arg)) {
  226. $returnValue++;
  227. }
  228. }
  229. // Return
  230. return $returnValue;
  231. }
  232. /**
  233. * RAND
  234. *
  235. * @param int $min Minimal value
  236. * @param int $max Maximal value
  237. * @return int Random number
  238. */
  239. public static function RAND($min = 0, $max = 0) {
  240. $min = self::flattenSingleValue($min);
  241. $max = self::flattenSingleValue($max);
  242. if ($min == 0 && $max == 0) {
  243. return (rand(0,10000000)) / 10000000;
  244. } else {
  245. return rand($min, $max);
  246. }
  247. }
  248. /**
  249. * MOD
  250. *
  251. * @param int $a Dividend
  252. * @param int $b Divisor
  253. * @return int Remainder
  254. */
  255. public static function MOD($a = 1, $b = 1) {
  256. $a = self::flattenSingleValue($a);
  257. $b = self::flattenSingleValue($b);
  258. return $a % $b;
  259. }
  260. /**
  261. * LEFT
  262. *
  263. * @param string $value Value
  264. * @param int $chars Number of characters
  265. * @return string
  266. */
  267. public static function LEFT($value = '', $chars = null) {
  268. $value = self::flattenSingleValue($value);
  269. $chars = self::flattenSingleValue($chars);
  270. return substr($value, 0, $chars);
  271. }
  272. /**
  273. * RIGHT
  274. *
  275. * @param string $value Value
  276. * @param int $chars Number of characters
  277. * @return string
  278. */
  279. public static function RIGHT($value = '', $chars = null) {
  280. $value = self::flattenSingleValue($value);
  281. $chars = self::flattenSingleValue($chars);
  282. return substr($value, strlen($value) - $chars);
  283. }
  284. /**
  285. * MID
  286. *
  287. * @param string $value Value
  288. * @param int $start Start character
  289. * @param int $chars Number of characters
  290. * @return string
  291. */
  292. public static function MID($value = '', $start = 1, $chars = null) {
  293. $value = self::flattenSingleValue($value);
  294. $start = self::flattenSingleValue($start);
  295. $chars = self::flattenSingleValue($chars);
  296. return substr($value, $start - 1, $chars);
  297. }
  298. /**
  299. * IS_BLANK
  300. *
  301. * @param mixed $value Value to check
  302. * @return boolean
  303. */
  304. public static function IS_BLANK($value = '') {
  305. $value = self::flattenSingleValue($value);
  306. return ($value == '');
  307. }
  308. /**
  309. * IS_ERR
  310. *
  311. * @param mixed $value Value to check
  312. * @return boolean
  313. */
  314. public static function IS_ERR($value = '') {
  315. $value = self::flattenSingleValue($value);
  316. return self::IS_ERROR($value) && (!self::IS_NA($value));
  317. }
  318. /**
  319. * IS_ERROR
  320. *
  321. * @param mixed $value Value to check
  322. * @return boolean
  323. */
  324. public static function IS_ERROR($value = '') {
  325. $value = self::flattenSingleValue($value);
  326. return in_array($value, self::_errorCodes);
  327. }
  328. /**
  329. * IS_NA
  330. *
  331. * @param mixed $value Value to check
  332. * @return boolean
  333. */
  334. public static function IS_NA($value = '') {
  335. $value = self::flattenSingleValue($value);
  336. return ($value == '#N/A');
  337. }
  338. /**
  339. * IS_EVEN
  340. *
  341. * @param mixed $value Value to check
  342. * @return boolean
  343. */
  344. public static function IS_EVEN($value = 0) {
  345. $value = self::flattenSingleValue($value);
  346. while (intval($value) != $value) {
  347. $value *= 10;
  348. }
  349. return ($value % 2 == 0);
  350. }
  351. /**
  352. * IS_NUMBER
  353. *
  354. * @param mixed $value Value to check
  355. * @return boolean
  356. */
  357. public static function IS_NUMBER($value = 0) {
  358. $value = self::flattenSingleValue($value);
  359. return is_numeric($value);
  360. }
  361. /**
  362. * STATEMENT_IF
  363. *
  364. * @param mixed $value Value to check
  365. * @param mixed $truepart Value when true
  366. * @param mixed $falsepart Value when false
  367. * @return mixed
  368. */
  369. public static function STATEMENT_IF($value = true, $truepart = '', $falsepart = '') {
  370. $value = self::flattenSingleValue($value);
  371. $truepart = self::flattenSingleValue($truepart);
  372. $falsepart = self::flattenSingleValue($falsepart);
  373. return ($value ? $truepart : $falsepart);
  374. }
  375. /**
  376. * STATEMENT_IFERROR
  377. *
  378. * @param mixed $value Value to check , is also value when no error
  379. * @param mixed $errorpart Value when error
  380. * @return mixed
  381. */
  382. public static function STATEMENT_IFERROR($value = '', $errorpart = '') {
  383. return self::STATEMENT_IF(self::IS_ERROR($value), $errorpart, $value);
  384. }
  385. /**
  386. * ACCRINT
  387. *
  388. * Computes the accrued interest for a security that pays periodic interest.
  389. *
  390. * @param int $issue
  391. * @param int $firstInterest
  392. * @param int $settlement
  393. * @param int $rate
  394. * @param int $par
  395. * @param int $frequency
  396. * @param int $basis
  397. * @return int The accrued interest for a security that pays periodic interest.
  398. */
  399. /*
  400. public static function ACCRINT($issue = 0, $firstInterest = 0, $settlement = 0, $rate = 0, $par = 1000, $frequency = 1, $basis = 0) {
  401. $issue = self::flattenSingleValue($issue);
  402. $firstInterest = self::flattenSingleValue($firstInterest);
  403. $settlement = self::flattenSingleValue($settlement);
  404. $rate = self::flattenSingleValue($rate);
  405. $par = self::flattenSingleValue($par);
  406. $frequency = self::flattenSingleValue($frequency);
  407. $basis = self::flattenSingleValue($basis);
  408. // Perform checks
  409. if ($issue >= $settlement || $rate <= 0 || $par <= 0 || !($frequency == 1 || $frequency == 2 || $frequency == 4) || $basis < 0 || $basis > 4) return '#NUM!';
  410. // Calculate value
  411. return $par * ($rate / $frequency) *
  412. }
  413. */
  414. /**
  415. * Flatten multidemensional array
  416. *
  417. * @param array $array Array to be flattened
  418. * @return array Flattened array
  419. */
  420. public static function flattenArray($array) {
  421. $arrayValues = array();
  422. foreach ($array as $value) {
  423. if (is_scalar($value) || is_resource($value)) {
  424. $arrayValues[] = $value;
  425. } else if (is_array($value)) {
  426. $arrayValues = array_merge($arrayValues, PHPExcel_Calculation_Functions::flattenArray($value));
  427. }
  428. }
  429. return $arrayValues;
  430. }
  431. /**
  432. * Convert an array with one element to a flat value
  433. *
  434. * @param mixed $value Array or flat value
  435. * @return mixed
  436. */
  437. public static function flattenSingleValue($value = '') {
  438. if (is_array($value)) {
  439. $value = array_pop($value);
  440. }
  441. return $value;
  442. }
  443. }