PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Calculation/TextData.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 715 lines | 442 code | 86 blank | 187 comment | 86 complexity | b3d72421f6b6f98f0c59effbbd8cbe03 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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_Calculation
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /** PHPExcel root directory */
  28. if (! defined('PHPEXCEL_ROOT'))
  29. {
  30. /**
  31. * @ignore
  32. */
  33. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  34. require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  35. }
  36. /**
  37. * PHPExcel_Calculation_TextData
  38. *
  39. * @category PHPExcel
  40. * @package PHPExcel_Calculation
  41. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  42. */
  43. class PHPExcel_Calculation_TextData
  44. {
  45. private static $_invalidChars = Null;
  46. private static function _uniord($c)
  47. {
  48. if (ord($c{0}) >= 0 && ord($c{0}) <= 127)
  49. return ord($c{0});
  50. if (ord($c{0}) >= 192 && ord($c{0}) <= 223)
  51. return (ord($c{0}) - 192) * 64 + (ord($c{1}) - 128);
  52. if (ord($c{0}) >= 224 && ord($c{0}) <= 239)
  53. return (ord($c{0}) - 224) * 4096 + (ord($c{1}) - 128) * 64 + (ord($c{2}) - 128);
  54. if (ord($c{0}) >= 240 && ord($c{0}) <= 247)
  55. return (ord($c{0}) - 240) * 262144 + (ord($c{1}) - 128) * 4096 + (ord($c{2}) - 128) * 64 + (ord($c{3}) - 128);
  56. if (ord($c{0}) >= 248 && ord($c{0}) <= 251)
  57. return (ord($c{0}) - 248) * 16777216 + (ord($c{1}) - 128) * 262144 + (ord($c{2}) - 128) * 4096 + (ord($c{3}) - 128) * 64 + (ord($c{4}) - 128);
  58. if (ord($c{0}) >= 252 && ord($c{0}) <= 253)
  59. return (ord($c{0}) - 252) * 1073741824 + (ord($c{1}) - 128) * 16777216 + (ord($c{2}) - 128) * 262144 + (ord($c{3}) - 128) * 4096 + (ord($c{4}) - 128) * 64 + (ord($c{5}) - 128);
  60. if (ord($c{0}) >= 254 && ord($c{0}) <= 255) //error
  61. return PHPExcel_Calculation_Functions :: VALUE();
  62. return 0;
  63. } // function _uniord()
  64. /**
  65. * CHARACTER
  66. *
  67. * @param string $character Value
  68. * @return int
  69. */
  70. public static function CHARACTER($character)
  71. {
  72. $character = PHPExcel_Calculation_Functions :: flattenSingleValue($character);
  73. if ((! is_numeric($character)) || ($character < 0))
  74. {
  75. return PHPExcel_Calculation_Functions :: VALUE();
  76. }
  77. if (function_exists('mb_convert_encoding'))
  78. {
  79. return mb_convert_encoding('&#' . intval($character) . ';', 'UTF-8', 'HTML-ENTITIES');
  80. }
  81. else
  82. {
  83. return chr(intval($character));
  84. }
  85. }
  86. /**
  87. * TRIMNONPRINTABLE
  88. *
  89. * @param mixed $value Value to check
  90. * @return string
  91. */
  92. public static function TRIMNONPRINTABLE($stringValue = '')
  93. {
  94. $stringValue = PHPExcel_Calculation_Functions :: flattenSingleValue($stringValue);
  95. if (is_bool($stringValue))
  96. {
  97. $stringValue = ($stringValue) ? 'TRUE' : 'FALSE';
  98. }
  99. if (self :: $_invalidChars == Null)
  100. {
  101. self :: $_invalidChars = range(chr(0), chr(31));
  102. }
  103. if (is_string($stringValue) || is_numeric($stringValue))
  104. {
  105. return str_replace(self :: $_invalidChars, '', trim($stringValue, "\x00..\x1F"));
  106. }
  107. return Null;
  108. } // function TRIMNONPRINTABLE()
  109. /**
  110. * TRIMSPACES
  111. *
  112. * @param mixed $value Value to check
  113. * @return string
  114. */
  115. public static function TRIMSPACES($stringValue = '')
  116. {
  117. $stringValue = PHPExcel_Calculation_Functions :: flattenSingleValue($stringValue);
  118. if (is_string($stringValue) || is_numeric($stringValue))
  119. {
  120. return trim(preg_replace('/ +/', ' ', $stringValue));
  121. }
  122. return Null;
  123. } // function TRIMSPACES()
  124. /**
  125. * ASCIICODE
  126. *
  127. * @param string $character Value
  128. * @return int
  129. */
  130. public static function ASCIICODE($characters)
  131. {
  132. $characters = PHPExcel_Calculation_Functions :: flattenSingleValue($characters);
  133. if (is_bool($characters))
  134. {
  135. if (PHPExcel_Calculation_Functions :: getCompatibilityMode() == PHPExcel_Calculation_Functions :: COMPATIBILITY_OPENOFFICE)
  136. {
  137. $characters = (int) $characters;
  138. }
  139. else
  140. {
  141. if ($characters)
  142. {
  143. $characters = 'True';
  144. }
  145. else
  146. {
  147. $characters = 'False';
  148. }
  149. }
  150. }
  151. $character = $characters;
  152. if ((function_exists('mb_strlen')) && (function_exists('mb_substr')))
  153. {
  154. if (mb_strlen($characters, 'UTF-8') > 1)
  155. {
  156. $character = mb_substr($characters, 0, 1, 'UTF-8');
  157. }
  158. return self :: _uniord($character);
  159. }
  160. else
  161. {
  162. if (strlen($characters) > 0)
  163. {
  164. $character = substr($characters, 0, 1);
  165. }
  166. return ord($character);
  167. }
  168. } // function ASCIICODE()
  169. /**
  170. * CONCATENATE
  171. *
  172. * @return string
  173. */
  174. public static function CONCATENATE()
  175. {
  176. // Return value
  177. $returnValue = '';
  178. // Loop through arguments
  179. $aArgs = PHPExcel_Calculation_Functions :: flattenArray(func_get_args());
  180. foreach ($aArgs as $arg)
  181. {
  182. if (is_bool($arg))
  183. {
  184. if (PHPExcel_Calculation_Functions :: getCompatibilityMode() == PHPExcel_Calculation_Functions :: COMPATIBILITY_OPENOFFICE)
  185. {
  186. $arg = (int) $arg;
  187. }
  188. else
  189. {
  190. if ($arg)
  191. {
  192. $arg = 'TRUE';
  193. }
  194. else
  195. {
  196. $arg = 'FALSE';
  197. }
  198. }
  199. }
  200. $returnValue .= $arg;
  201. }
  202. // Return
  203. return $returnValue;
  204. } // function CONCATENATE()
  205. /**
  206. * DOLLAR
  207. *
  208. * This function converts a number to text using currency format, with the decimals rounded to the specified place.
  209. * The format used is $#,##0.00_);($#,##0.00)..
  210. *
  211. * @param float $value The value to format
  212. * @param int $decimals The number of digits to display to the right of the decimal point.
  213. * If decimals is negative, number is rounded to the left of the decimal point.
  214. * If you omit decimals, it is assumed to be 2
  215. * @return string
  216. */
  217. public static function DOLLAR($value = 0, $decimals = 2)
  218. {
  219. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  220. $decimals = is_null($decimals) ? 0 : PHPExcel_Calculation_Functions :: flattenSingleValue($decimals);
  221. // Validate parameters
  222. if (! is_numeric($value) || ! is_numeric($decimals))
  223. {
  224. return PHPExcel_Calculation_Functions :: NaN();
  225. }
  226. $decimals = floor($decimals);
  227. if ($decimals > 0)
  228. {
  229. return money_format('%.' . $decimals . 'n', $value);
  230. }
  231. else
  232. {
  233. $round = pow(10, abs($decimals));
  234. if ($value < 0)
  235. {
  236. $round = 0 - $round;
  237. }
  238. $value = PHPExcel_Calculation_MathTrig :: MROUND($value, $round);
  239. // The implementation of money_format used if the standard PHP function is not available can't handle decimal places of 0,
  240. // so we display to 1 dp and chop off that character and the decimal separator using substr
  241. return substr(money_format('%.1n', $value), 0, - 2);
  242. }
  243. } // function DOLLAR()
  244. /**
  245. * SEARCHSENSITIVE
  246. *
  247. * @param string $needle The string to look for
  248. * @param string $haystack The string in which to look
  249. * @param int $offset Offset within $haystack
  250. * @return string
  251. */
  252. public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1)
  253. {
  254. $needle = PHPExcel_Calculation_Functions :: flattenSingleValue($needle);
  255. $haystack = PHPExcel_Calculation_Functions :: flattenSingleValue($haystack);
  256. $offset = PHPExcel_Calculation_Functions :: flattenSingleValue($offset);
  257. if (! is_bool($needle))
  258. {
  259. if (is_bool($haystack))
  260. {
  261. $haystack = ($haystack) ? 'TRUE' : 'FALSE';
  262. }
  263. if (($offset > 0) && (strlen($haystack) > $offset))
  264. {
  265. if (function_exists('mb_strpos'))
  266. {
  267. $pos = mb_strpos($haystack, $needle, -- $offset, 'UTF-8');
  268. }
  269. else
  270. {
  271. $pos = strpos($haystack, $needle, -- $offset);
  272. }
  273. if ($pos !== false)
  274. {
  275. return ++ $pos;
  276. }
  277. }
  278. }
  279. return PHPExcel_Calculation_Functions :: VALUE();
  280. } // function SEARCHSENSITIVE()
  281. /**
  282. * SEARCHINSENSITIVE
  283. *
  284. * @param string $needle The string to look for
  285. * @param string $haystack The string in which to look
  286. * @param int $offset Offset within $haystack
  287. * @return string
  288. */
  289. public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1)
  290. {
  291. $needle = PHPExcel_Calculation_Functions :: flattenSingleValue($needle);
  292. $haystack = PHPExcel_Calculation_Functions :: flattenSingleValue($haystack);
  293. $offset = PHPExcel_Calculation_Functions :: flattenSingleValue($offset);
  294. if (! is_bool($needle))
  295. {
  296. if (is_bool($haystack))
  297. {
  298. $haystack = ($haystack) ? 'TRUE' : 'FALSE';
  299. }
  300. if (($offset > 0) && (strlen($haystack) > $offset))
  301. {
  302. if (function_exists('mb_stripos'))
  303. {
  304. $pos = mb_stripos($haystack, $needle, -- $offset, 'UTF-8');
  305. }
  306. else
  307. {
  308. $pos = stripos($haystack, $needle, -- $offset);
  309. }
  310. if ($pos !== false)
  311. {
  312. return ++ $pos;
  313. }
  314. }
  315. }
  316. return PHPExcel_Calculation_Functions :: VALUE();
  317. } // function SEARCHINSENSITIVE()
  318. /**
  319. * FIXEDFORMAT
  320. *
  321. * @param mixed $value Value to check
  322. * @return boolean
  323. */
  324. public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false)
  325. {
  326. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  327. $decimals = PHPExcel_Calculation_Functions :: flattenSingleValue($decimals);
  328. $no_commas = PHPExcel_Calculation_Functions :: flattenSingleValue($no_commas);
  329. $valueResult = round($value, $decimals);
  330. if ($decimals < 0)
  331. {
  332. $decimals = 0;
  333. }
  334. if (! $no_commas)
  335. {
  336. $valueResult = number_format($valueResult, $decimals);
  337. }
  338. return (string) $valueResult;
  339. } // function FIXEDFORMAT()
  340. /**
  341. * LEFT
  342. *
  343. * @param string $value Value
  344. * @param int $chars Number of characters
  345. * @return string
  346. */
  347. public static function LEFT($value = '', $chars = 1)
  348. {
  349. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  350. $chars = PHPExcel_Calculation_Functions :: flattenSingleValue($chars);
  351. if ($chars < 0)
  352. {
  353. return PHPExcel_Calculation_Functions :: VALUE();
  354. }
  355. if (is_bool($value))
  356. {
  357. $value = ($value) ? 'TRUE' : 'FALSE';
  358. }
  359. if (function_exists('mb_substr'))
  360. {
  361. return mb_substr($value, 0, $chars, 'UTF-8');
  362. }
  363. else
  364. {
  365. return substr($value, 0, $chars);
  366. }
  367. } // function LEFT()
  368. /**
  369. * MID
  370. *
  371. * @param string $value Value
  372. * @param int $start Start character
  373. * @param int $chars Number of characters
  374. * @return string
  375. */
  376. public static function MID($value = '', $start = 1, $chars = null)
  377. {
  378. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  379. $start = PHPExcel_Calculation_Functions :: flattenSingleValue($start);
  380. $chars = PHPExcel_Calculation_Functions :: flattenSingleValue($chars);
  381. if (($start < 1) || ($chars < 0))
  382. {
  383. return PHPExcel_Calculation_Functions :: VALUE();
  384. }
  385. if (is_bool($value))
  386. {
  387. $value = ($value) ? 'TRUE' : 'FALSE';
  388. }
  389. if (function_exists('mb_substr'))
  390. {
  391. return mb_substr($value, -- $start, $chars, 'UTF-8');
  392. }
  393. else
  394. {
  395. return substr($value, -- $start, $chars);
  396. }
  397. } // function MID()
  398. /**
  399. * RIGHT
  400. *
  401. * @param string $value Value
  402. * @param int $chars Number of characters
  403. * @return string
  404. */
  405. public static function RIGHT($value = '', $chars = 1)
  406. {
  407. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  408. $chars = PHPExcel_Calculation_Functions :: flattenSingleValue($chars);
  409. if ($chars < 0)
  410. {
  411. return PHPExcel_Calculation_Functions :: VALUE();
  412. }
  413. if (is_bool($value))
  414. {
  415. $value = ($value) ? 'TRUE' : 'FALSE';
  416. }
  417. if ((function_exists('mb_substr')) && (function_exists('mb_strlen')))
  418. {
  419. return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
  420. }
  421. else
  422. {
  423. return substr($value, strlen($value) - $chars);
  424. }
  425. } // function RIGHT()
  426. /**
  427. * STRINGLENGTH
  428. *
  429. * @param string $value Value
  430. * @param int $chars Number of characters
  431. * @return string
  432. */
  433. public static function STRINGLENGTH($value = '')
  434. {
  435. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  436. if (is_bool($value))
  437. {
  438. $value = ($value) ? 'TRUE' : 'FALSE';
  439. }
  440. if (function_exists('mb_strlen'))
  441. {
  442. return mb_strlen($value, 'UTF-8');
  443. }
  444. else
  445. {
  446. return strlen($value);
  447. }
  448. } // function STRINGLENGTH()
  449. /**
  450. * LOWERCASE
  451. *
  452. * Converts a string value to upper case.
  453. *
  454. * @param string $mixedCaseString
  455. * @return string
  456. */
  457. public static function LOWERCASE($mixedCaseString)
  458. {
  459. $mixedCaseString = PHPExcel_Calculation_Functions :: flattenSingleValue($mixedCaseString);
  460. if (is_bool($mixedCaseString))
  461. {
  462. $mixedCaseString = ($mixedCaseString) ? 'TRUE' : 'FALSE';
  463. }
  464. if (function_exists('mb_convert_case'))
  465. {
  466. return mb_convert_case($mixedCaseString, MB_CASE_LOWER, 'UTF-8');
  467. }
  468. else
  469. {
  470. return strtoupper($mixedCaseString);
  471. }
  472. } // function LOWERCASE()
  473. /**
  474. * UPPERCASE
  475. *
  476. * Converts a string value to upper case.
  477. *
  478. * @param string $mixedCaseString
  479. * @return string
  480. */
  481. public static function UPPERCASE($mixedCaseString)
  482. {
  483. $mixedCaseString = PHPExcel_Calculation_Functions :: flattenSingleValue($mixedCaseString);
  484. if (is_bool($mixedCaseString))
  485. {
  486. $mixedCaseString = ($mixedCaseString) ? 'TRUE' : 'FALSE';
  487. }
  488. if (function_exists('mb_convert_case'))
  489. {
  490. return mb_convert_case($mixedCaseString, MB_CASE_UPPER, 'UTF-8');
  491. }
  492. else
  493. {
  494. return strtoupper($mixedCaseString);
  495. }
  496. } // function UPPERCASE()
  497. /**
  498. * PROPERCASE
  499. *
  500. * Converts a string value to upper case.
  501. *
  502. * @param string $mixedCaseString
  503. * @return string
  504. */
  505. public static function PROPERCASE($mixedCaseString)
  506. {
  507. $mixedCaseString = PHPExcel_Calculation_Functions :: flattenSingleValue($mixedCaseString);
  508. if (is_bool($mixedCaseString))
  509. {
  510. $mixedCaseString = ($mixedCaseString) ? 'TRUE' : 'FALSE';
  511. }
  512. if (function_exists('mb_convert_case'))
  513. {
  514. return mb_convert_case($mixedCaseString, MB_CASE_TITLE, 'UTF-8');
  515. }
  516. else
  517. {
  518. return ucwords($mixedCaseString);
  519. }
  520. } // function PROPERCASE()
  521. /**
  522. * REPLACE
  523. *
  524. * @param string $value Value
  525. * @param int $start Start character
  526. * @param int $chars Number of characters
  527. * @return string
  528. */
  529. public static function REPLACE($oldText = '', $start = 1, $chars = null, $newText)
  530. {
  531. $oldText = PHPExcel_Calculation_Functions :: flattenSingleValue($oldText);
  532. $start = PHPExcel_Calculation_Functions :: flattenSingleValue($start);
  533. $chars = PHPExcel_Calculation_Functions :: flattenSingleValue($chars);
  534. $newText = PHPExcel_Calculation_Functions :: flattenSingleValue($newText);
  535. $left = self :: LEFT($oldText, $start - 1);
  536. $right = self :: RIGHT($oldText, self :: STRINGLENGTH($oldText) - ($start + $chars) + 1);
  537. return $left . $newText . $right;
  538. } // function REPLACE()
  539. /**
  540. * SUBSTITUTE
  541. *
  542. * @param string $text Value
  543. * @param string $fromText From Value
  544. * @param string $toText To Value
  545. * @param integer $instance Instance Number
  546. * @return string
  547. */
  548. public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0)
  549. {
  550. $text = PHPExcel_Calculation_Functions :: flattenSingleValue($text);
  551. $fromText = PHPExcel_Calculation_Functions :: flattenSingleValue($fromText);
  552. $toText = PHPExcel_Calculation_Functions :: flattenSingleValue($toText);
  553. $instance = floor(PHPExcel_Calculation_Functions :: flattenSingleValue($instance));
  554. if ($instance == 0)
  555. {
  556. if (function_exists('mb_str_replace'))
  557. {
  558. return mb_str_replace($fromText, $toText, $text);
  559. }
  560. else
  561. {
  562. return str_replace($fromText, $toText, $text);
  563. }
  564. }
  565. else
  566. {
  567. $pos = - 1;
  568. while ($instance > 0)
  569. {
  570. if (function_exists('mb_strpos'))
  571. {
  572. $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
  573. }
  574. else
  575. {
  576. $pos = strpos($text, $fromText, $pos + 1);
  577. }
  578. if ($pos === false)
  579. {
  580. break;
  581. }
  582. -- $instance;
  583. }
  584. if ($pos !== false)
  585. {
  586. if (function_exists('mb_strlen'))
  587. {
  588. return self :: REPLACE($text, ++ $pos, mb_strlen($fromText, 'UTF-8'), $toText);
  589. }
  590. else
  591. {
  592. return self :: REPLACE($text, ++ $pos, strlen($fromText), $toText);
  593. }
  594. }
  595. }
  596. return $left . $newText . $right;
  597. } // function SUBSTITUTE()
  598. /**
  599. * RETURNSTRING
  600. *
  601. * @param mixed $value Value to check
  602. * @return boolean
  603. */
  604. public static function RETURNSTRING($testValue = '')
  605. {
  606. $testValue = PHPExcel_Calculation_Functions :: flattenSingleValue($testValue);
  607. if (is_string($testValue))
  608. {
  609. return $testValue;
  610. }
  611. return Null;
  612. } // function RETURNSTRING()
  613. /**
  614. * TEXTFORMAT
  615. *
  616. * @param mixed $value Value to check
  617. * @return boolean
  618. */
  619. public static function TEXTFORMAT($value, $format)
  620. {
  621. $value = PHPExcel_Calculation_Functions :: flattenSingleValue($value);
  622. $format = PHPExcel_Calculation_Functions :: flattenSingleValue($format);
  623. if ((is_string($value)) && (! is_numeric($value)) && PHPExcel_Shared_Date :: isDateTimeFormatCode($format))
  624. {
  625. $value = PHPExcel_Calculation_DateTime :: DATEVALUE($value);
  626. }
  627. return (string) PHPExcel_Style_NumberFormat :: toFormattedString($value, $format);
  628. } // function TEXTFORMAT()
  629. } // class PHPExcel_Calculation_TextData