PageRenderTime 56ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/code/bugfix_templates/SilvercartZendLocaleFormatDecorator.php

https://bitbucket.org/silvercart/silvercart/
PHP | 255 lines | 147 code | 23 blank | 85 comment | 55 complexity | 3fb1bb03aba1e30aef0091ac2f237f60 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010, 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart 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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage BugfixTemplates
  22. */
  23. /**
  24. * Bugfix for the Zend Locale Format class.
  25. *
  26. * Altered line 141: substr => iconv_substr
  27. * Altered line 196: and ($rest != '?')
  28. * Altered line 207: commented
  29. *
  30. * @package Silvercart
  31. * @subpackage BugfixTemplates
  32. * @author Sascha Koehler <skoehler@pixeltricks.de>
  33. * @copyright 2011 pixeltricks GmbH
  34. * @since 02.02.2011
  35. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  36. */
  37. class SilvercartZendLocaleFormatDecorator extends Zend_Locale_Format {
  38. /**
  39. * Contains the decorated class.
  40. *
  41. * @var Zend_Locale_Format
  42. *
  43. * @author Sascha Koehler <skoehler@pixeltricks.de>
  44. * @copyright 2011 pixeltricks GmbH
  45. * @since 02.02.2011
  46. */
  47. protected $cZend_Locale_Format;
  48. /**
  49. * Constructor.
  50. *
  51. * @param Zend_Locale_Format $cZend_Locale_Format the original class
  52. *
  53. * @return void
  54. *
  55. * @author Sascha Koehler <skoehler@pixeltricks.de>
  56. * @copyright 2011 pixeltricks GmbH
  57. * @since 02.02.2011
  58. */
  59. public function __construct(Zend_Locale_Format $cZend_Locale_Format) {
  60. $this->cZend_Locale_Format = $cZend_Locale_Format;
  61. }
  62. /**
  63. * Returns a locale formatted number depending on the given options.
  64. * The seperation and fraction sign is used from the set locale.
  65. * ##0.# -> 12345.12345 -> 12345.12345
  66. * ##0.00 -> 12345.12345 -> 12345.12
  67. * ##,##0.00 -> 12345.12345 -> 12,345.12
  68. *
  69. * @param string $value Localized number string
  70. * @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
  71. *
  72. * @return string locale formatted number
  73. *
  74. * @throws Zend_Locale_Exception
  75. *
  76. * @author Sascha Koehler <skoehler@pixeltricks.de>
  77. * @since 02.02.2011
  78. */
  79. public static function toNumber($value, array $options = array()) {
  80. // load class within method for speed
  81. require_once 'Zend/Locale/Math.php';
  82. $value = Zend_Locale_Math::normalize($value);
  83. $options = self::_checkOptions($options) + self::$_options;
  84. $options['locale'] = (string) $options['locale'];
  85. // Get correct signs for this locale
  86. $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
  87. iconv_set_encoding('internal_encoding', 'UTF-8');
  88. // Get format
  89. $format = $options['number_format'];
  90. if ($format === null) {
  91. $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
  92. if (iconv_strpos($format, ';') !== false) {
  93. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
  94. $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
  95. } else {
  96. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  97. }
  98. }
  99. } else {
  100. // seperate negative format pattern when available
  101. if (iconv_strpos($format, ';') !== false) {
  102. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
  103. $format = iconv_substr($format, iconv_strpos($format, ';') + 1);
  104. } else {
  105. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  106. }
  107. }
  108. if (strpos($format, '.')) {
  109. if (is_numeric($options['precision'])) {
  110. $value = Zend_Locale_Math::round($value, $options['precision']);
  111. } else {
  112. if (substr($format, strpos($format, '.') + 1, 3) == '###') {
  113. $options['precision'] = null;
  114. } else {
  115. $options['precision'] = strlen(substr($format, strpos($format, '.') + 1,
  116. strrpos($format, '0') - strpos($format, '.')));
  117. $format = substr($format, 0, strpos($format, '.') + 1) . '###'
  118. . substr($format, strrpos($format, '0') + 1);
  119. }
  120. }
  121. } else {
  122. $value = Zend_Locale_Math::round($value, 0);
  123. $options['precision'] = 0;
  124. }
  125. $value = Zend_Locale_Math::normalize($value);
  126. }
  127. if (strpos($format, '0') === false) {
  128. require_once 'Zend/Locale/Exception.php';
  129. throw new Zend_Locale_Exception('Wrong format... missing 0');
  130. }
  131. // get number parts
  132. $pos = iconv_strpos($value, '.');
  133. if ($pos !== false) {
  134. if ($options['precision'] === null) {
  135. $precstr = iconv_substr($value, $pos + 1);
  136. } else {
  137. $precstr = iconv_substr($value, $pos + 1, $options['precision']);
  138. if (iconv_strlen($precstr) < $options['precision']) {
  139. $precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
  140. }
  141. }
  142. } else {
  143. if ($options['precision'] > 0) {
  144. $precstr = str_pad("0", ($options['precision']), "0");
  145. }
  146. }
  147. if ($options['precision'] === null) {
  148. if (isset($precstr)) {
  149. $options['precision'] = iconv_strlen($precstr);
  150. } else {
  151. $options['precision'] = 0;
  152. }
  153. }
  154. // get fraction and format lengths
  155. if (strpos($value, '.') !== false) {
  156. $number = substr((string) $value, 0, strpos($value, '.'));
  157. } else {
  158. $number = $value;
  159. }
  160. $prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
  161. $prec = Zend_Locale_Math::normalize($prec);
  162. if (iconv_strpos($prec, '-') !== false) {
  163. $prec = iconv_substr($prec, 1);
  164. }
  165. if (($prec == 0) and ($options['precision'] > 0)) {
  166. $prec = "0.0";
  167. }
  168. if (($options['precision'] + 2) > iconv_strlen($prec)) {
  169. $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
  170. }
  171. if (iconv_strpos($number, '-') !== false) {
  172. $number = iconv_substr($number, 1);
  173. }
  174. $group = iconv_strrpos($format, ',');
  175. $group2 = iconv_strpos ($format, ',');
  176. $point = iconv_strpos ($format, '0');
  177. // Add fraction
  178. $rest = "";
  179. if (($value < 0) && (strpos($format, '.'))) {
  180. $rest = iconv_substr(iconv_substr($format, strpos($format, '.') + 1), -1, 1);
  181. }
  182. if ($options['precision'] == '0') {
  183. $format = iconv_substr($format, 0, $point) . iconv_substr($format, iconv_strrpos($format, '#') + 2);
  184. } else {
  185. $format = iconv_substr($format, 0, $point) . $symbols['decimal']
  186. . iconv_substr($prec, 2)
  187. . iconv_substr($format, iconv_strrpos($format, '#') + 1 + strlen($prec));
  188. }
  189. if (($value < 0) and ($rest != '0') and ($rest != '#') and ($rest != '?')) {
  190. //$format .= $rest;
  191. }
  192. // Add seperation
  193. if ($group == 0) {
  194. // no seperation
  195. $format = $number . iconv_substr($format, $point);
  196. } else if ($group == $group2) {
  197. // only 1 seperation
  198. $seperation = ($point - $group);
  199. for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
  200. if (iconv_substr($number, 0, $x - $seperation) !== "") {
  201. $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
  202. . iconv_substr($number, $x - $seperation);
  203. }
  204. }
  205. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  206. } else {
  207. // 2 seperations
  208. if (iconv_strlen($number) > ($point - $group)) {
  209. $seperation = ($point - $group);
  210. $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
  211. . iconv_substr($number, iconv_strlen($number) - $seperation);
  212. if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
  213. $seperation2 = ($group - $group2 - 1);
  214. for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
  215. $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
  216. . iconv_substr($number, $x - $seperation2);
  217. }
  218. }
  219. }
  220. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  221. }
  222. // set negative sign
  223. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
  224. if (iconv_strpos($format, '-') === false) {
  225. $format = $symbols['minus'] . $format;
  226. } else {
  227. $format = str_replace('-', $symbols['minus'], $format);
  228. }
  229. }
  230. return (string) $format;
  231. }
  232. }